diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..063b04d 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -5,10 +5,23 @@ // write your code here -var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; - -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +var names = [ + "Rakesh", + "Antonio", + "Alexandra", + "Andronicus", + "Annam", + "Mikey", + "Anastasia", + "Karim", + "Ahmed", +]; +//var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA); +function findLongNameThatStartsWithA(name) { + return name.length > 7 && name[0] === "A"; +} console.log(longNameThatStartsWithA); /* EXPECTED OUTPUT */ diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..c33b753 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -15,7 +15,10 @@ var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; var students = ["Islam", "Lesley", "Harun", "Rukmini"]; var mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; -var pairs = pairsByIndex.map(function(indexes) { +let pairs = pairsByIndex.map((indexes) => { + if (indexes === null) { + 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/README.md b/week-3/1-exercises/C-array-every/README.md index 65a7055..39d0421 100644 --- a/week-3/1-exercises/C-array-every/README.md +++ b/week-3/1-exercises/C-array-every/README.md @@ -18,7 +18,7 @@ To check that each name is longer than 3 characters, you'd have to run this func ## `.every()` -_Searches through an array and returns true if every item satisifies the predicate function you provided. Otherwise, it returns false_. +_Searches through an array and returns true if every item satisfies the predicate function you provided. Otherwise, it returns false_. ```js var studentNameLength = students.every(isAboveThreshold); diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..a6f8e2a 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,8 +5,11 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement +var groupIsOnlyStudents = group.every(comparison); // complete this statement +function comparison(name) { + return students.includes(name); +} 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..d91a08b 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,12 +8,14 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement +var pairsByIndex = pairsByIndexRaw.filter((item) => { + return Array.isArray(item) && item.length === 2; +}); // Complete this statement 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..1c8963d 100644 --- a/week-3/1-exercises/E-array-map/exercise.js +++ b/week-3/1-exercises/E-array-map/exercise.js @@ -3,3 +3,18 @@ var numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; +/*var multiplied = numbers.map(multiply); + +function multiply(num) { + return num * 100; +} +//console.log(numbers.map(multiply)); + +console.log(numbers); +console.log(multiplied);*/ + +numbers.map(function (num) { + return num * 100; +}); + +console.log(numbers.map((num) => num * 100)); diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..c1317c7 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(function (num) { + if (num % 3 === 0 && num % 5 === 0) { + console.log("FizzBuzz"); + } else if (num % 5 === 0) { + console.log("Buzz"); + } else if (num % 3 === 0) { + console.log("Fizz"); + } else { + console.log(num); + } +}); + /* 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..bb49f56 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(2); // 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..162f3ea 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,12 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let newWord = str.split(""); + let firstLetter = newWord[0].toUpperCase(); + newWord[0] = firstLetter; + return newWord.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..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..b24c119 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -1,6 +1,5 @@ /* -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. +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. It has produced an array of their Oxygen levels. @@ -9,8 +8,11 @@ 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) { + let safePlanet = arr.find( + (item) => parseFloat(item) > 19.5 && parseFloat(item) < 23.5 + ); + return safePlanet; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -19,33 +21,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..1606454 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -9,27 +9,48 @@ Use the tests to confirm which message to return */ +/* +function checkPink(fruit) { + return fruit === "pink"; +} +function bushChecker(arr) { + let tester = arr.every(checkPink); + let txt = ""; + if (tester == true) { + txt = "Bush is safe to eat from"; + } else { + txt = "Toxic! Leave bush alone!"; + } + return txt; +} */ -function bushChecker() { - +function bushChecker(arr) { + let testFruit = arr.every((item) => item === "pink"); + if (testFruit === 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"] -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..7a4e22e 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,8 +8,11 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(arr) { + let settlers = arr.filter( + (item, index, arr) => item[0] === "A" && arr[index].includes("family") + ); + return settlers; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -26,24 +29,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..525f515 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,8 +7,9 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(arr) { + let students = arr.filter((student) => student[1] >= 8); + return students.map((data) => data[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -19,23 +20,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..89cfd81 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,7 +7,11 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(locationArray, transportMode) { + let location = locationArray.filter((element) => + element.includes(transportMode) + ); + return location.map((item) => item[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -17,19 +21,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..d235e0e 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,35 +4,41 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { +function getLanes(arr) { + function streetChecker(street) { + return street.includes("Lane"); + } + let streetsWithLane = arr.filter(streetChecker); + return streetsWithLane; } /* ======= 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 d408ba6..9b1019f 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -23,32 +23,51 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { + let passwordChecks = passwords.map( + (password, index) => + password.length >= 5 && + /[A-Z]/.test(password) && + /[a-z]/.test(password) && + /[0-9]/.test(password) && + /[!#$%.]/.test(password) && + noRepeatPassword(passwords, password, index) + ); + function noRepeatPassword(arr, password, index) { + let validPassword = arr.indexOf(password); + if (validPassword === index) { + return true; + } + return false; + } + return passwordChecks; } /* ======= 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", diff --git a/week-3/3-extra/3-sorting-algorithm.js b/week-3/3-extra/3-sorting-algorithm.js index 3603942..6b5b37f 100644 --- a/week-3/3-extra/3-sorting-algorithm.js +++ b/week-3/3-extra/3-sorting-algorithm.js @@ -14,8 +14,28 @@ 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 result = []; + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] === "number") { + result.push(arr[i]); + } + for (let i = 0; i < result.length; i++) { + let minValue = i; + for (let j = i + 1; j < result.length; j++) { + if (result[j] < result[minValue]) { + minValue = j; + } + } + if (minValue !== i) { + let temp = result[minValue]; + result[minValue] = result[i]; + result[i] = temp; + } + } + } + return result; +} /* ======= TESTS - DO NOT MODIFY ===== */ const agesCase1 = [ diff --git a/week-3/3-extra/creditCardValidator.js b/week-3/3-extra/creditCardValidator.js new file mode 100644 index 0000000..98b0570 --- /dev/null +++ b/week-3/3-extra/creditCardValidator.js @@ -0,0 +1,56 @@ +/* +- Number must be 16 digits, all of them must be numbers. +- You must have at least two different digits represented (all of the digits cannot be the same). +- The final digit must be even. +- The sum of all the digits must be greater than 16. +*/ + +//let numToStringArray = ["1", "2", "2", "4", "4", "6", "1"]; +/*let uniqueDigits = []; +let checkDigitsNotSame = numToStringArray.map((item) => { + if (numToStringArray.includes(item)) { + uniqueDigits.push(item); + } +}); +console.log(uniqueDigits);*/ + +//console.log(checkLastDigitIsEven); + +function isCardValid(cardNumber) { + //CHECK THAT THE DIGITS ARE NUMBERS + if (typeof cardNumber === "number") { + let uniqueDigits = []; + let numToStringArray = cardNumber.toString().split(""); //cardNumber is converted first to a string and then to an array + + //SUMS THE DIGITS ON THE CARD TO CHECK IT'S > 16 + let sum = 0; + let sumOfDigits = numToStringArray.map((item) => (sum += Number(item))); + // console.log(sum); + + //TO CHECK THAT THERE ARE AT LEAST 2 DIFFERENT NUMBERS + numToStringArray.map((item) => { + if (!uniqueDigits.includes(item)) { + uniqueDigits.push(item); + } + }); + //CHECK THAT THE LAST DIGIT IS EVEN + let checkLastDigitIsEven = + numToStringArray[numToStringArray.length - 1] % 2 === 0; + + if ( + numToStringArray.length === 16 && + sum > 16 && + uniqueDigits.length >= 2 && + checkLastDigitIsEven + ) { + //console.log("Yes it is"); + return true; + } else { + // console.log("No it isn't"); + return false; + } + } else { + console.log("Invalid card number"); + } +} +console.log(isCardValid(4444444444444442));