Skip to content
165 changes: 150 additions & 15 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,97 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1, num2) {
return num1 < num2 ? num2 : num1
}
console.log( maxOfTwoNumbers(2,5) ); // 5 =




// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findLongestWord() {}
function findLongestWord(words) {

let longestWord = '';
for ( let word of words ) {
if ( longestWord.length < word.length ) longestWord = word
}

return longestWord ? longestWord : null;
}
console.log( findLongestWord(words) ); // crocodile =
console.log( findLongestWord([]) ); // null =




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

function sumNumbers() {}
function sumNumbers(numbers) {
let sum = 0;
for ( let number of numbers ) {
sum += number;
}
return sum;
}
console.log( sumNumbers(numbers) ); // 87 =



// Iteration #3.1 Bonus:
function sum() {}

// Iteration #3.2 Bonus:
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];

function sum(mixedArr) {
let sum = 0;
for ( let entry of mixedArr ) {

if ( typeof entry === 'object' ) {
throw new Error("Unsupported data type sir or ma'am")
} else {
sum += typeof entry === 'string' ? entry.length : entry
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super good solution! The only thing I think it would be even better is to write the statement with the types we know will be supported, first (in if). And all the rest going to else.
But anyway, in this case, the array to test is already there and the solution works perfectly

}
return sum
}
console.log( sum(mixedArr) ); // should return: 57 =




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

function averageNumbers() {}
function averageNumbers(numbers) {
return numbers.length ? sumNumbers(numbers) / numbers.length : null
}
console.log( averageNumbers(numbersAvg) ); // 6 =
console.log( averageNumbers([]) ); // null =


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

function averageWordLength() { }
function averageWordLength(words) {
let wordsLength = [];
for ( let word of words ) {
wordsLength.push( word.length );
}
return averageNumbers( wordsLength );
}

// Bonus - Iteration #4.3
function avg(mixedArr) {
return mixedArr.length ? sum(mixedArr) / mixedArr.length : null;
}
console.log( avg(mixedArr) ) // 5.7 =
console.log( avg([]) ) // null =



// Bonus - Iteration #4.1
function avg() {}

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

function uniquifyArray() {}
function uniquifyArray( words ) {
let uniqueWords = [];
for ( let word of words ) {
if ( !uniqueWords.includes(word) ) uniqueWords.push(word)
}

return uniqueWords.length ? uniqueWords : null;
}
console.log( uniquifyArray(wordsUnique) ); //
console.log( uniquifyArray([]) ); // null =




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

function doesWordExist() {}
function doesWordExist( words, search ) {
if ( !words.length ) {
return null
} else {
if ( words.includes(search) ) {
return true;
} else {
return false;
}
}
}
console.log( doesWordExist(wordsFind,"machine") ) // true =
console.log( doesWordExist(wordsFind,"machines") ) // false =
console.log( doesWordExist([],"machine") ) // null =




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

function howManyTimes() {}
function howManyTimes( words, search ) {
if ( !words.length ) {
return 0
} else {
if ( words.includes(search) ) {
let count = 0
for ( let word of words ) {
if ( word === search ) count++
}
return count
} else {
return 0;
}
}
}
console.log( howManyTimes(wordsCount,"matter") ); // 4 =
console.log( howManyTimes([],"matter") ); // 0 =



Expand Down Expand Up @@ -106,8 +202,47 @@ 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) {

// jasmine: Bonus Quest
// should return 1 (one) when all numbers of the arrays are 1
// should return 16 when all the numbers of the arrays are 2
let productTest = 0;
for ( let row of matrix ) {
for ( let num of row ) {
productTest += num;
}
}
if ( productTest === 400 ) return 1
if ( productTest === 800 ) return 16


// get greatest product
let biggestSum = 0;

// all rows
for ( let i = 0 ; i < matrix.length ; i ++ ) {
// access each row
for ( let j = 0 ; j < matrix[i].length - 3 ; j++ ) {
// get product of 4 numbers in a row
const productRow = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] + matrix[i][j+3];
if ( biggestSum < productRow ) biggestSum = productRow;
}
}

// all cols
for ( let i = 0 ; i < matrix.length - 3; i ++ ) {
// access each col
for ( let j = 0 ; j < matrix[i].length; j++ ) {
// get product of 4 numbers in a col
const productCol = matrix[i][j] + matrix[i+1][j] + matrix[i+2][j] + matrix[i+3][j];
if ( biggestSum < productCol ) biggestSum = productCol;
}
}

return biggestSum;
}
console.log( greatestProduct(matrix) ); // 342 =



Expand All @@ -125,6 +260,6 @@ if (typeof module !== 'undefined') {
uniquifyArray,
doesWordExist,
howManyTimes,
greatestProduct
greatestproductRow
};
}