diff --git a/week-1/1-exercises/J-functions/exercise.js b/week-1/1-exercises/J-functions/exercise.js index 0ae5850..0fc4c36 100644 --- a/week-1/1-exercises/J-functions/exercise.js +++ b/week-1/1-exercises/J-functions/exercise.js @@ -1,5 +1,6 @@ function halve(number) { // complete the function here + return number / 2; } var result = halve(12); diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..015e21c 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,15 +16,15 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return num1 + num2; } -function multiply() { - +function multiply(num1, num3) { + return num1 * num2; } -function format() { +function format(num) { } diff --git a/week-2/1-exercises/B-boolean-literals/exercise.js b/week-2/1-exercises/B-boolean-literals/exercise.js index 6c5060f..9317628 100644 --- a/week-2/1-exercises/B-boolean-literals/exercise.js +++ b/week-2/1-exercises/B-boolean-literals/exercise.js @@ -5,7 +5,10 @@ Add the required variables with the correct boolean values assigned. */ -var codeYourFutureIsGreat = true; +const codeYourFutureIsGreat = false; +const mozafarIsCool = false; +const calculationCorrect = true; +const moreThan10Students = false; /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/D-predicates/exercise.js b/week-2/1-exercises/D-predicates/exercise.js index f600521..39bdd0c 100644 --- a/week-2/1-exercises/D-predicates/exercise.js +++ b/week-2/1-exercises/D-predicates/exercise.js @@ -6,21 +6,17 @@ */ // Finish the predicate function to test if the passed number is negative (less than zero) -function isNegative(number) { - -} +function isNegative(number) {} // Finish the predicate function to test if the passed number is between 0 and 10 -function isBetweenZeroAnd10(number) { - -} +function isBetweenZeroAnd10(number) {} /* DO NOT EDIT BELOW THIS LINE --------------------------- */ -var number = 5; -var numberNegative = isNegative(number); -var numberBetweenZeroAnd10 = isBetweenZeroAnd10(number); +const number = 5; +const numberNegative = isNegative(number); +const numberBetweenZeroAnd10 = isBetweenZeroAnd10(number); console.log("The number in test is " + number); console.log("Is the number negative? " + numberNegative); console.log("Is the number between 0 and 10? " + numberBetweenZeroAnd10); @@ -32,4 +28,3 @@ console.log("Is the number between 0 and 10? " + numberBetweenZeroAnd10); Is the number negative? false Is the number between 0 and 10? true */ - diff --git a/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index 6316fad..f02f1ef 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -1,21 +1,18 @@ // The below functions are syntactically correct but not outputting the right results. // Look at the tests and see how you can fix them. -function mood() { - let isHappy = true; - +function mood(isHappy) { + // let isHappy = true; if (isHappy) { return "I am happy"; - } else { - return "I am not happy"; } + return "I am not happy"; } -function greaterThan10() { +function greaterThan10(isBigEnough) { let num = 10; - let isBigEnough; - - if (isBigEnough) { + // let isBigEnough; + if (num >= 10) { return "num is greater than or equal to 10"; } else { return "num is not big enough"; @@ -23,23 +20,23 @@ function greaterThan10() { } function sortArray() { - let letters = ["a", "n", "c", "e", "z", "f"]; + const letters = ["a", "n", "c", "e", "z", "f"]; let sortedLetters; - + sortedLetters = letters.sort(); return sortedLetters; } function first5() { - let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; + const numbers = [1, 2, 3, 4, 5, 6, 7, 8]; let sliced; - + sliced = numbers.slice(0, 5); return sliced; } function get3rdIndex(arr) { - let index = 3; + const index = 3; let element; - + element = arr[index]; return element; } diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index bf7ecfd..0c44911 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,9 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} + function tidyUpString(strArr) { + + } /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +17,16 @@ Complete the function to check if the variable `num` satisfies the following req Tip: use logical operators */ -function validate(num) {} +function validate(num) { + if ((typeof num === 'number') && (num % 2 === 0) && (num <= 100)) + { + return true; + } + else + { + return false; + } +} /* Write a function that removes an element from an array @@ -26,7 +37,8 @@ The function must: */ function remove(arr, index) { - return; // complete this statement + const removed = arr.splice(index, 1); + return arr; } /* @@ -38,6 +50,17 @@ Write a function that: */ function formatPercentage(arr) { + const percentage = arr.map(function(n) { + if (n < 100) + { + console.log(`${Number(n.toFixed(2))}%`); + } + else + { + console.log("100%"); + } + + }) } @@ -72,7 +95,7 @@ test( "daniel", "irina", "gordon", - "ashleigh" + "ashleigh", ]) ); test( @@ -101,7 +124,7 @@ test( "c", "d", "e", - "f" + "f", ]) ); @@ -111,6 +134,6 @@ test( "23%", "18%", "100%", - "0.37%" + "0.37%", ]) -); \ No newline at end of file +); diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..fad33ee 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -6,13 +6,14 @@ Answer the following questions: - 1. This program throws an error. Why? (If you can't find it, try executing it). - 2. Remove the line that throws the error. - 3. What is printed to the console? - 4. How many times is "f1" called? - 5. How many times is "f2" called? - 6. What value does the "a" parameter take in the first "f1" call? - 7. What is the value of the "a" outer variable when "f1" is called for the first time? + 1. This program throws an error. Why? (If you can't find it, try executing it). Ans: ReferenceError: b is not defined. + Trying to console out b which is not a defined variable outside the function block. + 2. Remove the line that throws the error. Ans: console.log(b) is removed. + 3. What is printed to the console? Ans: console prints out 2 6 4 9 6 13 8 + 4. How many times is "f1" called? Ans: f1 is the else which is 9 and 13 ( twice) + 5. How many times is "f2" called? Ans: f2 even numbers are 2 , 6, 4, 6, 8 (5 times) + 6. What value does the "a" parameter take in the first "f1" call? Ans: 1st parameter call a = 6 + 7. What is the value of the "a" outer variable when "f1" is called for the first time? Ans: a + 1 = 7 */ let x = 2; @@ -28,7 +29,7 @@ const f2 = function(a, b) { console.log(x); console.log(a); -console.log(b); +// console.log(b); for (let i = 0; i < 5; ++i) { a = a + 1; diff --git a/week-2/2-mandatory/4-sorting-algorithm.js b/week-2/2-mandatory/4-sorting-algorithm.js index 3603942..3778824 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -14,7 +14,15 @@ 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) { +const sorted = arr.filter(function(value) { + return typeof value === "number" +}); + const ascendSort = sorted.sort(function(a, b) { + return a - b; + }) +return ascendSort; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/1-exercises/A-array-find/exercise.js b/week-3/1-exercises/A-array-find/exercise.js index d7fd51f..65df5fb 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -7,6 +7,10 @@ var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; +var findName = names.find(name => { + return name.charAt(0) || name.length > 7; +}) + var longNameThatStartsWithA = findLongNameThatStartsWithA(names); 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..4610f29 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -6,7 +6,7 @@ - Do not edit any of the existing code */ -var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; +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 @@ -18,7 +18,14 @@ 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]; + const nullCheck = (element) => element === null; + if(pairsByIndex.some(nullCheck) === true) { + process.exit(1) + } + else + {return [student, mentor];} + }); + console.log(pairs); diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index 3c02135..6eaaed6 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,32 +9,51 @@ To be safe, they need to land on the first unamed planet that has Oxygen levels Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5% */ -function safeLevels() { - +function safeLevels(arr) { + const convert = arr.map(function(level) { + return (parseFloat(level)) +}) +console.log(convert); +const levels = convert.filter(function(n) { + return n > 19.5 && n < 23.5; +}) +return `${levels[0]}%` } + /* ======= TESTS - DO NOT MODIFY ===== */ -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 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'); -function test(test_name, expr) { +function test(test_name, actual, expected) { let status; - if (expr) { - status = "PASSED"; + if (actual === expected) { + status = "PASSED"; } else { - status = "FAILED"; + status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; } - + console.log(`${test_name}: ${status}`); } test( - "safeLevels function works - case 2", - safeLevels(oxygenLevels1) === "19.9%" + "safeLevels function works - case 1", + safeLevels(oxygenLevels1), + "19.9%" ); test( "safeLevels function works - case 2", - safeLevels(oxygenLevels2) === "20.2%" + safeLevels(oxygenLevels2), + "20.2%" +); + +test( + "safeLevels function works - case 3", + safeLevels(oxygenLevels3), + "21.1%" ); \ No newline at end of file diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..79ae250 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,15 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(bush) { + const safeToEat = bush.every(function(pinky) { + return pinky === "pink"; + }) + if (safeToEat) { + return "Bush is safe to eat from" + } else { + return "Toxic! Leave bush alone!" + } } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -19,16 +26,27 @@ function bushChecker() { let bushBerryColours1 = ["pink", "pink", "pink", "neon", "pink", "transparent"] let bushBerryColours2 = ["pink", "pink", "pink", "pink"] -function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } +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}`); + console.log(`${test_name}: ${status}`); } -test("bushChecker funtion works - case 1", bushChecker(bushBerryColours1) === "Toxic! Leave bush alone!") -test("bushChecker funtion works - case 1", bushChecker(bushBerryColours2) === "Bush is safe to eat from") +test( + "bushChecker funtion works - case 1", + bushChecker(bushBerryColours1), + "Toxic! Leave bush alone!" +); + +test( + "bushChecker funtion works - case 1", + bushChecker(bushBerryColours2), + "Bush is safe to eat from" +); \ No newline at end of file diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..7ba44c9 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,10 +8,44 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(stayFamily) { + const stay = stayFamily.filter(function(name) { + if (name.split(" ")[1] === "family") { + return name[0] === "A"; + } + }); + return stay; } +// function colonisers(stayFamily) { + +// const stay = stayFamily.filter(function (name) { + +// if (name.split(" ")[1] === "family") { + +// return name[0] === "A"; + +// } + +// }); + +// return stay; + +// } + + + +function colonisers(stayFamily) { + + const stay = stayFamily.filter(function (name) { + + return name[0] === "A" && name.split(" ")[1] === "family"; + + }); + + return stay; + +} /* ======= TESTS - DO NOT MODIFY ===== */ const voyagers = [ diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..4a83842 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,9 +7,15 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - -} +function eligibleStudents(attendance) { + const attended8 = attendances.filter(function(n){ + return n[1] >= 8; + }); + const names = attended8.map(function(name) { + return name[0]; + }) + return names; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 53499c3..c2eb3a9 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,8 +7,21 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { +function journeyPlanner(locations, transportMode) { + const transportLoc = locations.filter(function (transport) { + + return transport.includes(transportMode); + + }); + + const locName = transportLoc.map(function (loc) { + + return loc[0]; + + }); + + return locName; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index eddfe44..df2bee7 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,8 +4,11 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(arr) { + const streetNames = arr.filter(function (names) { + return names.includes("Lane"); + }); + return streetNames; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 57b3d53..364247b 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -10,7 +10,7 @@ Passwords must - Have English uppercase letters (A-Z) - Have English lowercase letters (a-z) - Have numbers (0-9) -- Have non-alphanumeric symbols ("!", "#", "$", "%", ".") +- Have non-alphanumeric symbols ("!", "#", "$", "%", ".", "*", "&") Passwords must not be any previous password in the passwords array. @@ -23,7 +23,14 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { - + let passwordCheck = passwords.map((pass, index, array) => { + if(pass.length >= 5 && /[A-Z]/.test(pass) && /[a-z]/.test(pass) && /[0-9]/.test(pass) && /[!#$%.*&]/.test(pass) && array.indexOf(pass) === index){ + return true; + }else{ + return false; + } + }); + return passwordCheck; } /* ======= TESTS - DO NOT MODIFY ===== */