Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 148 additions & 12 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,125 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1,num2) {
if (num1>num2){
return(num1+ " is greater")
}
else if (num1<num2){
return (num2 +" is greater")
}
else{
return ("they are equal")
}

}
console.log(maxOfTwoNumbers(10,8))


// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
const empty = []
function findLongestWord(arr) {
if(arr.length===0){
return null

}

let longest=arr[0]
for(let i=1;i<arr.length;i++){
if(arr[i].length>longest.length){
longest=arr[i]
}

}
return longest


}
console.log(findLongestWord(empty))
console.log(findLongestWord(words))

function findLongestWord() {}



// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}
function sumNumbers(arr) {
let sum=0
for(let i=0; i<arr.length; i++){
sum+=arr[i]
}
return sum
}
console.log(sumNumbers([6, 12, 1, 18, 13, 16, 2, 1, 8, 10]))




// Iteration #3.1 Bonus:
function sum() {}
const mixedArr = [6, 12, "miami", 1, true, "barca", "200", "lisboa", 8, 10];
function sum(arr) {
let total = 0;

for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (typeof element === "number") {
total += element;
} else if (typeof element === "string") {
total += element.length;
} else if (typeof element === "boolean") {
total += element ? 1 : 0;
} else {
throw new Error("Unsupported data type in array");
}
}
return total;
}
console.log(sum(mixedArr));




// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
const number= [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(arr) {
if (arr.length === 0) return null;
const sum = arr.reduce((acc, curr) => acc + curr, 0);
return sum / arr.length;
}
console.log(averageNumbers(number));


// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength() { }
function averageWordLength(arr) {
if (arr.length === 0) return null;
const totalLength = arr.reduce((acc, curr) => acc + curr.length, 0);
return totalLength / arr.length;
}
console.log(averageWordLength(words));

// Bonus - Iteration #4.1
function avg() {}
function avg(arr) {
if (arr.length === 0) return null;
const total = arr.reduce((acc, curr) => {
if (typeof curr === "number") {
return acc + curr;
} else if (typeof curr === "string") {
return acc + curr.length;
} else if (typeof curr === "boolean") {
return acc + (curr ? 1 : 0);
} else {
throw new Error("Unsupported data type in array");
}
}, 0);

return total / arr.length;
}
console.log(avg(mixedArr));


// Iteration #5: Unique arrays
const wordsUnique = [
Expand All @@ -52,14 +136,27 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}
function uniquifyArray(arr) {
if (arr.length === 0) return null;
return arr.filter((word, index) => arr.indexOf(word) === index);
}
console.log(uniquifyArray(words));




// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

function doesWordExist() {}
function doesWordExist(arr,word) {
if (arr.length === 0) return null;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === word) return true;
}
return false; // Word does not exist
}
console.log(doesWordExist(words, 'truth'));
console.log(doesWordExist(words, 'hello'));



Expand All @@ -78,7 +175,17 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}
function howManyTimes(arr,word) {
if (arr.length === 0) return 0;
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === word) count++;
}
return count;
}
console.log(howManyTimes(words, 'matter'));
console.log(howManyTimes(words, 'truth'));




Expand Down Expand Up @@ -106,7 +213,36 @@ const matrix = [
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct() {}
function greatestProduct(matrix) {
let maxProduct = 0;

for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {

if (col + 3 < matrix[row].length) {
const horizontalProduct =
matrix[row][col] *
matrix[row][col + 1] *
matrix[row][col + 2] *
matrix[row][col + 3];
maxProduct = Math.max(maxProduct, horizontalProduct);
}

if (row + 3 < matrix.length) {
const verticalProduct =
matrix[row][col] *
matrix[row + 1][col] *
matrix[row + 2][col] *
matrix[row + 3][col];
maxProduct = Math.max(maxProduct, verticalProduct);
}
}
}

return maxProduct;
}
console.log(greatestProduct(matrix));




Expand Down