From b3e9de241f5051565e37c90b0dfb30dfab9d13ab Mon Sep 17 00:00:00 2001 From: "Mr.Demir" Date: Thu, 11 May 2023 22:46:31 +0100 Subject: [PATCH] lst --- 1-exercises/A-array-find/exercise.js | 5 +++ 1-exercises/B-array-some/exercise.js | 10 ++++- 1-exercises/D-array-filter/exercise.js | 2 +- 1-exercises/E-array-map/exercise.js | 20 +++++++++- 2-mandatory/1-create-functions.js | 24 +++++++++--- 2-mandatory/2-oxygen-levels.js | 9 ++++- 2-mandatory/3-bush-berries.js | 14 +++++-- 2-mandatory/4-space-colonies.js | 8 +++- 2-mandatory/5-eligible-students.js | 14 ++++++- 2-mandatory/6-journey-planner.js | 25 ++++++++++--- 2-mandatory/7-lane-names.js | 6 ++- 2-mandatory/8-password-validator.js | 52 ++++++++++++++++++++++++-- 12 files changed, 159 insertions(+), 30 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..0bdc094b 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -4,6 +4,11 @@ */ // write your code here +function findLongNameThatStartsWithA(names) { + return names.find(function(name) { + return name.startsWith("A") && name.length > 7; + }); +} let names = [ "Rakesh", diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..f8ca631d 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -11,7 +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){ + process.exit(1); + } + } +} let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; @@ -21,4 +27,4 @@ let pairs = pairsByIndex.map(function (indexes) { return [student, mentor]; }); -console.log(pairs); +console.log(pairs); \ No newline at end of file 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/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..b540120d 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,8 +3,26 @@ 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 diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..2e8aceb0 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(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 ===== */ @@ -142,4 +154,4 @@ test("formatPercentage function works", () => { "100%", "0.37%", ]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..caddb013 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(arr) { + + 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]; + } + } +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..f237e992 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" ); -}); +}); \ No newline at end of file diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..d833d90b 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 ===== */ @@ -43,4 +47,4 @@ test("getSettlers function works", () => { "Archer family", "A Great family", ]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..23f06e63 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 ===== */ @@ -31,4 +41,4 @@ test("getEligibleStudents function works", () => { test("getEligibleStudents function can return empty array", () => { const attendance = [["Jacob", 7]]; expect(getEligibleStudents(attendance)).toEqual([]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..407e7a24 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 ===== */ @@ -209,4 +222,4 @@ describe("journeyPlanner", () => { "Tower Bridge", ]); }); -}); +}); \ No newline at end of file diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..fd1f712f 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 ===== */ @@ -20,4 +22,4 @@ test("getLanes function works", () => { ]; expect(getLanes(streetNames)).toEqual(["Abchurch Lane", "Addle Lane"]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..de7fa68d 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", () => { @@ -69,4 +113,4 @@ test("Example 2", () => { "Pl3nty!", ]) ).toEqual([true, true, false, false, false]); -}); +}); \ No newline at end of file