From 91e96a3bbe757fc0a6801a1b4cce9679141f4aca Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Tue, 7 Mar 2023 19:43:18 +0000 Subject: [PATCH 01/11] Exercise A passes --- 1-exercises/A-array-find/exercise.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..1a33a404 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -4,7 +4,9 @@ */ // write your code here - +function findLongNameThatStartsWithA(names) { + return names.find((name) => name.startsWith("A") && name.length > 7); +} let names = [ "Rakesh", "Antonio", From bbcbc65323c7c0590001afec7363bfd606286c67 Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Thu, 9 Mar 2023 14:12:22 +0000 Subject: [PATCH 02/11] Exercise - D --- 1-exercises/D-array-filter/exercise.js | 2 +- 2-mandatory/1-create-functions.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..9151bb88 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,7 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +let pairsByIndex = pairsByIndexRaw.filter(pair => Array.isArray(pair) && pair.length == 2); // Complete this statement let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..a7f53aa0 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,7 @@ 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(arr) { } /* From ef206c5436ebe0fed6c293498260952c17d7da3f Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Thu, 9 Mar 2023 15:24:54 +0000 Subject: [PATCH 03/11] Exercise - E --- 1-exercises/E-array-map/exercise.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..decc0677 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,11 +3,29 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +//let numbersMultipliedByOneHundred = numbers.map(number => number * 100); // complete this statement +// another solution +/* function hundredTimes(number){ + return number * 100 +} +let numbersMultipliedByOneHundred = numbers.map(hundredTimes); */ + +// another solution +//let numbersMultipliedByOneHundred = numbers.map(function hundredTimes(number){return number * 100}); + +// another solution +//let numbersMultipliedByOneHundred = numbers.map(function (number){return number * 100}); + +//another solution +let numbersMultipliedByOneHundred = numbers.map((number) => { + return number * 100; +}); + +// do not edit below console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT [10, 20, 30, 40, 50] -*/ \ No newline at end of file +*/ From 333a64a8873e05c61169f9a940f6c697969cad13 Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Thu, 9 Mar 2023 15:27:06 +0000 Subject: [PATCH 04/11] Exercise - A used {return...} instead only statement --- 1-exercises/A-array-find/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 1a33a404..966df8f0 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -5,7 +5,7 @@ // write your code here function findLongNameThatStartsWithA(names) { - return names.find((name) => name.startsWith("A") && name.length > 7); + return names.find((name) => {return name.startsWith("A") && name.length > 7}); } let names = [ "Rakesh", From bdfa651bc8c29fa5feb56b6d1ff9eb879f32d9db Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:19:09 +0000 Subject: [PATCH 05/11] Mandatory - 2 --- 1-exercises/A-array-find/exercise.js | 5 ++++- 1-exercises/B-array-some/exercise.js | 7 +++++++ 2-mandatory/1-create-functions.js | 20 ++++++++++++++++---- 2-mandatory/2-oxygen-levels.js | 17 ++++++++++++++--- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 966df8f0..a247125f 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -5,8 +5,11 @@ // write your code here function findLongNameThatStartsWithA(names) { - return names.find((name) => {return name.startsWith("A") && name.length > 7}); + return names.find((name) => { + return name.startsWith("A") && name.length > 7; + }); } + let names = [ "Rakesh", "Antonio", diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..7a3d4462 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -11,6 +11,13 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; // If there is a null value in the array exit the program with the error code // https://nodejs.org/api/process.html#process_process_exit_code // process.exit(1); +function checkNull(arr){ + for (value of arr){ + if (value == null){ + exit(1); + } + } +} let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index a7f53aa0..f3c073d7 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -4,6 +4,7 @@ Write a function that: - Returns a new array containing the first five elements of the passed array. */ function first5(arr) { + return arr.filter((element, index) => index < 5); } /* @@ -11,7 +12,8 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { +function sortArray(arr) { + return arr.sort(); } /* @@ -24,7 +26,8 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(arr) { + return arr.map((str) => str.trim().replace(/\//g, "").toLowerCase()); } /* @@ -33,7 +36,9 @@ Write a function that: - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { +function remove(arr, index) { + const newArr = arr.filter((v, i) => index == i); + return newArr; } /* @@ -44,7 +49,14 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(arr) { + const formattedPercentages = numbers.map((number) => { + if (number > 100) { + number = 100; + } + return (Math.round(number * 100) / 100).toFixed(2) + "%"; + }); + return formattedPercentages; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..678f0f83 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,11 +11,22 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(arr) { + //result = ""; + for (let i = 0; i < arr.length; i++) { + if ((arr[i].split("%"))[0] < 23.5 && (arr[i].split("%"))[0] > 19.5 && arr[i].includes("%")) { + return arr[i]; + } + } +} + +// console.log( +// findSafeOxygenLevel(["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]) +// ); /* ======= TESTS - DO NOT MODIFY ===== */ -test("findSafeOxygenLevel function works - case 1", () => { + test("findSafeOxygenLevel function works - case 1", () => { expect( findSafeOxygenLevel(["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]) ).toEqual("19.9%"); @@ -35,4 +46,4 @@ test("findSafeOxygenLevel function filters out invalid percentages", () => { test("findSafeOxygenLevel function returns undefined if no valid planets found", () => { expect(findSafeOxygenLevel(["50"])).toBeUndefined(); -}); +}); From bc3279ae90600498d63d2790000dedb207fa697b Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 12:30:35 +0000 Subject: [PATCH 06/11] Mandatory - 3 --- 2-mandatory/3-bush-berries.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..6550f6a3 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -23,11 +23,19 @@ function isBushSafe(berryArray) { //Write your code here -} + if (berryArray.every(v => v == "pink")) { + return "Bush is safe to eat from"; + } else { + return "Toxic! Leave bush alone!"; + } + +} + +console.log(isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"])); /* ======= TESTS - DO NOT MODIFY ===== */ -test("isBushSafe finds toxic busy", () => { + test("isBushSafe finds toxic busy", () => { expect( isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"]) ).toEqual("Toxic! Leave bush alone!"); @@ -37,4 +45,4 @@ test("isBushSafe function finds safe bush", () => { expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual( "Bush is safe to eat from" ); -}); +}); From 0deed41b00de4756801ce844dfc9f3d47cf71dc4 Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 12:36:32 +0000 Subject: [PATCH 07/11] Mandatory - 4 --- 2-mandatory/4-space-colonies.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..3bd7a4d6 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,11 @@ */ -function getSettlers() {} +function getSettlers(arr) { + return (result = arr.filter( + (v) => v.startsWith("A") && v.includes("family") + )); +} /* ======= TESTS - DO NOT MODIFY ===== */ From 6f2b4a94e7881a94f105755f561a34392d5c7039 Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 12:45:27 +0000 Subject: [PATCH 08/11] Mandatory - 5 --- 2-mandatory/5-eligible-students.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..ccc4430b 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,17 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(arr) { + let result = []; + + for (let i = 0; i < arr.length; i++) { + if (arr[i][1] > 7) { + result.push(arr[i][0]) + } + } + + return result +} /* ======= TESTS - DO NOT MODIFY ===== */ From 54abcdd4e15cca20d8aff3abf6aa40e15a68ca2a Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 13:05:05 +0000 Subject: [PATCH 09/11] Mandatory - 6 --- 2-mandatory/6-journey-planner.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..efbf991e 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(arr) { + return arr.splice(1, arr.length) +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,9 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(arr, str) { + return arr.includes(str) +} /* Implement the function getLocationName that @@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(arr) { + return arr[0] +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -123,6 +129,13 @@ function getLocationName() {} */ function journeyPlanner(locations, transportMode) { // Implement the function body + let result = []; + for (let i = 0; i < locations.length; i++) { + if (locations[i].includes(transportMode)){ + result.push(locations[i][0]) + } + } + return result } /* ======= TESTS - DO NOT MODIFY ===== */ From a0af0a4a13119010b398821ead9259da3254de9d Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 13:07:12 +0000 Subject: [PATCH 10/11] Mandatory - 7 --- 2-mandatory/7-lane-names.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..d67803cf 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,9 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +function getLanes(arr) { + return arr.filter(v => v.includes("Lane")) +} /* ======= TESTS - DO NOT MODIFY ===== */ From 099017960be169a02d0cc05ebb30a90def204247 Mon Sep 17 00:00:00 2001 From: nsaimk <103388334+nsaimk@users.noreply.github.com> Date: Sat, 25 Mar 2023 19:07:10 +0000 Subject: [PATCH 11/11] Mandatory - 8 --- 2-mandatory/8-password-validator.js | 50 +++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..f8a2187d 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,13 +23,57 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + /* let result = []; + for (let i = 0; i < passwords.length; i++) { + if ( + containsLowercaseLetter(passwords[i]) && + containsUppercaseLetter(passwords[i]) && + containsNumber(passwords[i]) && + containsSymbol(passwords[i]) && + passwords[i].length >= 5 + ) { + if (result.indexOf(passwords[i]) === -1) { + result.push(true); + } else { + result.push(false); + } + } else { + result.push(false); + } + } + return result; */ + let result = []; + let seenPasswords = []; // create a new array to keep track of seen passwords + for (let i = 0; i < passwords.length; i++) { + if ( + containsLowercaseLetter(passwords[i]) && + containsUppercaseLetter(passwords[i]) && + containsNumber(passwords[i]) && + containsSymbol(passwords[i]) && + passwords[i].length >= 5 + ) { + if (seenPasswords.indexOf(passwords[i]) === -1) { + result.push(true); + seenPasswords.push(passwords[i]); // add current password to seen passwords array + } else { + result.push(false); + } + } else { + result.push(false); + } + } + return result; +} +console.log( + validatePasswords(["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]) +); // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { return /[A-Z]/.test(string); } - +//console.log(containsUppercaseLetter("yUhbsdj")); // Returns true if string contains at least one lowercase letter. function containsLowercaseLetter(string) { return /[a-z]/.test(string); @@ -44,7 +88,7 @@ function containsNumber(string) { function containsSymbol(string) { return /[!#$%.*&]/.test(string); } - +console.log(containsSymbol); /* ======= TESTS - DO NOT MODIFY ===== */ test("Example 1", () => {