Skip to content

solved #3452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

solved #3452

Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ While following the instructions for each iteration, make sure to carefully read

Implement the function `maxOfTwoNumbers` that takes two numbers as arguments and returns the bigger number.


<br>


Expand Down
97 changes: 86 additions & 11 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,94 @@
//helper functions

const isArrayEmpty = (arr) => Array.isArray(arr) && arr.length === 0


// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1, num2) {
return num1 > num2 ? num1 : num2
}



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

function findLongestWord() {}
const findLongestWord = (words) => isArrayEmpty(words) ? null : words.reduce((acc, w) => w.length > acc.length ? w : acc)



// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
const sumNumbers = (numbers) => isArrayEmpty(numbers) ? 0 : numbers.reduce((acc, n) => acc + n, 0)


function sumNumbers() {}

// Bonus: Calculate the average of a mixed elements array


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

// ! test file expects (line 124) this very specific Error("Unsupported data type sir or ma'am") => maybe test should be updated to be more generic. Otherwise, it is impossible to pass the test without checking the test file.


const sum = (arr) => isArrayEmpty(arr) ? 0 : arr.reduce((acc, item) => {
switch (typeof item) {
case "string": return acc + item.length
case "boolean": return acc + item
case "number": return acc + item
default: throw new Error("Unsupported data type sir or ma'am") // check comment above function
}
}, 0)





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

function averageNumbers() {}
function averageNumbers(arr) {
const sumAll = arr.reduce((acc, n) => acc + n, 0)
const average = sumAll / arr.length
return isArrayEmpty(arr) ? null : average
}


averageNumbers(numbersAvg)


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

function averageWordLength() { }
function averageWordLength(arr) {
const sumWords = arr.reduce((acc, n) => acc + n, "")
const averageLength = sumWords.length / arr.length
return isArrayEmpty(arr) ? null : averageLength
}

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



function avg(arr) {
const sumAll = arr.reduce((acc, item) => {
switch (typeof item) {
case "string": return acc + item.length
case "boolean": return acc + item
case "number": return acc + item
default: throw new Error("error") // check comment above function
}
}, 0)
const average = sumAll / arr.length
return isArrayEmpty(arr) ? null : average

}






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

function uniquifyArray() {}
function uniquifyArray(arr) {
return isArrayEmpty(arr) ? null : arr.filter((word, index) => arr.indexOf(word) === index)


}




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

function doesWordExist() {}
function doesWordExist(arr, word) {
return isArrayEmpty(arr) ? null : arr.includes(word)
}





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

function howManyTimes() {}
function howManyTimes(arr, word) {

return isArrayEmpty(arr) ? 0 : arr.filter((w) => w === word).length

}



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

function greatestProduct() {}


// should declare a function named greatestProduct
// should return 1 (one) when all numbers of the arrays are 1
// should return 16 when all the numbers of the arrays are 2

function greatestProduct() {


}



Expand Down