From 39d7f6cd6bb22e07e291808997631dc422a53f38 Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Tue, 18 Feb 2020 11:34:16 +0100 Subject: [PATCH 1/6] first commit --- starter-code/src/functions-and-arrays.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 720d3dcf5..f94e3ee06 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,4 +1,7 @@ // Iteration #1: Find the maximum +function maxOfTwoNumbers(){ + +} // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; From 139fa5f30cdda1dfe9baff2df0c8c2ba36086aa7 Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Tue, 18 Feb 2020 12:01:42 +0100 Subject: [PATCH 2/6] second commit, second iteration --- starter-code/src/functions-and-arrays.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index f94e3ee06..04cdd8c23 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,11 +1,27 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers(){ - +function maxOfTwoNumbers(num1, num2){ + if (num1 > num2) { + return num1; + } else if (num2 > num1){ + return num2; + } } // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +function findLongestWord (array){ + var longestWord = ""; + array.forEach(function(word){ + if (word.length > longestWord.length) { + longestWord = word; + } + }); + return longestWord; +} + +console.log(findLongestWord(words)); + // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; From dd1680eacf8c25e6ecf8894bfa8bc5a89b8ea578 Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Tue, 18 Feb 2020 14:44:33 +0100 Subject: [PATCH 3/6] third commit, iteration four --- starter-code/src/functions-and-arrays.js | 40 +++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 04cdd8c23..94aba8b23 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -4,20 +4,27 @@ function maxOfTwoNumbers(num1, num2){ return num1; } else if (num2 > num1){ return num2; - } + } else + return num1; + return num2; } // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; function findLongestWord (array){ - var longestWord = ""; - array.forEach(function(word){ - if (word.length > longestWord.length) { - longestWord = word; + if (array.length === 0){ + return null; + } else if (array.length > 0) { + var longestWord = ""; + array.forEach(function(word){ + if (word.length > longestWord.length) { + longestWord = word; + return longestWord; } - }); - return longestWord; + }); + return longestWord; + } } console.log(findLongestWord(words)); @@ -26,10 +33,29 @@ console.log(findLongestWord(words)); const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +function sumNumbers (array){ + sum = 0; + for (var i = 0; i < array.length; i++){ + sum += array[i] + } + return sum +} + +console.log(sumNumbers(numbers)); + // Iteration #4: Calculate the average // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; +function averageNumbers(array){ + if (array.length === 0){ + return null; + } else { + average = sumNumbers(array) / array.length; + return average; + } +} + // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; From 93aebf421b2cc9b6a875cebfe159b35290dcf8d6 Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Tue, 18 Feb 2020 16:09:06 +0100 Subject: [PATCH 4/6] fourth commit, iteration six --- starter-code/src/functions-and-arrays.js | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 94aba8b23..7686ab219 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -59,6 +59,22 @@ function averageNumbers(array){ // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; +function averageWordLength (array){ + if (array.length === 0){ + return null; + } else { + average = array.join('').length / array.length + return average; + } +} + +// function avg (array){ +// if (array.length === 0){ +// return null; +// } else { +// } +// } + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', @@ -74,9 +90,33 @@ const wordsUnique = [ 'bring' ]; + +function uniquifyArray (array){ + if (array.length === 0){ + return null; + } else { + return array.filter((a,b) => array.indexOf(a) === b) + } +} + +console.log(uniquifyArray(words)); + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; + +function doesWordExist (array, word){ + if (array.length === 0){ + return null; + } else { + if (array.includes(word) === true){ + return true; + } else { + return false; + } + } +} + // Iteration #7: Count repetition const wordsCount = [ 'machine', From c4ef95b365e1bd1511cc64ae442f024120df9497 Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Tue, 18 Feb 2020 16:41:14 +0100 Subject: [PATCH 5/6] fifth commit, iterations done - bonus incomplete --- starter-code/src/functions-and-arrays.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 7686ab219..43c1e806e 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -132,6 +132,18 @@ const wordsCount = [ 'matter' ]; + +function howManyTimes (array, word){ + if (array.length === 0){ + return 0; + } else { + var count = 0; + array.forEach((value) => (value === word && count++) + ); + return count; + } +} + // Iteration #8: Bonus const matrix = [ From 2fe8edd0cd0f0921bf80714d2423fdb4fccb846c Mon Sep 17 00:00:00 2001 From: Yvana Wagter Date: Wed, 19 Feb 2020 17:23:47 +0100 Subject: [PATCH 6/6] sixth commit, bonus 3.1 and 4.1 done --- starter-code/src/functions-and-arrays.js | 44 +++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 43c1e806e..5d73cc596 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -27,7 +27,6 @@ function findLongestWord (array){ } } -console.log(findLongestWord(words)); // Iteration #3: Calculate the sum @@ -41,7 +40,32 @@ function sumNumbers (array){ return sum } -console.log(sumNumbers(numbers)); +function sum (array){ + if (array.length === 0){ + return 0; + } else if (array.length == 1) { + return array [i]; + } else if (array.length > 1){ + var totalSum = 0; + for (let i = 0; i < array.length; i++){ + switch (typeof(array[i])){ + case "boolean": + if (array[i]){ + totalSum++; + } + case "string": + totalSum += array[i].length; + break; + case "number": + totalSum += array[i]; + break; + default: + throw new Error(); + } + } + } + return totalSum; +} // Iteration #4: Calculate the average // Level 1: Array of numbers @@ -68,12 +92,15 @@ function averageWordLength (array){ } } -// function avg (array){ -// if (array.length === 0){ -// return null; -// } else { -// } -// } +function avg (array){ + if (array.length === 0){ + return null; + } else { + average = (sum(array) / array.length); + return average; + } +} + // Iteration #5: Unique arrays const wordsUnique = [ @@ -168,3 +195,4 @@ 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] ]; +