diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..73228f3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // 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": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\week-3\\1-exercises\\D-array-filter\\exercise.js" + } + ] +} \ No newline at end of file diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..aa4ae32 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -4,10 +4,14 @@ */ // write your code here +function findLongNameThatStartsWithA(name) { + return name.length > 7 && name[0] === "A"; +} var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA); + console.log(longNameThatStartsWithA); diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..fd15037 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -11,14 +11,21 @@ var 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 isNull (number){ + return number === null; +} var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; var pairs = pairsByIndex.map(function(indexes) { + if ( pairsByIndex.some(isNull) === true){ + process.exit(1); + } + else{ var student = students[indexes[0]]; var mentor = mentors[indexes[1]]; return [student, mentor]; + } }); console.log(pairs); diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..233f8e6 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -4,9 +4,8 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; - -var groupIsOnlyStudents; // complete this statement - +var groupIsOnlyStudents = group.every(x => students.includes(x));// complete this statement + if (groupIsOnlyStudents) { console.log("The group contains only students"); } else { diff --git a/week-3/1-exercises/D-array-filter/exercise.js b/week-3/1-exercises/D-array-filter/exercise.js index 6e32cc6..1a554c1 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,15 +8,21 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +var pairsByIndex = pairsByIndexRaw.filter(function name(arr) { + for (const key in arr) { + if (typeof key !== null && key >=1 ) { + return arr[key]; + } + } + +}); // Complete this statement var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; -var pairs = pairsByIndex.map(function(indexes) { - var student = students[indexes[0]]; - var mentor = mentors[indexes[1]]; - return [student, mentor]; -}); - + var pairs = pairsByIndex.map(function (indexes) { + var student = students[indexes[0]]; + var mentor = mentors[indexes[1]]; + return [student, mentor]; + }); console.log(pairs); diff --git a/week-3/1-exercises/E-array-map/exercise.js b/week-3/1-exercises/E-array-map/exercise.js index 2835e92..55c808a 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,8 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +var numberMultiplied = numbers.map(function(number){ + return number*100; +}); + +console.log(numberMultiplied); \ No newline at end of file diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..49bddf9 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -8,7 +8,23 @@ */ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + +var names = ["Daniel", "mozafar", "irina"]; +arr.forEach(function (name, index) { + if (name % 3 === 0 && name % 5 === 0) { + console.log(name + ": " + "FizzBuzz"); + } + else if (name % 5 === 0) { + console.log(name + ": " + "Buzz"); + } + else if (name % 3 === 0) { + console.log(name + ": " + "Fizz"); + } + else { + console.log(name + ": " + name); + } +}); /* EXPECTED OUTPUT */ /* diff --git a/week-3/1-exercises/G-array-methods/exercise.js b/week-3/1-exercises/G-array-methods/exercise.js index 44e9c80..8704997 100644 --- a/week-3/1-exercises/G-array-methods/exercise.js +++ b/week-3/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ var numbers = [3, 2, 1]; -var sortedNumbers; // complete this statement +var sortedNumbers= numbers.sort(); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/G-array-methods/exercise2.js b/week-3/1-exercises/G-array-methods/exercise2.js index 3dd24a1..dd87089 100644 --- a/week-3/1-exercises/G-array-methods/exercise2.js +++ b/week-3/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,7 @@ var mentors = ["Daniel", "Irina", "Rares"]; var students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -var everyone; // complete this statement +var everyone = mentors.concat(students); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise.js b/week-3/1-exercises/H-array-methods-2/exercise.js index d36303b..0da03a1 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ var everyone = [ "Swathi" ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive = everyone.slice(0,5); // complete this statement +var lastFive=everyone.slice(everyone.length-5,everyone.length); // complete this statement /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise2.js b/week-3/1-exercises/H-array-methods-2/exercise2.js index b7be576..c57726b 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise2.js +++ b/week-3/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 newStr= str.slice(0,1).toUpperCase()+str.slice(1,str.length); + + return newStr; +} /* DO NOT EDIT BELOW THIS LINE diff --git a/week-3/1-exercises/H-array-methods-2/exercise3.js b/week-3/1-exercises/H-array-methods-2/exercise3.js index 82e9dd8..75a6b76 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise3.js +++ b/week-3/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ var ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); // complete this statement } /* diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index f9d745f..50a7169 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,10 +9,20 @@ To be safe to land on, a planet needs to have an Oxygen level between 19.5% and Write a function that finds the first safe oxygen level in the array - Oxygen between 19.5% and 23.5% */ -function safeLevels() { +function safeLevels(arr) { + var safeLevel = arr.find(function (value){ + if( parseFloat(value) > 19.5 && parseFloat(value) < 23.5){ + return true; + } + else { + return false; + } + }) + return safeLevel; } + /* ======= TESTS - DO NOT MODIFY ===== */ const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]; diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index aca45ad..cfba80a 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,10 +10,26 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function colorCheck(color){ + if (color ==="pink"){ + return true; + } } +function bushChecker(arr) { + let safeBerries = arr.every(colorCheck); + if (safeBerries === true){ + return "Bush is safe to eat from"; + } + else { + return "Toxic! Leave bush alone!"; + } + } + + + + + /* ======= TESTS - DO NOT MODIFY ===== */ let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"] diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index 239eddd..36c4587 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,7 +8,10 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { +function colonisers(arr) { + return arr.filter(function(name){ + return name.startsWith("A") && name.includes("family"); + }) } diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index d8fe052..fbc4c2c 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,14 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(arr) { + let nameArr = []; + for(let i=0;i=8){ + nameArr.push(arr[i][0]); + } + } +return nameArr; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -22,6 +28,7 @@ const attendances = [ ["Nina", 10] ] + const util = require('util'); function test(test_name, actual, expected) { diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 816637d..b1c6736 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,9 +7,18 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(arr,mode) { + let nameArr = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i].includes(mode)) { + nameArr.push(arr[i][0]); + } + } + return nameArr; } + + /* ======= TESTS - DO NOT MODIFY ===== */ const londonLocations = [ diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index ef4e1c5..96e1a76 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,10 +4,18 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(arr,name) { + let nameArr = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i].includes("Lane")) { + nameArr.push(arr[i]); + } + } + return nameArr; } + + /* ======= TESTS - DO NOT MODIFY ===== */ const streetNames = [ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 8f9e597..7a23612 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -22,33 +22,64 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) { +function validatePasswords(arr) { + + + var passw = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z]).{5,}$/; + let nameArr = []; + for (let i = 0; i < arr.length - 1; i++) { + for (let j = i + 1; j <= arr.length; j++) { + if (arr[i] === arr[j]) { + arr[j] = nameArr.push(false); + nameArr.shift(); + } + } + } + + for (let i = 0; i < arr.length; i++) { + + if (passw.test(arr[i])){ + nameArr.push(true); + + } + + else { + nameArr.push(false); + } + + } + return nameArr; + + } + /* ======= TESTS - DO NOT MODIFY ===== */ -const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"] -const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"] +const passwords1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]; +const passwords2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]; -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (util.isDeepStrictEqual(actual, expected)) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } + let status; + if (util.isDeepStrictEqual(actual, expected)) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } - console.log(`${test_name}: ${status}`); + console.log(`${test_name}: ${status}`); } test( "validatePasswords function works - case 1", validatePasswords(passwords1), [false, false, true, false, false] - ); +); test( "validatePasswords function works - case 2",