From 8eb47dcba6a3043e4d0b44855ea9ea2ca0eb24bd Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 Mar 2023 19:28:55 +0000 Subject: [PATCH 1/2] 1-exercises/ fiexd --- 1-exercises/A-array-find/exercise.js | 6 ++++-- 1-exercises/B-array-some/exercise.js | 12 +++++++++++- 1-exercises/C-array-every/exercise.js | 6 +++++- 1-exercises/D-array-filter/exercise.js | 10 ++++++++-- 1-exercises/E-array-map/exercise.js | 7 +++++-- 1-exercises/F-array-forEach/exercise.js | 20 +++++++++++++++++--- 1-exercises/G-array-methods/exercise.js | 6 +++--- 1-exercises/G-array-methods/exercise2.js | 6 +++--- 1-exercises/H-array-methods-2/exercise.js | 8 ++++---- 1-exercises/H-array-methods-2/exercise2.js | 12 ++++++++---- 1-exercises/H-array-methods-2/exercise3.js | 6 +++--- 1-exercises/I-string-replace/exercise.js | 11 +++++++---- 1-exercises/J-string-substring/exercise.js | 10 +++++----- 1-exercises/J-string-substring/exercise2.js | 8 +++++--- 1-exercises/J-string-substring/exercise3.js | 8 ++++---- 15 files changed, 92 insertions(+), 44 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..e740ad3f 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -16,9 +16,11 @@ let names = [ "Karim", "Ahmed", ]; +function findLongNameThatStartsWithA(name){ + return name.length >7 ; +} -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); - +let longNameThatStartsWithA = names.find(findLongNameThatStartsWithA); console.log(longNameThatStartsWithA); /* EXPECTED OUTPUT */ diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..f27c598a 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -7,14 +7,23 @@ */ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; +function checkNull (){ + console.error('pairsByIndex have null value'); + return process.exit(1); +} +let containNull = pairsByIndex.some(checkNull) +console.log(containNull); + + + + // } // 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); let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; - let pairs = pairsByIndex.map(function (indexes) { let student = students[indexes[0]]; let mentor = mentors[indexes[1]]; @@ -22,3 +31,4 @@ let pairs = pairsByIndex.map(function (indexes) { }); console.log(pairs); + diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..51f12b0f 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,7 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +let groupIsOnlyStudents = group.every(checkIfAllStudent) ; // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); @@ -16,3 +16,7 @@ if (groupIsOnlyStudents) { /* EXPECTED RESULT */ // The group does not contain only students +function checkIfAllStudent(name) { + return students.includes(name) ; +} + diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..4447722e 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,9 +8,11 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +let pairsByIndex = pairsByIndexRaw.filter(function isPairs(name){ + return Array.isArray(name) && name.length === 2; +}); // Complete this statement -let students = ["Islam", "Lesley", "Harun", "Rukmini"]; +let students = ["Islam", "Lesley", "Haroun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; let pairs = pairsByIndex.map(function (indexes) { @@ -21,6 +23,10 @@ let pairs = pairsByIndex.map(function (indexes) { console.log(pairs); + + + + /* EXPECTED RESULT [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..c6965b79 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -2,10 +2,13 @@ // Write multiple solutions using different syntax (as shown in the README) let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +let numbersMultipliedByOneHundred = numbers .map(function multiple(number) { + return number * 100; +}); +console.log(numbersMultipliedByOneHundred); + -let numbersMultipliedByOneHundred; // complete this statement -console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..0f499a1e 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -1,13 +1,27 @@ -/* +/* Using .forEach() print the numbers 1 to 15, with some exceptions: - - For multiples of 3 print “Fizz” instead of the number - - For the multiples of 5 print “Buzz”. + - For multiples of 3 print “Fizz” instead of the number + - For the multiples of 5 print “Buzz”. - For numbers which are multiples of both 3 and 5 print “FizzBuzz” An array with numbers 1-15 has been provided. */ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach(num => { + if (num % 3 === 0 && num % 5 === 0) { + console.log("FizzBuzz"); + } else if (num % 3 === 0) { + console.log("Fizz"); + } else if (num % 5 === 0) { + console.log("Buzz"); + } else { + console.log(num); + } +}); + + + /* EXPECTED OUTPUT */ diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..d3c2cb72 100644 --- a/1-exercises/G-array-methods/exercise.js +++ b/1-exercises/G-array-methods/exercise.js @@ -4,15 +4,15 @@ */ let numbers = [3, 2, 1]; -let sortedNumbers; // complete this statement +let sortedNumbers = numbers.sort(); -/* +/* DO NOT EDIT BELOW THIS LINE --------------------------- */ console.log(sortedNumbers); -/* +/* EXPECTED RESULT --------------- [1, 2, 3] diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js index 4c68c3a6..212cff01 100644 --- a/1-exercises/G-array-methods/exercise2.js +++ b/1-exercises/G-array-methods/exercise2.js @@ -7,15 +7,15 @@ let mentors = ["Daniel", "Irina", "Rares"]; let students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -let everyone; // complete this statement +let everyone = mentors.concat(students); -/* +/* DO NOT EDIT BELOW THIS LINE --------------------------- */ console.log(everyone); -/* +/* EXPECTED RESULT --------------- ["Daniel", "Irina", "Rares", "Rukmini", "Abdul", "Austine", "Swathi"] diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..86a0dfa2 100644 --- a/1-exercises/H-array-methods-2/exercise.js +++ b/1-exercises/H-array-methods-2/exercise.js @@ -15,17 +15,17 @@ let everyone = [ "Swathi", ]; -let firstFive; // complete this statement -let lastFive; // complete this statement +let firstFive = everyone.slice(0, 5); +let lastFive = everyone.slice(2); -/* +/* DO NOT EDIT BELOW THIS LINE --------------------------- */ console.log(firstFive); console.log(lastFive); -/* +/* EXPECTED RESULT --------------- ["Daniel", "Irina", "Rares", "Rukmini", "Abdul"] diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js index 14bb4318..4562df29 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -1,15 +1,19 @@ /* Array methods - .join() ------------------------- - Complete the capitalise function + Complete the capitalise function It should return a string with the first letter in uppercase For example, capitailise("hello") should return "Hello" Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { +let str1 = str.split(''); +str1[0] = str1[0].toUpperCase() + str1[0].slice(1); +return str1.join(''); +} -/* +/* DO NOT EDIT BELOW THIS LINE --------------------------- */ let name = "daniel"; @@ -17,7 +21,7 @@ let name = "daniel"; console.log(capitalise(name)); console.log(capitalise("hello")); -/* +/* EXPECTED RESULT --------------- Daniel diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js index c8e079e4..53bab279 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,17 +7,17 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); } -/* +/* DO NOT EDIT BELOW THIS LINE --------------------------- */ console.log(isInUK("France")); console.log(isInUK("Republic of Ireland")); console.log(isInUK("England")); -/* +/* EXPECTED RESULT --------------- false diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..06152172 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -1,11 +1,11 @@ -/* +/* You are given a sentence contains a story. - Current it says + Current it says "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day." - Change the story using .replace() so that it says + Change the story using .replace() so that it says "I like cats. One night I went to the park and I saw 100000 cats. It was a brilliant night." */ @@ -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..f5fa8485 100644 --- a/1-exercises/J-string-substring/exercise.js +++ b/1-exercises/J-string-substring/exercise.js @@ -1,17 +1,17 @@ -/* +/* You are given an statement - You should remove the word "and dogs" by using substring + You should remove the word "and dogs" by using substring */ let statement = "I like programming and dogs"; -statement = statement.substring(); +statement = statement.substring(0,19); console.log(statement); -/* EXPECTED OUTPUT +/* EXPECTED OUTPUT "I like programming" - + */ diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..c8ed6160 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -1,4 +1,4 @@ -/* +/* You are given an array with a list of names. You should log only the peoples first names. @@ -20,16 +20,18 @@ names[2] = names[2].substring(); names[3] = names[3].substring(); names[4] = names[4].substring(); + names.forEach((name) => { console.log(name); + }); -/* EXPECTED OUTPUT +/* EXPECTED OUTPUT "Tamzin" "Jessica" "Tony" "Shae" "Arron" - + */ diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js index 14f77417..8066ec80 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -1,4 +1,4 @@ -/* +/* You are given an statement You should remove the word "not" by using .substring() and log the result. @@ -8,12 +8,12 @@ let statement = "I do not like programming"; -let result = ""; +let result = statement.substring(0, 5) + statement.substring(8); console.log(result); -/* EXPECTED OUTPUT +/* EXPECTED OUTPUT "I do like programming" - + */ From a6ff238486ba3f7d559b8ca1f1a9bd4c4f0c6d5e Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 Mar 2023 19:31:56 +0000 Subject: [PATCH 2/2] 2-mandatory/ --- 2-mandatory/1-create-functions.js | 24 +++++++++++++++------- 2-mandatory/2-oxygen-levels.js | 10 ++++++++- 2-mandatory/3-bush-berries.js | 25 +++++++++++++--------- 2-mandatory/4-space-colonies.js | 7 +++++-- 2-mandatory/5-eligible-students.js | 10 ++++++++- 2-mandatory/6-journey-planner.js | 32 ++++++++++++++++++++--------- 2-mandatory/7-lane-names.js | 10 ++++++--- 2-mandatory/8-password-validator.js | 30 +++++++++++++++++++++++---- 8 files changed, 110 insertions(+), 38 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..43a25e0e 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(letters) { + return letters.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(letters) { + return letters.slice().sort(); } /* @@ -24,16 +26,17 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { -} - +function tidyUpString(names) { + return names.map((name) => name.trim().replace(/\//g, '').toLowerCase() +)}; /* Write a function that: - Takes an array and an index as input. - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { +function remove(element, index ) { + return element.slice(0, index).concat(element.slice(index + 1)); } /* @@ -44,9 +47,16 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(numbers) { + return numbers.map((number) => { + if (number > 100) { + number = 100; + } + return (Math.round(number * 100) / 100) + '%'; + }); } + /* ======= TESTS - DO NOT MODIFY ===== */ test("first5 function works for more than five elements", () => { diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..bc209fde 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,15 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} + +function findSafeOxygenLevel(levels) { + +} + + + + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..46457d7f 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -9,20 +9,25 @@ Create a function which checks if the bush has ALL PINK berries and is safe for the astronauts to eat from the bush. Use the tests to confirm which message to return - + This exercise can be solved in a few different ways. One way might include the array methods - .some() and .every(). - - The .some() method tests to see if some of the values (at least 1) in an array - match what you're looking for and returns true or false. - - The .every() method will only return true if all values match watch you're looking for. - + .some() and .every(). + + The .some() method tests to see if some of the values (at least 1) in an array + match what you're looking for and returns true or false. + + The .every() method will only return true if all values match watch you're looking for. + Let's first look at an example that will teach you how to use these methods. */ -function isBushSafe(berryArray) { - //Write your code here +function isBushSafe(berries) { + let nonPinkBerriesPresent = berries.some((berry) => berry !== "pink"); + if (nonPinkBerriesPresent) { + return "Toxic! Leave bush alone!"; + } else { + return "Bush is safe to eat from"; + } } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..2a3ba440 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -12,10 +12,13 @@ Input: Is an array Output: Is an array Logic: Only strings that start with A, and finish with family - + */ -function getSettlers() {} + +function getSettlers(names) { + return names.filter(name => name.startsWith('A') && name.endsWith('family')); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..1354b45a 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(names) { + let students = []; + for(let i = 0; i= 8){ + students.push(names[i][0]); +} + } + return students; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..4dd54914 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -19,12 +19,12 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; - //edit code below - if (stringText) { - return stringText; - } else { + let index = stringText.search(magicWord); + if (index === -1) { return "Not found"; - } + } else { + return index; + } } /* @@ -64,8 +64,10 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} - +function getTransportModes(tarnAndLoc) { +let transMode = tarnAndLoc.slice(1); +return transMode; +} /* Implement the function isAccessibleByTransportMode that @@ -81,8 +83,10 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transport, transMode) { + return transport.includes(transMode); +} /* Implement the function getLocationName that @@ -92,8 +96,10 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locationName) { +return locationName[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,9 +128,15 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { - // Implement the function body + + let accessibleLocations = locations.filter(location => location.includes(transportMode)).map(getLocationName); + return accessibleLocations; } + + + + /* ======= 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..6ff06be5 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -1,13 +1,17 @@ /* You are given a list of some London street names. - + Write a function that will return all street names which contain 'Lane' in their name. HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} - +function getLanes(lane) { + return lane.filter(allStreetName); +} +function allStreetName(name){ + return name.indexOf('Lane')>=0 ; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("getLanes function works", () => { diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..05dc2a92 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -1,7 +1,7 @@ -/* +/* Password Validation -Write a program that should check if each password in an array +Write a program that should check if each password in an array contains a valid password (see below for password criteria) and return a new array with true or false booleans for whether that password was valid or not. @@ -11,7 +11,7 @@ To be valid, a password must: - Have at least one English lowercase letter (a-z) - Have at least one number (0-9) - Have at least one non-alphanumeric symbol ("!", "#", "$", "%", ".", "*", "&") -- Must not be any previous password in the passwords array. +- Must not be any previous password in the passwords array. We have supplied functions which will help you with some of these checks. @@ -23,7 +23,29 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords){ + let validPass= []; + let newPass =[]; + for(let i=0;i= 5 && + containsUppercaseLetter(passwords[i]) && + containsLowercaseLetter(passwords[i]) && + containsNumber(passwords[i]) && + containsSymbol(passwords[i]) + ) { + newPass = passwords.slice(0,i) + validPass.push(newPass.some(x => x == passwords[i]) ? false : true); + }else{ + validPass.push(false); + } +} +return validPass; + } + + + + // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) {