From 600cc290b80f901b57c94bee10102acef46aa531 Mon Sep 17 00:00:00 2001 From: Karen Schneider Date: Wed, 31 Jan 2024 18:37:48 +0100 Subject: [PATCH 1/2] iteracion1,2,3 --- src/functions-and-arrays.js | 38 +++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 3a7dbec41..34079e75a 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -1,21 +1,51 @@ // Iteration #1: Find the maximum -function maxOfTwoNumbers() {} +function maxOfTwoNumbers(numOne,numTwo) { + if(numOne > numTwo){ + return numOne + } else if (numOne < numTwo){ + return numTwo + } else { + return numOne || numTwo + } +} +//console.log(maxOfTwoNumbers(10,7)) // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; -function findLongestWord() {} +function findLongestWord(arrayWord) { + + let longWord = ""; + if(arrayWord === 0){ + return null; + } + for(let i of arrayWord){ + if (i.length > longWord.length ){ + longWord = i; + } + } + return longWord +} + +//console.log(findLongestWord(words)) // Iteration #3: Calculate the sum -const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8]; -function sumNumbers() {} +function sumNumbers(numbers) { + let sum = 0; + for(let i of numbers){ + sum += i + } + return sum +} +//console.log(numbers) // Iteration #3.1 Bonus: function sum() {} From e25fb7299a89db3486ad6e60ca171882fc7e2fc2 Mon Sep 17 00:00:00 2001 From: Karen Schneider Date: Thu, 1 Feb 2024 12:05:32 +0100 Subject: [PATCH 2/2] final --- src/functions-and-arrays.js | 85 ++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/functions-and-arrays.js b/src/functions-and-arrays.js index 34079e75a..becd5bc9c 100644 --- a/src/functions-and-arrays.js +++ b/src/functions-and-arrays.js @@ -19,7 +19,7 @@ function findLongestWord(arrayWord) { let longWord = ""; - if(arrayWord === 0){ + if(arrayWord.length === 0){ return null; } for(let i of arrayWord){ @@ -56,19 +56,54 @@ function sum() {} // Level 1: Array of numbers const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]; -function averageNumbers() {} +function averageNumbers(numbers) { + if (!numbers || numbers.length === 0) { + return null; + } + let sum = 0; + + for (let i of numbers){ + sum += i + } + return sum / numbers.length +} + +//console.log(averageNumbers(numbersAvg)) // Level 2: Array of strings const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']; -function averageWordLength() { } +function averageWordLength(words) { + if( !words || words.length === 0 ){ + return null + } + let totalWords = 0; + + for (let i of words){ + totalWords += i.length; + } + return totalWords / words.length + } + + //console.log(averageWordLength(wordsArr)) // Bonus - Iteration #4.1 function avg() {} // Iteration #5: Unique arrays const wordsUnique = [ + 'crab', + 'poison', + 'contagious', + 'simple', + 'bring', + 'sharp', + 'playground', + 'poison', + 'communion', + 'simple', + 'bring', 'crab', 'poison', 'contagious', @@ -82,15 +117,39 @@ const wordsUnique = [ 'bring' ]; -function uniquifyArray() {} +function uniquifyArray(wordsUnique) { + if ( wordsUnique.length === 0 ) { + return null; + } + let uniqueArray = [] + for (let i of wordsUnique) { + if (!uniqueArray.includes(i)) { + uniqueArray.push(i) + } + } + return uniqueArray +} + +//console.log(uniquifyArray(wordsUnique)) // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; -function doesWordExist() {} +function doesWordExist( wordsFind, wordSearch ) { + if (wordsFind.length === 0){ + return null; + } + + if (wordsFind.indexOf(wordSearch) !== -1){ + return true + } else { + return false + } +} +//console.log(doesWordExist(wordsFind, 'machine')) // Iteration #7: Count repetition @@ -108,8 +167,22 @@ const wordsCount = [ 'matter' ]; -function howManyTimes() {} +function howManyTimes(wordsCount, wordToSearch) { + if (wordsCount.length === 0) { + return 0; + } + + let count = 0; + + for (let i = 0; i < wordsCount.length; i++) { + if (wordsCount[i] === wordToSearch) { + count++; + } + } + return count; +} +//console.log(howManyTimes(wordsCount, 'matter')); // Iteration #8: Bonus