Skip to content

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
123 changes: 122 additions & 1 deletion src/functions-and-arrays.js
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;
}
}

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:

function maxOfTwoNumbers(n1, n2) {
  if (n1 > n2) {
    return n1;
  }
  return n2;
}


// 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;
}

Choose a reason for hiding this comment

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

this function completes but it resolves to 18miami1truebarca200lisboa810 as it's the same as the one before. We can go through this if you'd like?

// 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);
}



Choose a reason for hiding this comment

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

works when the i is defined. Just make sure to add the let on line 69

// 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));
}

Choose a reason for hiding this comment

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

the best way to attempt this one would be something like...

  • declare a variable for the total
  • create a for loop
  • Add the length of each word within the array to the total (total += wordsArr[i].length;)
  • return the average (total / wordsarr.length)

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
Expand All @@ -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;

}

Choose a reason for hiding this comment

The 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;

}

Choose a reason for hiding this comment

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

also looks good! Nice one

// Iteration #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -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 = [
Expand All @@ -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;
}