diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..9f6932f 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -5,9 +5,23 @@ // write your code here -var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; +var names = [ + "Rakesh", + "Antonio", + "Alexandra", + "Andronicus", + "Annam", + "Mikey", + "Anastasia", + "Karim", + "Ahmed", +]; -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +function findLongNameThatStartsWithA(names) { + return names[0] === "A" && names.length > 7; +} + +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..cfd4242 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -11,11 +11,18 @@ 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); - var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; -var pairs = pairsByIndex.map(function(indexes) { +//null checker function for me to understand clearer +function isItNull(num) { + return num === null; +} + +var pairs = pairsByIndex.map(function (indexes) { + if (isItNull(indexes)) { + process.exit(1); + } var student = students[indexes[0]]; var mentor = mentors[indexes[1]]; return [student, mentor]; diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..934d97b 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,7 +5,11 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement +function isGroupStudentsOnly(name) { + return students.includes(name); +} + +var groupIsOnlyStudents = group.every(isGroupStudentsOnly); // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); diff --git a/week-3/1-exercises/D-array-filter/exercise.js b/week-3/1-exercises/D-array-filter/exercise.js index 6e32cc6..b98d835 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,12 +8,16 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +function isItPairs(pair) { + return Array.isArray(pair) && pair.length === 2; +} + +var pairsByIndex = pairsByIndexRaw.filter(isItPairs); var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; -var pairs = pairsByIndex.map(function(indexes) { +var pairs = pairsByIndex.map(function (indexes) { var student = students[indexes[0]]; var mentor = mentors[indexes[1]]; return [student, mentor]; diff --git a/week-3/1-exercises/E-array-map/exercise.js b/week-3/1-exercises/E-array-map/exercise.js index 2835e92..174fac9 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,24 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +let multiplyByHundred = numbers.map(function multiplies(number) { + return number * 100; +}); + +console.log(multiplyByHundred); + +let multiplyByHundredSecond = numbers.map(function (number) { + return number * 100; +}); + +console.log(multiplyByHundredSecond); + +let multiplyByHundredThird = numbers.map((number) => { + return number * 100; +}); + +console.log(multiplyByHundredThird); + +let multiplyByHundredFourth = numbers.map((number) => number * 100); + +console.log(multiplyByHundredFourth); diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..8624b2f 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,18 @@ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach((number) => { + if (number % 3 === 0 && number % 5 === 0) { + console.log("FizzBuzz"); + } else if (number % 3 === 0) { + console.log("Fizz"); + } else if (number % 5 === 0) { + console.log("Buzz"); + } else { + console.log(number); + } +}); + /* 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..6a8e4b3 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..df1fe87 100644 --- a/week-3/1-exercises/H-array-methods-2/exercise.js +++ b/week-3/1-exercises/H-array-methods-2/exercise.js @@ -12,11 +12,11 @@ var everyone = [ "Rukmini", "Abdul", "Austine", - "Swathi" + "Swathi", ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive = everyone.slice(0, 5); // complete this statement +var lastFive = everyone.slice(-5); // 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..3abf9f6 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 newArr = str.split(""); + newArr[0] = newArr[0].toUpperCase(); + return newArr.join(""); +} /* 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..950ac1b 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); } /* diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index f9d745f..84126d9 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,8 +9,9 @@ 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(level) { + let levelSafety = level.find((level) => level > "19.5%" && level < "23.5%"); + return levelSafety; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -19,33 +20,23 @@ const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]; const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"]; const oxygenLevels3 = ["200%", "21.1%"]; -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } -test( - "safeLevels function works - case 1", - safeLevels(oxygenLevels1), - "19.9%" -); - -test( - "safeLevels function works - case 2", - safeLevels(oxygenLevels2), - "20.2%" -); - -test( - "safeLevels function works - case 3", - safeLevels(oxygenLevels3), - "21.1%" -); +test("safeLevels function works - case 1", safeLevels(oxygenLevels1), "19.9%"); + +test("safeLevels function works - case 2", safeLevels(oxygenLevels2), "20.2%"); + +test("safeLevels function works - case 3", safeLevels(oxygenLevels3), "21.1%"); diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index aca45ad..0594aee 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,26 +10,34 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(bush) { + let allPink = bush.every((item) => item === "pink"); + + if (allPink) { + 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"] -let bushBerryColours2 = ["pink", "pink", "pink", "pink"] +let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"]; +let bushBerryColours2 = ["pink", "pink", "pink", "pink"]; -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } test( diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index 239eddd..94ccd2a 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,8 +8,8 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(name) { + return name.filter((name) => name[0] === "A" && name.includes("family")); } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -26,24 +26,26 @@ const voyagers = [ "Asimov", "Oscar family", "Avery family", - "Archer family" + "Archer family", ]; -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)}`; - } - - console.log(`${test_name}: ${status}`); + 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}`); } -test( - "colonisers function works", - colonisers(voyagers), - ["Adam family", "Avery family", "Archer family"] -) +test("colonisers function works", colonisers(voyagers), [ + "Adam family", + "Avery family", + "Archer family", +]); diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index d8fe052..3a19a0a 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,10 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(attendance) { + return attendance + .filter((classes) => classes[1] >= 8) + .map((classes) => classes[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -19,23 +21,27 @@ const attendances = [ ["Elamin", 6], ["Adam", 7], ["Tayoa", 11], - ["Nina", 10] -] + ["Nina", 10], +]; -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)}`; - } - - console.log(`${test_name}: ${status}`); + 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}`); } -test("eligibleStudents function works", - eligibleStudents(attendances), - ["Ahmed", "Clement", "Tayoa", "Nina"] -); +test("eligibleStudents function works", eligibleStudents(attendances), [ + "Ahmed", + "Clement", + "Tayoa", + "Nina", +]); diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 816637d..022e0dd 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,7 +7,8 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(location, transport) { + return location.filter((lt) => lt.includes(transport)).map((lt) => lt[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -17,19 +18,21 @@ const londonLocations = [ ["Greenwich", "bus", "river boat", "dlr", "air line", "tube"], ["London Bridge", "tube", "river boat"], ["Tower Bridge", "tube", "bus"], -] +]; -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)}`; - } - - console.log(`${test_name}: ${status}`); + 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}`); } test( diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index ef4e1c5..3022c6a 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,35 +4,36 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(street) { + return street.filter((name) => name.includes("Lane")); } /* ======= TESTS - DO NOT MODIFY ===== */ const streetNames = [ - "Abchurch Lane", - "Adam's Court", - "Addle Hill", - "Addle Lane", - "Alban Highwalk" -] + "Abchurch Lane", + "Adam's Court", + "Addle Hill", + "Addle Lane", + "Alban Highwalk", +]; -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)}`; - } - - console.log(`${test_name}: ${status}`); + 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}`); } -test( - "getLanes function works", - getLanes(streetNames), - ["Abchurch Lane", "Addle Lane"] -); +test("getLanes function works", getLanes(streetNames), [ + "Abchurch Lane", + "Addle Lane", +]); diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 8f9e597..f3e04db 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -22,33 +22,70 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) { +function validatePasswords(pws) { + function validity(pw) { + //at least 5 characters + function minFive(pw) { + return pw.length >= 5; + } + //least one English uppercase letter (A-Z) + function upperCase(pw) { + return /[A-Z]/.test(pw); + } + //least one English lowercase letter (a-z) + function lowerCase(pw) { + return /[a-z]/.test(pw); + } + //least one number (0-9) + function numbers(pw) { + return /[0-9]/.test(pw); + } + //least one non-alphanumeric symbol ("!", "#", "$", "%", ".", "*", "&") + function specialChara(pw) { + return /[!#$%.]/.test(pw); + } + + return ( + minFive(pw) && + upperCase(pw) && + lowerCase(pw) && + numbers(pw) && + specialChara(pw) + ); + } + //when it first appears + function first(fullPw, pw, i) { + return fullPw.indexOf(pw) === i; + } + return pws.map((pw, i) => validity(pw) && first(pws, pw, i)); } /* ======= 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",