-
Notifications
You must be signed in to change notification settings - Fork 6.7k
BER-FTWD-Phillip #1543
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
BER-FTWD-Phillip #1543
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,103 @@ | ||
// Iteration #1: Find the maximum | ||
|
||
function maxOfTwoNumbers(a,b){ | ||
if(a > b){ | ||
return a; | ||
} else if (a < b){ | ||
return b; | ||
} else { | ||
return a; | ||
} | ||
} | ||
|
||
// Iteration #2: Find longest word | ||
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; | ||
|
||
function findLongestWord(arr) { | ||
if(arr.length == 0){ | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you return here, you dont need to have an else statement as you are returning here (meaning it will go out of the function and not execute the rest of the function anyways) |
||
} else if(arr.length == 1) { | ||
return arr[0]; | ||
} else { | ||
const lengthArr = arr.map(item => item.length); | ||
const longestWord = arr[lengthArr.indexOf(Math.max(...lengthArr))]; | ||
return longestWord; | ||
} | ||
} | ||
|
||
// Iteration #3: Calculate the sum | ||
|
||
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
|
||
function sumNumbers(arr){ | ||
let sum1 = 0; | ||
|
||
for(let i=0; i < arr.length; i++){ | ||
sum1 += arr[i]; | ||
} | ||
|
||
return sum1; | ||
} | ||
|
||
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; | ||
|
||
function sum(mixArr){ | ||
let sum2 = 0; | ||
|
||
for(let j=0; j < mixArr.length; j++) { | ||
if(typeof mixArr[j] == "number") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually make and or statement here: since a number + a boolean results in: 5 + true === 6, and 5 + false === 5 |
||
sum2 += mixArr[j]; | ||
} else if(typeof mixArr[j] == "string") { | ||
sum2 += mixArr[j].length; | ||
} else if(typeof mixArr[j] == "boolean") { | ||
if(mixArr[j] == true) { | ||
sum2 += 1; | ||
} | ||
} else { | ||
throw new Error("Unsupported data type sir or ma'am"); | ||
} | ||
} | ||
|
||
return sum2; | ||
} | ||
|
||
// Iteration #4: Calculate the average | ||
// Level 1: Array of numbers | ||
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
|
||
function averageNumbers (numArr){ | ||
if(numArr.length == 0){ | ||
return null; | ||
} | ||
|
||
return sumNumbers(numArr)/numArr.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to see you are re-using your functions! |
||
} | ||
|
||
// Level 2: Array of strings | ||
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; | ||
|
||
function averageWordLength(strArr){ | ||
let sum3 = 0; | ||
|
||
if(strArr.length == 0){ | ||
return null; | ||
} | ||
|
||
for(let k=0; k < strArr.length; k++){ | ||
sum3 += strArr[k].length; | ||
} | ||
|
||
return sum3/strArr.length; | ||
} | ||
|
||
function avg(arr){ | ||
if(arr.length == 0){ | ||
return null; | ||
} | ||
|
||
return parseFloat((sum(arr)/arr.length).toFixed(2)); | ||
} | ||
|
||
// Iteration #5: Unique arrays | ||
const wordsUnique = [ | ||
'crab', | ||
|
@@ -29,9 +113,29 @@ const wordsUnique = [ | |
'bring' | ||
]; | ||
|
||
function uniquifyArray (wordsArr){ | ||
if(wordsArr.length == 0){ | ||
return null; | ||
} | ||
|
||
return Array.from(new Set(wordsArr)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever;) |
||
} | ||
|
||
// Iteration #6: Find elements | ||
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; | ||
|
||
function doesWordExist (wordsArr, searchterm){ | ||
if(wordsArr.length == 0){ | ||
return null; | ||
} | ||
|
||
if(wordsArr.find(word => word == searchterm) !== undefined){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I THINK you can instead write: this will either return true or false depending if the condition is met or not |
||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// Iteration #7: Count repetition | ||
const wordsCount = [ | ||
'machine', | ||
|
@@ -47,6 +151,18 @@ const wordsCount = [ | |
'matter' | ||
]; | ||
|
||
function howManyTimes(wordsArr, searchterm){ | ||
let sum5 = 0; | ||
|
||
wordsArr.forEach(function(word){ | ||
if(word == searchterm){ | ||
sum5 += 1; | ||
} | ||
}); | ||
|
||
return sum5; | ||
} | ||
|
||
// Iteration #8: Bonus | ||
|
||
const matrix = [ | ||
|
@@ -71,3 +187,7 @@ const matrix = [ | |
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], | ||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] | ||
]; | ||
|
||
function greatestProduct(matrix){ | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can rather write: if(a >= b) {return a}
This way you dont need to make the else statement:)