From f7d10b1fc7e1939eea420654ff971a2ecb030ddb Mon Sep 17 00:00:00 2001 From: Dok Date: Thu, 11 May 2023 19:04:31 +0200 Subject: [PATCH 1/5] Iteration 1&2 done --- src/functions-and-arrays.js | 41 ++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..f5b4d7ade 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,24 +1,55 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(a, b) { + if(a < b){ + return b; + } else if(a > b){ + return a ; + }else{ + return a + } +} +maxOfTwoNumbers(8, 19); // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(words){ + + if (words.length === 0) { + return null; + } + let hugeWord = words[0]; + + words.forEach(function(word){ + + if(word.length > hugeWord.length){ + hugeWord += word + console.log(hugeWord) + } + }); + + return hugeWord +} + +findLongestWord(words) // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +let summ = 0; +function sumNumbers(number3) { + + +} -function sumNumbers() {} - +sumNumbers(numbers) // Iteration #3.1 Bonus: -function sum() {} +// function sum() {} From 8d1797f41839f28b68792d0569348f788b6b6c80 Mon Sep 17 00:00:00 2001 From: Dok Date: Thu, 11 May 2023 22:13:40 +0200 Subject: [PATCH 2/5] Add bonus 3 and 4/Add exercice 3/4/5 --- src/functions-and-arrays.js | 161 ++++++++++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 16 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index f5b4d7ade..9e8a086a5 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -17,39 +17,100 @@ const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', function findLongestWord(words){ + let hugeWord = ""; + let hugeWordLength = -1; if (words.length === 0) { return null; } - let hugeWord = words[0]; + words.forEach(function(word){ - if(word.length > hugeWord.length){ - hugeWord += word - console.log(hugeWord) + if(word.length > hugeWordLength){ + hugeWordLength = word.length; + hugeWord = word; + } }); return hugeWord } - findLongestWord(words) // Iteration #3: Calculate the sum const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; -let summ = 0; -function sumNumbers(number3) { - + +function sumNumbers(numbers) { + + let sum = 0; + numbers.forEach(function(value){ + sum += value; + }); + return sum; + } +/* + +function sumNumbers(numbers) { + +let sum = 0; + + for(let i = 0 ; i < numbers.length ; i++){ + + sum += numbers[i]; + + } + return sum } +const result = sumNumbers(numbers); +console.log(result); + +}*/ sumNumbers(numbers) // Iteration #3.1 Bonus: -// function sum() {} + +const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10]; + +function sum(words) { +let wordsLength = 0; + + if (words.length === 0) { + return 0; + } + + for(let i = 0; i < words.length ; i++){ + + if(typeof words[i] === 'number'){ + + wordsLength += words[i] + + }else if(typeof words[i] === true){ + + wordsLength++ + }else if(typeof words[i] === false){ + + continue + + } else if(typeof words[i] === 'string') { + + wordsLength += words[i].length; + + } else if(typeof words[i] === 'object'){ + + throw new Error("Unsupported data type sir or ma'am"); + } + } + + console.log(wordsLength); + + return wordsLength + } + sum(mixedArr); @@ -57,16 +118,51 @@ sumNumbers(numbers) // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + + if (numbers.length === 0) { + return null; + } + return sumNumbers(numbers) / numbers.length ; +} +const result = averageNumbers(numbersAvg); +console.log(result) // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + let wordsLength = 0; + let averageWords = 0; + + if (words.length === 0) { + return null; + } + + for(let i = 0; i < words.length ; i++){ + wordsLength += words[i].length; + } + averageWords = wordsLength / words.length + return averageWords + } + +const result4 = averageWordLength(wordsArr); +console.log(result4) + // Bonus - Iteration #4.1 -function avg() {} + +function avg(numbers) { + + if (numbers.length === 0) { + return null; + } + + return sum(numbers) / numbers.length; + +} +avg(mixedArr) // Iteration #5: Unique arrays const wordsUnique = [ @@ -83,14 +179,37 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +uniquifyArray(wordsUnique); + +function uniquifyArray(arrs){ + + + + if (arrs.length === 0) { + return null; + } + + let newArrs = []; + let i = 0; + arrs.forEach(Element => { + if(!arrs.indexOf(Element) == i) + newArrs.unshift(Element) + i++ + }); + return newArrs +} // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist() { + if (numbers.length === 0) { + return null; + } + +} @@ -109,7 +228,12 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes() { + if (numbers.length === 0) { + return 0; + } + +} @@ -137,7 +261,12 @@ const matrix = [ [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48] ]; -function greatestProduct() {} +function greatestProduct() { + if (numbers.length === 0) { + return null; + } + +} From 7ffdeb6c75de9b9a80376392273f374e57458dd4 Mon Sep 17 00:00:00 2001 From: Dok Date: Fri, 12 May 2023 01:30:25 +0200 Subject: [PATCH 3/5] Add 6/7/correction of 5 --- src/functions-and-arrays.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 9e8a086a5..235aa5f1d 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -179,7 +179,7 @@ const wordsUnique = [ 'bring' ]; -uniquifyArray(wordsUnique); +uniquifyArray(wordsUnique, 'playground'); function uniquifyArray(arrs){ @@ -204,13 +204,21 @@ function uniquifyArray(arrs){ // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() { - if (numbers.length === 0) { +function doesWordExist(wordsUse, wordSearch) { + if (wordsUse.length === 0) { return null; } + + for(let i = 0 ; i < wordsUse.length ; i++){ + if(wordsUse[i] === wordSearch){ + return true; + } +} + return false; } +doesWordExist(wordsFind, 'starting'); // Iteration #7: Count repetition @@ -228,13 +236,22 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() { - if (numbers.length === 0) { +function howManyTimes(wordsUse, wordSearch) { + if (wordsUse.length === 0) { return 0; } -} + let count = 0; + for (let i = 0; i < wordsUse.length; i++) { + if (wordsUse[i] === wordSearch) { + count++; + } + } + + return count; +} +howManyTimes(wordsCount, 'matter'); // Iteration #8: Bonus From 7a0515086a5ad6c4185bfd8aba561704ec860a95 Mon Sep 17 00:00:00 2001 From: Dok Date: Fri, 12 May 2023 08:47:38 +0200 Subject: [PATCH 4/5] correction of Bonus 3/4 result but i can't resolve the result of Iteration 5 --- src/functions-and-arrays.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 235aa5f1d..dd4f5575e 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -82,20 +82,14 @@ let wordsLength = 0; if (words.length === 0) { return 0; } + let bool = true; for(let i = 0; i < words.length ; i++){ - if(typeof words[i] === 'number'){ + if(typeof words[i] === 'number' || typeof words[i] === 'boolean'){ wordsLength += words[i] - }else if(typeof words[i] === true){ - - wordsLength++ - }else if(typeof words[i] === false){ - - continue - } else if(typeof words[i] === 'string') { wordsLength += words[i].length; @@ -190,13 +184,16 @@ function uniquifyArray(arrs){ } let newArrs = []; - let i = 0; - arrs.forEach(Element => { - if(!arrs.indexOf(Element) == i) - newArrs.unshift(Element) - i++ - }); - + let i = 0 + for(let element of arrs){ + if(!arrs.includes(element) == i){ + newArrs.unshift(element); + i++ + + }else if (newArrs.includes(element)){ + continue + }; +} return newArrs } @@ -212,9 +209,10 @@ function doesWordExist(wordsUse, wordSearch) { for(let i = 0 ; i < wordsUse.length ; i++){ if(wordsUse[i] === wordSearch){ + return true; } -} + } return false; } From 6f8630e22abd51d7825392ae44035bbaa568b593 Mon Sep 17 00:00:00 2001 From: Dok Date: Fri, 12 May 2023 08:57:23 +0200 Subject: [PATCH 5/5] add in green 1 more line of iteration 5 but i can't find how delete the double if i find it --- src/functions-and-arrays.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index dd4f5575e..83ed61fa1 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -192,7 +192,10 @@ function uniquifyArray(arrs){ }else if (newArrs.includes(element)){ continue - }; + + } else if (arrs.includes(element)){ + return arrs + } } return newArrs }