diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..3dea97ec 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -17,9 +17,12 @@ let names = [ "Ahmed", ]; -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +function findLongNameThatStartsWithA(array) { + const findedWords = array.find((word) => word.length > 7 && word[0] === "A"); + return findedWords; +} -console.log(longNameThatStartsWithA); +console.log(findLongNameThatStartsWithA(names)); /* EXPECTED OUTPUT */ // "Alexandra" diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..4529c7ac 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -12,6 +12,10 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; // https://nodejs.org/api/process.html#process_process_exit_code // process.exit(1); +if (pairsByIndex.some((number) => number === null)) { + process.exit(1); +} + let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..ef7441de 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,10 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +function notOnlyStudent(group) { + return group.includes(students); +} +let groupIsOnlyStudents = group.every(notOnlyStudent); // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..5c4143bc 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,13 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +function validContent(pair) { + if (pair != null && pair.length === 2) { + return true; + } +} + +let pairsByIndex = pairsByIndexRaw.filter(validContent); // Complete this statement let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; @@ -24,4 +30,4 @@ console.log(pairs); /* EXPECTED RESULT [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] -*/ \ No newline at end of file +*/ diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..0c125652 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,11 +3,15 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +function multiplication(number) { + return number * 100; +} + +let numbersMultipliedByOneHundred = numbers.map(multiplication); // complete this statement console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT [10, 20, 30, 40, 50] -*/ \ No newline at end of file +*/ diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..87eb20cc 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,20 @@ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +function printNumber(number) { + if (number % 3 == 0 && number % 5 == 0) { + console.log("FizzBuzz"); + } else if (number % 3 == 0) { + console.log("Fizz"); + } else if (number % 5 == 0) { + console.log("Buzz"); + } else { + console.log(number); + } +} + +arr.forEach(printNumber); + /* EXPECTED OUTPUT */ /* diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..10ab7bcd 100644 --- a/1-exercises/G-array-methods/exercise.js +++ b/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,8 @@ */ let numbers = [3, 2, 1]; -let sortedNumbers; // complete this statement + +let sortedNumbers = numbers.sort(); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js index 4c68c3a6..fb100649 100644 --- a/1-exercises/G-array-methods/exercise2.js +++ b/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,7 @@ let mentors = ["Daniel", "Irina", "Rares"]; let students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -let everyone; // complete this statement +let everyone = mentors.concat(students); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/README.md b/1-exercises/H-array-methods-2/README.md index 2c49b98a..ea658ae1 100644 --- a/1-exercises/H-array-methods-2/README.md +++ b/1-exercises/H-array-methods-2/README.md @@ -27,7 +27,7 @@ function isAMentor(name) { return mentors.includes(name); } -consooe.log("Is Rukmuni a mentor?"); +console.log("Is Rukmuni a mentor?"); console.log(isAMentor("Rukmini")); // logs false ``` diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..331cc4aa 100644 --- a/1-exercises/H-array-methods-2/exercise.js +++ b/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ let everyone = [ "Swathi", ]; -let firstFive; // complete this statement -let lastFive; // complete this statement +let firstFive = everyone.slice(0, 5); // complete this statement +let lastFive = everyone.slice(2, 7); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js index 14bb4318..fe1a124c 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,11 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let result = str.split(""); + result[0] = result[0].toUpperCase(); + return result.join(""); +} /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js index c8e079e4..7279417e 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); // complete this statement } /* diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..e33b13f4 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,10 @@ let story = "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."; -let result = story.replace("", ""); +let result = story.replace( + "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day.", + "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." +); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..14288304 100644 --- a/1-exercises/J-string-substring/exercise.js +++ b/1-exercises/J-string-substring/exercise.js @@ -6,7 +6,7 @@ let statement = "I like programming and dogs"; -statement = statement.substring(); +statement = statement.substring(0, 18); console.log(statement); diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..cb541c6a 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -14,11 +14,11 @@ let names = [ "Arron Graham", ]; -names[0] = names[0].substring(); -names[1] = names[1].substring(); -names[2] = names[2].substring(); -names[3] = names[3].substring(); -names[4] = names[4].substring(); +names[0] = names[0].substring(0, 6); +names[1] = names[1].substring(0, 7); +names[2] = names[2].substring(0, 4); +names[3] = names[3].substring(0, 4); +names[4] = names[4].substring(0, 5); names.forEach((name) => { console.log(name); diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js index 14f77417..dce8ba13 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -8,7 +8,7 @@ let statement = "I do not like programming"; -let result = ""; +let result = statement.substring(0, 5) + statement.substring(9, 25); console.log(result); diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..76ff5fcf 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,8 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the first five elements of the passed array. */ -function first5() { +function first5(element) { + return element.slice(0, 5); } /* @@ -11,7 +12,9 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { +function sortArray(letter) { + let element = letter.slice(); + return element.sort(); } /* @@ -24,7 +27,10 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(array) { + return array.map((newArray) => + newArray.trim().replace("/", "").toLowerCase() + ); } /* @@ -33,7 +39,8 @@ Write a function that: - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { +function remove(array, index) { + return array.slice(0, index).concat(array.slice(index + 1)); } /* @@ -44,7 +51,13 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(array) { + return array.map((number) => { + if (number > 100) { + return "100%"; + } + return `${Math.round(number * 100) / 100}%`; + }); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..0d860407 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,14 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(oxygenLevels) { + return oxygenLevels.find((element) => { + let newString = Number(element.replace("%", "")); + if (newString > 19.5 && newString < 23.5 && element.includes("%")) { + return element; + } + }); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..c1c77196 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -23,8 +23,12 @@ function isBushSafe(berryArray) { //Write your code here + if (berryArray.every((berry) => berry === "pink")) { + return "Bush is safe to eat from"; + } else { + return "Toxic! Leave bush alone!"; + } } - /* ======= TESTS - DO NOT MODIFY ===== */ test("isBushSafe finds toxic busy", () => { diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..b5380e2c 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,20 @@ */ -function getSettlers() {} +function getSettlers(voyager) { + return voyager.filter((word) => { + if (word.includes("family") && word.startsWith("A")) { + return word; + } + }); + + // const findedWords = voyager.find( + // (word) => { if (word.includes("family") && word.startsWith("A")){ + // return word; + // } + // ); + // return findedWords; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..a5bfce20 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,15 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(classAttendance) { + return classAttendance + .filter((student) => { + if (student[1] > 7) { + return student[0]; + } + }) + .map((student) => student[0]); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..e1979368 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,8 +20,8 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; //edit code below - if (stringText) { - return stringText; + if (stringText.includes(magicWord)) { + return stringText.indexOf(magicWord); } else { return "Not found"; } @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(array) { + return array.slice(1); +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,13 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(array, string) { + if (array.includes(string)) { + return true; + } else { + return false; + } +} /* Implement the function getLocationName that @@ -92,7 +100,11 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locations) { + if (locations) { + return locations[0]; + } +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -123,8 +135,11 @@ function getLocationName() {} */ function journeyPlanner(locations, transportMode) { // Implement the function body + let findLocations = locations.filter((location) => + location.includes(transportMode) + ); + return findLocations.map((location) => location[0]); } - /* ======= TESTS - DO NOT MODIFY ===== */ const string1 = "I Love coding and perfect code makes me happy"; diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..a5f0ac6c 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,10 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +function getLanes(streetName) { + let name = "Lane"; + return streetName.filter((names) => names.includes(name)); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..786cb194 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,17 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + return passwords.map( + (password, index) => + containsUppercaseLetter(password) && + containsLowercaseLetter(password) && + containsNumber(password) && + containsSymbol(password) && + password.length > 4 && + !passwords.slice(0, index).includes(password) + ); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) {