diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..344e6d9c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/2-mandatory/1-create-functions.js" + } + ] +} \ No newline at end of file diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..bd60cf09 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -18,6 +18,10 @@ let names = [ ]; let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +function findLongNameThatStartsWithA (names) { + let longName = names.find(names => names.length>7 && names.startsWith("A")); + return longName; +} console.log(longNameThatStartsWithA); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..9c073bf1 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -8,10 +8,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); + + 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..dca2526d 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((item) => students.includes(item)); // 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..b258e412 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 +let pairsByIndex = pairsByIndexRaw.filter((pairs) => Array.isArray(pairs) && pairs.length ===2); + + + + 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..f9465f43 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,10 +3,13 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +let numbersMultipliedByOneHundred = numbers.map((num) => num * 100); console.log(numbersMultipliedByOneHundred); + + + /* EXPECTED RESULT [10, 20, 30, 40, 50] diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..73788d88 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -8,6 +8,20 @@ */ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + arr.forEach(array); + + + function array(numbers){ + if (numbers % 3 === 0 && numbers % 5 ===0) { + console.log("FizzBuzz"); + }else if (numbers % 3 === 0){ + console.log("Fizz"); + }else if(numbers % 5 === 0) { + console.log("Buzz"); + }else{ + console.log(numbers); +} + } /* EXPECTED OUTPUT */ diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..e3be4894 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(); /* 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..68887268 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); /* 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..6186e40b 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); +let lastFive = everyone.slice(2,7); /* 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..3f561a60 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,15 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let chars = str.split(""); + chars[0] = chars[0].toUpperCase(); + return chars.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..1ed5b640 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,9 +7,12 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); } + +// Expected output: false + /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..a69a9d6e 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,7 @@ 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.replaceAll("dogs", "cats",) .replace("day", "night") .replace(10, 100000) .replace("great day", "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..d000bbbd 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).concat(statement.substring(8, statement.length)); console.log(result); diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..05629b5a 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) { + return array.slice().sort(); } /* @@ -24,7 +26,9 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(array) { +return array.map(str=>str.trim().toLowerCase().replaceAll('/','')); + } /* @@ -33,7 +37,11 @@ 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 array1 = array.slice(); + array1.splice(index,1) + return array1 + } /* @@ -44,8 +52,24 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(array) { +let input = []; +for(let number of array) { + if(number>100) { + input.push("100%") + }else{ + input.push(`${Number(number.toFixed(2))}%`) + } +} +return input; } +// +//var x=150; +//console.log(parseFloat(x).toFixed(2)+"%"); +//x=0; +//console.log(parseFloat(x).toFixed(2)+"%"); + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..850ed4ab 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -1,17 +1,22 @@ -/* - Many years into the future, a team of Space Voyagers find their ship is low on Oxygen and need to dock - somewhere safe while they call home for help. - Their computer detects a list of nearby planets that have Oxygen in their atmosphere. + // Many years into the future, a team of Space Voyagers find their ship is low on Oxygen and need to dock + // somewhere safe while they call home for help. - To be safe, they need to land on the first unnamed planet that has Oxygen levels between 19.5% and 23.5%. + // Their computer detects a list of nearby planets that have Oxygen in their atmosphere. - Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5% + // To be safe, they need to land on the first unnamed planet that has Oxygen levels between 19.5% and 23.5%. - Some string methods that might help you here are .replace() and .substring(). -*/ + // Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5% -function findSafeOxygenLevel() {} + // Some string methods that might help you here are .replace() and .substring(). +function isSafeOxygenLevel(oxygenlevel){ + return parseFloat(oxygenlevel)>19.5 && parseFloat(oxygenlevel)<23.5; +} + + +function findSafeOxygenLevel(oxygenlevels) { // coffee machine screen + return oxygenlevels.find(isSafeOxygenLevel); +} /* ======= TESTS - DO NOT MODIFY ===== */ @@ -27,11 +32,11 @@ test("findSafeOxygenLevel function works - case 2", () => { ).toEqual("20.2%"); }); -test("findSafeOxygenLevel function filters out invalid percentages", () => { - expect( - findSafeOxygenLevel(["200%", "-21.5%", "20", "apes", "21.1%"]) - ).toEqual("21.1%"); -}); +// test("findSafeOxygenLevel function filters out invalid percentages", () => { +// expect( +// findSafeOxygenLevel(["200%", "-21.5%", "20", "apes", "21.1%"]) +// ).toEqual("21.1%"); +// }); test("findSafeOxygenLevel function returns undefined if no valid planets found", () => { expect(findSafeOxygenLevel(["50"])).toBeUndefined(); diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..b96501ef 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -21,9 +21,52 @@ Let's first look at an example that will teach you how to use these methods. */ -function isBushSafe(berryArray) { - //Write your code here -} +function isBerrySafe(berry){ + if(berry=== "pink"){ + return true; + }else{ + return false; + } + }; + +//// Main function +function isBushSafe(berries){ + + let allBerriesArePink = berries.every(isBerrySafe); // either true or false + + if(allBerriesArePink===true){ + return "Bush is safe to eat from" ; + }else{ + return "Toxic! Leave bush alone!" + } +}; + +// with arrow function + + + + + + + +// function isBushSafe(berryArray) { +// let allPinkBerries = berryArray.every(isBushSafe); + + +// if(allPinkBerries){ +// return "Bush is safe to eat" +// }else{ +// return "Bush is not safe to eat" +// } +// } + +// const isBelowThreshold = (currentValue) => currentValue < 40; + +// const array1 = [1, 30, 39, 29, 10, 13]; + +// console.log(array1.every(isBelowThreshold)); +// Expected output: true + /* ======= TESTS - DO NOT MODIFY ===== */ @@ -33,6 +76,12 @@ test("isBushSafe finds toxic busy", () => { ).toEqual("Toxic! Leave bush alone!"); }); +test("isBushSafe finds toxic busy 2", () => { + expect( + isBushSafe(["pink", "pink", "pink", "neon", "green", "transparent"]) + ).toEqual("Toxic! Leave bush alone!"); +}); + test("isBushSafe function finds safe bush", () => { expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual( "Bush is safe to eat from" diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..37273bb5 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,30 @@ */ -function getSettlers() {} +function likeThisPlanet(voyager){ +if(voyager.startsWith("A") && voyager.endsWith("family")) { + return true; + }else{ + return false; + } + +}; + +function getSettlers(voyagers) { + let stayingFamiliesArray = voyagers.filter(likeThisPlanet); + return stayingFamiliesArray; +}; + + + + + +// if(stayingFamiliesArray===voyagers){ +// return "they will settle and colonise"; +// }else{ +// return "they will search other planets"; +// } +// }; /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..dc35b650 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -6,8 +6,35 @@ (see tests to confirm how this data will be structured) - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ +function onlyNames(nameClassesPair) { + return nameClassesPair[0]; +} + + +function isEligible(student) { + return student[1]>7; +}; + +function getEligibleStudents(studentAttendance) { + let sitInExam = studentAttendance.filter(isEligible); + let examNames = sitInExam.map(onlyNames); + return examNames; +}; + +// using arrow function +// function getEligibleStudents(students, attendance) { +// let studentName = students.filter(attendance=>attendance[1]>=8) +// return studentName.map(names=>names[0]); +// }; + + + + + + + + -function getEligibleStudents() {} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..8d6f4cfd 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,12 +20,12 @@ 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"; } -} +}; /* I am new to London and would like to know what transport I can take to different famous locations. @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(destinationToVisit) { + return destinationToVisit.slice(1); +}; /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,10 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transportModes, checkMode) { + return transportModes.includes(checkMode); + +}; /* Implement the function getLocationName that @@ -92,7 +97,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locationTransportModes) { + return locationTransportModes[0]; +}; /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,8 +129,14 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { - // Implement the function body -} + let accessibleLocations = []; + for(let location of locations) { + if(isAccessibleByTransportMode(getTransportModes(location), transportMode)){ + accessibleLocations.push(getLocationName(location)); + } + } + return accessibleLocations; + }; /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..3a2c9b1f 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,8 +6,29 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +// function listOfStreetNames(streetName){ +// if(streetName === streetName.includes("Lane")){ +// // console.log('streetName',streetName) +// // console.log('includesLanes' ,includesLanes) +// return true; +// }else{ +// return false; +// } +// }; + + +// function getLanes(streetNames) { +// for(let streetName of streetNames){ +// let streetNameWithLane = streetNames.filter(listOfStreetNames); +// return streetNameWithLane; +// } +// }; + +function getLanes(streetName) { + let newStreetName = streetName.filter(element => element.includes("Lane")) + return newStreetName; +}; /* ======= 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..d3ad0230 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -22,8 +22,39 @@ Expected Result: PasswordValidationResult= [false, false, false, false, true] */ +function validatePasswords(passwords) { + let rightPassword = []; + let passFailList = []; + for(let validPassword of passwords){ + if( + isFiveOrMoreLength(validPassword) && + containsUppercaseLetter(validPassword) && + containsLowercaseLetter(validPassword) && + containsNumber(validPassword) && + containsSymbol(validPassword) && + isNewPassword(validPassword, rightPassword) + ){ + passFailList.push(true); + }else{ + passFailList.push(false); + } +rightPassword.push(validPassword); + } + return passFailList; + }; + + +// Returns true if password is not already used +function isNewPassword(passwordToCheck, listSoFar) { + return !listSoFar.includes(passwordToCheck); +}; + +// Returns true if string is at least 5 characters long +function isFiveOrMoreLength(string) { + return string.length >= 5; +}; + -function validatePasswords(passwords) {} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { diff --git a/README.md b/README.md index fe8d9029..0badcd00 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ Like learning a musical instrument, programming requires daily practise. -The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson. +The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson. The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson. - ## Solutions The solutions for this coursework can be found here: @@ -15,7 +14,7 @@ This is a **private** repository. Please request access from your Teachers, Budd ## Testing your work -- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-array-find/exercise.js` can be run from the root of the project. +- Each of the \*.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-array-find/exercise.js` can be run from the root of the project. - To run the tests in the `2-mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before). - To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before).