From 8ee26febaea92f9dac3cca31c044bb612390da8f Mon Sep 17 00:00:00 2001 From: Igor Montovani Date: Thu, 9 Oct 2025 20:38:17 +0200 Subject: [PATCH] Solved lab --- src/challenges.js | 159 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 128 insertions(+), 31 deletions(-) diff --git a/src/challenges.js b/src/challenges.js index 940a599..5a6421c 100644 --- a/src/challenges.js +++ b/src/challenges.js @@ -10,35 +10,76 @@ const repeatedWords = [ "matter", "truth", "disobedience", - "matter" + "matter", ]; -function howManyTimes() {} - - - +function howManyTimes(array, word) { + if (array.length === 0 || array === undefined) { + return 0; + } else { + let repeatedWordCounter = 0; + + for (let i = 0; i < array.length; i++) { + if (array[i] === word) { + repeatedWordCounter++; + } + } + return repeatedWordCounter; + } +} // Iteration 2 | Number Sequence -function createSequence() {} - - - +function createSequence(number) { + let sequenceArray = []; + + if (number === 0) { + return sequenceArray; + } else { + for (let i = 0; i <= number; i++) { + sequenceArray.push(i); + } + return sequenceArray; + } +} // Iteration 3 | Multiply for Each const numbers = [1, 2, 5, 10, 13, 50]; -function multiplyBy() {} - - - +function multiplyBy(array, multiplier) { + let newArray = [] + array.forEach((element) => { + newArray.push(element * multiplier) + + }); + return newArray + } // Iteration 4 | Filter Out const original = ["cat", "dog", "fish", "bird", "cat", "fish"]; const toRemove = ["cat", "dog"]; -function filterOut() {} - +function filterOut(originalArray, stringsToRemove) { + let newArray = [] + if(originalArray.length === 0 || originalArray === undefined){ + return null + } else { + for (i = 0; i < originalArray.length; i++) { + let found = false + + for (j = 0; j < stringsToRemove.length; j++){ + if(originalArray[i] === stringsToRemove[j]){ + found = true + break + } + } + if (!found) { + newArray.push(originalArray[i]) + } + } + return newArray + } +} // Iteration 5 | Unique Arrays @@ -53,36 +94,92 @@ const duplicateWords = [ "poison", "communion", "simple", - "bring" + "bring", ]; -function uniquifyArray() {} +function uniquifyArray(array) { + const newArray = [] + for (i=0; i