-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Simon Ertel [WDFT June 2020 Berlin] #1677
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"liveServer.settings.port": 5501 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,95 @@ | ||
// Iteration #1: Find the maximum | ||
function maxOfTwoNumbers(a, b) { | ||
if (a > b) { | ||
return a; | ||
} | ||
if (b > a) { | ||
return b; | ||
} | ||
if (a === b) { | ||
return a || b; | ||
} | ||
} | ||
|
||
// Iteration #2: Find longest word | ||
//Iteration #2: Find longest word | ||
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; | ||
|
||
function findLongestWord(words) { | ||
let longestWord = ""; | ||
if (words.length === 0) return null; | ||
for (let i = 0; i < words.length; i++) { | ||
if (words[i].length > longestWord.length) { | ||
longestWord = words[i]; | ||
} | ||
} | ||
return longestWord; | ||
} | ||
|
||
|
||
|
||
// Iteration #3: Calculate the sum | ||
|
||
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
|
||
function sumNumbers(arr) { | ||
let sum = 0;// has to be intialized outside the for Loop | ||
for (let i = 0; i < arr.length; i++) { | ||
sum += arr[i]; | ||
} | ||
return sum;// also outside the for loop otherwise it just iterates trough 6 | ||
} | ||
|
||
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; | ||
|
||
|
||
function sum(array) { | ||
let sum = 0; | ||
let type; | ||
for (let element of array) { | ||
type = typeof element; | ||
if (type === 'object') throw new Error("Unsupported data type sir or ma'am"); | ||
if (type === 'string') sum += element.length; | ||
else sum += element; | ||
} | ||
return sum; | ||
} | ||
|
||
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 function completes but it resolves to |
||
// Iteration #4: Calculate the average | ||
// Level 1: Array of numbers | ||
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
|
||
function averageNumbers(arr) { | ||
if(arr.length === 0) return null; | ||
let sum = 0; | ||
for (let i = 0; i < arr.length; i++) { | ||
sum += arr[i]; | ||
} | ||
return (sum / arr.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. works when the |
||
// Level 2: Array of strings | ||
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; | ||
|
||
function averageWordLength(words) { | ||
if(words.length === 0) return null; | ||
let sum = 0; | ||
for (let i = 0; i < words.length; i++) { | ||
sum += words[i].length; | ||
} | ||
return sum / words.length; | ||
} | ||
|
||
//Bonus - Iteration #4.1: A generic avg() function | ||
|
||
|
||
|
||
function avg(array) { | ||
if (array.length === 0) return null; | ||
return Number((sum(array) / array.length).toFixed(2)); | ||
} | ||
|
||
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. the best way to attempt this one would be something like...
|
||
// Iteration #5: Unique arrays | ||
const wordsUnique = [ | ||
'crab', | ||
|
@@ -29,9 +105,31 @@ const wordsUnique = [ | |
'bring' | ||
]; | ||
|
||
function uniquifyArray(arr) { | ||
if (arr.length === 0) return null; | ||
let unique = []; | ||
for (let el of arr) | ||
if (unique.indexOf(el) === -1) { | ||
unique.push(el) | ||
} | ||
return unique; | ||
|
||
} | ||
|
||
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. nicely done! |
||
// Iteration #6: Find elements | ||
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; | ||
|
||
function doesWordExist(arr, word) { | ||
if(arr.length === 0) return null; | ||
for (let el of arr) { | ||
if (el === word) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
|
||
} | ||
|
||
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. also looks good! Nice one |
||
// Iteration #7: Count repetition | ||
const wordsCount = [ | ||
'machine', | ||
|
@@ -47,6 +145,16 @@ const wordsCount = [ | |
'matter' | ||
]; | ||
|
||
function howManyTimes(haystack, needle) { | ||
let count = 0; | ||
for (let word of haystack) { | ||
if (word === needle) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
// Iteration #8: Bonus | ||
|
||
const matrix = [ | ||
|
@@ -71,3 +179,16 @@ 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) { | ||
let result = 0; | ||
let horizontal = 0; | ||
let vertical = 0; | ||
for (let j = 0; j < 20; j++) { | ||
for (let i = 0; i < 17; i++) { | ||
horizontal = matrix[j][i] * matrix[j][i + 1] * matrix[j][i + 2] * matrix[j][i + 3]; | ||
vertical = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j]; | ||
result = Math.max(horizontal, vertical, result); | ||
} | ||
} | ||
return result; | ||
} |
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.
sweet. Could refactor it slightly so it's a bit shorter: