-
Notifications
You must be signed in to change notification settings - Fork 6.7k
BER-WDFT-052021-Nelly Neumann -Solved Lab #2406
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
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,38 +1,68 @@ | ||
| // Iteration #1: Find the maximum | ||
| function maxOfTwoNumbers() {} | ||
|
|
||
|
|
||
| function maxOfTwoNumbers(number1, number2) { | ||
| if (number1 > number2) { | ||
| return number1; | ||
| } else { | ||
| return number2; | ||
| } | ||
| } | ||
|
|
||
| // Iteration #2: Find longest word | ||
| const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; | ||
|
|
||
| function findLongestWord() {} | ||
|
|
||
|
|
||
| function findLongestWord(wordarray) { | ||
| if (wordarray.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| let longestword = ''; | ||
| for (let i = 0; i < wordarray.length; i++) { | ||
| if (wordarray[i].length > longestword.length) { | ||
| longestword = wordarray[i]; | ||
| } | ||
| } | ||
| return longestword; | ||
| } | ||
|
|
||
| // Iteration #3: Calculate the sum | ||
| const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
|
|
||
| function sumNumbers() {} | ||
|
|
||
|
|
||
| function sumNumbers(numbersarray) { | ||
| let result = 0; | ||
| for (let i = 0; i < numbersarray.length; i++) { | ||
| result = result + numbersarray[i]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // Iteration #3.1 Bonus: | ||
| function sum() {} | ||
|
|
||
|
|
||
|
|
||
| // Iteration #4: Calculate the average | ||
| // Level 1: Array of numbers | ||
| const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
|
|
||
| function averageNumbers() {} | ||
|
|
||
| function averageNumbers(numbersarray) { | ||
| if (numbersarray.length === 0) { | ||
| return null; | ||
| } | ||
| let sum = sumNumbers (numbersarray) | ||
| let numberOfElements= numbersarray.length | ||
| let average = sum / numberOfElements | ||
| return average | ||
|
Comment on lines
+49
to
+52
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. Another tiny detail. Just so that you know, you could do this in one line |
||
| } | ||
|
|
||
| // Level 2: Array of strings | ||
| const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; | ||
|
|
||
| function averageWordLength() { } | ||
| function averageWordLength(wordsarray) { | ||
| let wordlist = [] | ||
| for (let i = 0; i < wordsarray.length; i++){ | ||
| wordlist.push(wordsarray[i].length) | ||
| } | ||
| let average = averageNumbers(wordlist) | ||
| return average | ||
| } | ||
|
Comment on lines
+58
to
+65
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. This works fine but it creates an extra array. Since the length is just a number we can add all the lengths in a variable instead. But the way you did it also works. |
||
|
|
||
| // Bonus - Iteration #4.1 | ||
| function avg() {} | ||
|
|
@@ -52,16 +82,25 @@ const wordsUnique = [ | |
| 'bring' | ||
| ]; | ||
|
|
||
| function uniquifyArray() {} | ||
|
|
||
| function uniquifyArray(wordsUniqueArray) { | ||
|
|
||
| } | ||
|
Comment on lines
+85
to
+87
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. Check the lab solution for this one and feel free to reach out if you have any questions. |
||
|
|
||
| // Iteration #6: Find elements | ||
| const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; | ||
|
|
||
| function doesWordExist() {} | ||
|
|
||
|
|
||
| function doesWordExist(wordsFindArray,word) { | ||
| if (wordsFindArray.length === 0) { | ||
| return null; | ||
| } | ||
| let found = false | ||
| for (let i = 0; i < wordsFindArray.length; i++){ | ||
| if (wordsFindArray [i] === word){ | ||
| found = true | ||
| } | ||
| } | ||
| return found | ||
| } | ||
|
Comment on lines
+92
to
+103
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. This works but if the target word is the first word in the array and the array has 1000 elements, it will still loop on the entire array before returning true. Instead you could directly return true in line 99 so the function ends and return true as soon as the target is found. If the for loop is finished, then this means the target was not found and you can return false. |
||
|
|
||
| // Iteration #7: Count repetition | ||
| const wordsCount = [ | ||
|
|
@@ -78,9 +117,18 @@ const wordsCount = [ | |
| 'matter' | ||
| ]; | ||
|
|
||
| function howManyTimes() {} | ||
|
|
||
|
|
||
| function howManyTimes(wordsCountArray, word) { | ||
| let result = 0 | ||
| if (wordsCountArray.length === 0) { | ||
| return 0; | ||
| } | ||
| for (let i = 0; i < wordsCountArray.length; i++){ | ||
| if ( wordsCountArray [i] === word) { | ||
| result++ | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // Iteration #8: Bonus | ||
| const matrix = [ | ||
|
|
@@ -108,9 +156,6 @@ const matrix = [ | |
|
|
||
| function greatestProduct() {} | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // The following is required to make unit tests work. | ||
| /* Environment setup. Do not modify the below code. */ | ||
| if (typeof module !== 'undefined') { | ||
|
|
||
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 could also write this as
result += numbersarray[i];but this is just a tiny detail.