diff --git a/week-1/1-exercises/B-hello-world/exercise.js b/week-1/1-exercises/B-hello-world/exercise.js index b179ee9..cc7e44f 100644 --- a/week-1/1-exercises/B-hello-world/exercise.js +++ b/week-1/1-exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ -console.log("Hello world"); +console.log("Hello world. I just started learning javascript"); +console.log(2020); diff --git a/week-1/1-exercises/C-variables/exercise.js b/week-1/1-exercises/C-variables/exercise.js index a6bbb97..a1af969 100644 --- a/week-1/1-exercises/C-variables/exercise.js +++ b/week-1/1-exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var greeting = "Hello World!"; +console.log(greeting); +console.log(greeting); console.log(greeting); diff --git a/week-1/1-exercises/D-strings/exercise.js b/week-1/1-exercises/D-strings/exercise.js index 2cffa6a..2d108e2 100644 --- a/week-1/1-exercises/D-strings/exercise.js +++ b/week-1/1-exercises/D-strings/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` +var message = "This is a string"; +var messageType = typeof message; +console.log(messageType); // logs 'string' console.log(message); diff --git a/week-1/1-exercises/E-strings-concatenation/exercise.js b/week-1/1-exercises/E-strings-concatenation/exercise.js index 2cffa6a..6943e8c 100644 --- a/week-1/1-exercises/E-strings-concatenation/exercise.js +++ b/week-1/1-exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +var greetingStart = "Hello, my name is "; +var name = "Berhane"; -console.log(message); +var greeting = greetingStart + name; + +console.log(greeting); // Logs "Hello, my name is Daniel" diff --git a/week-1/1-exercises/F-strings-methods/exercise.js b/week-1/1-exercises/F-strings-methods/exercise.js index 2cffa6a..3b95944 100644 --- a/week-1/1-exercises/F-strings-methods/exercise.js +++ b/week-1/1-exercises/F-strings-methods/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` - +var name = "Berhane"; +var nameLength = name.length; +var message = "My name is " + name +" and my name is " + nameLength + " characters long" +console.log(nameLength); // Logs 6 console.log(message); diff --git a/week-1/1-exercises/F-strings-methods/exercise2.js b/week-1/1-exercises/F-strings-methods/exercise2.js index b4b4694..068ec34 100644 --- a/week-1/1-exercises/F-strings-methods/exercise2.js +++ b/week-1/1-exercises/F-strings-methods/exercise2.js @@ -1,3 +1,3 @@ -const name = " Daniel "; - -console.log(message); +const name = " My name is Berhane and my name is 7 characters long "; +name.trim(); +console.log(name); diff --git a/week-1/1-exercises/G-numbers/exercise.js b/week-1/1-exercises/G-numbers/exercise.js index 49e7bc0..f92d940 100644 --- a/week-1/1-exercises/G-numbers/exercise.js +++ b/week-1/1-exercises/G-numbers/exercise.js @@ -1 +1,5 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudents = 15; +var numberOfMentors = 8; +var sum = numberOfStudents + numberOfMentors; +console.log("Total numnber of students and mentors:" + sum); diff --git a/week-1/1-exercises/I-floats/exercise.js b/week-1/1-exercises/I-floats/exercise.js index a5bbcd8..f88e40a 100644 --- a/week-1/1-exercises/I-floats/exercise.js +++ b/week-1/1-exercises/I-floats/exercise.js @@ -1,2 +1,9 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var stuPercentile = numberOfStudents/23*100; +var mentorpercentile = numberOfMentors/23*100; +stuPercentile = Math.round(stuPercentile); +mentorpercentile = Math.round(mentorpercentile); +console.log("Percentage Students:" +stuPercentile + "%" ); +console.log("Percentage Mentors:" + mentorpercentile + "%"); + diff --git a/week-1/1-exercises/J-functions/exercise.js b/week-1/1-exercises/J-functions/exercise.js index 0ae5850..c1b5db3 100644 --- a/week-1/1-exercises/J-functions/exercise.js +++ b/week-1/1-exercises/J-functions/exercise.js @@ -1,7 +1,11 @@ function halve(number) { // complete the function here + return number/2; } var result = halve(12); - +var result2 = halve(32); +var result3 = halve(3); console.log(result); +console.log(result2); +console.log(result3); diff --git a/week-1/1-exercises/J-functions/exercise2.js b/week-1/1-exercises/J-functions/exercise2.js index 82ef5e7..468d455 100644 --- a/week-1/1-exercises/J-functions/exercise2.js +++ b/week-1/1-exercises/J-functions/exercise2.js @@ -1,5 +1,6 @@ function triple(number) { // complete function here + return number * 3; } var result = triple(12); diff --git a/week-1/1-exercises/K-functions-parameters/exercise.js b/week-1/1-exercises/K-functions-parameters/exercise.js index 8d5db5e..71969d1 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise.js +++ b/week-1/1-exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1,num2) { // Calculate the result of the function and return it + return num1 * num2; } // Assign the result of calling the function the variable `result` diff --git a/week-1/1-exercises/K-functions-parameters/exercise2.js b/week-1/1-exercises/K-functions-parameters/exercise2.js index db7a890..1dcf0e0 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise2.js +++ b/week-1/1-exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,10 @@ // Declare your function first +function divide(num1, num2) { + // Calculate the result of the function and return it + return num1 / num2; +} +// Assign the result of calling the function the variable `result` var result = divide(3, 4); console.log(result); diff --git a/week-1/1-exercises/K-functions-parameters/exercise3.js b/week-1/1-exercises/K-functions-parameters/exercise3.js index 537e9f4..fbfbf0c 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise3.js +++ b/week-1/1-exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,7 @@ // Write your function here +function createGreeting(name){ + return "Hello, my name is " + name; +} var greeting = createGreeting("Daniel"); diff --git a/week-1/1-exercises/K-functions-parameters/exercise4.js b/week-1/1-exercises/K-functions-parameters/exercise4.js index 7ab4458..067cb5f 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise4.js +++ b/week-1/1-exercises/K-functions-parameters/exercise4.js @@ -1,4 +1,9 @@ // Declare your function first +function addition(num1, num2) { + return num1 + num2; +} + +sum = addition(13, 124); // Call the function and assign to a variable `sum` diff --git a/week-1/1-exercises/K-functions-parameters/exercise5.js b/week-1/1-exercises/K-functions-parameters/exercise5.js index 7c5bcd6..23d1783 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise5.js +++ b/week-1/1-exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,8 @@ // Declare your function here +function createLongGreeting(name, age){ + return "Hello, my name is " + name + " and I'm " + age + " years old" + +} const greeting = createLongGreeting("Daniel", 30); diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..353fb45 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -2,18 +2,19 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +function addNumbers(a, b, c) { return a + b + c; } -function introduceMe(name, age) -return "Hello, my name is " + name "and I am " age + "years old"; +function introduceMe(name, age){ +return "Hello, my name is " + name + " and I am " + age + " years old"; +} -function getAddition(a, b) { - total = a ++ b +function getRemainder(a, b) { + // Use string interpolation here - return "The total is %{total}" + return `The remainder is ${a%b}`; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -23,12 +24,12 @@ function getAddition(a, b) { function test(test_name, expr) { let status; if (expr) { - status = "PASSED" + status = "PASSED" ; } else { - status = "FAILED" + status = "FAILED" ; } - console.log(`${test_name}: ${status}`) + console.log(`${test_name}: ${status}`) ; } test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..f64f61a 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,16 +1,16 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return wordtrim(); + return word.trim(); } function getWordLength(word) { - return "word".length() + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + + return a * b * c; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index bbb88a2..0231107 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,16 +1,18 @@ // Add comments to explain what this function does. You're meant to use Google! function getNumber() { - return Math.random() * 10; + return Math.random() * 10; // returns a random integer from 0 to 9 + } // Add comments to explain what this function does. You're meant to use Google! function s(w1, w2) { - return w1.concat(w2); + return w1.concat(w2); // concatenates w1(string 1) with w2(string 2) } function concatenate(firstWord, secondWord, thirdWord) { // Write the body of this function to concatenate three words together // Look at the test case below to understand what to expect in return + return firstWord +" " + secondWord +" "+ thirdWord; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 6b84208..b61c6b4 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -5,7 +5,12 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(productPrice) { + + var salesTax = 0.2 * productPrice; + return productPrice+salesTax; + +} /* CURRENCY FORMATTING @@ -17,7 +22,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function formatCurrency() {} +function formatCurrency(price) { + var totPrice = calculateSalesTax(price); + totPrice = totPrice.toFixed(2); + return "£"+totPrice; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 7f321d9..d31f3ed 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(amountPound) { + var amountUSD = amountPound*1.4 + return amountUSD +} /* CURRENCY FORMATTING @@ -16,7 +19,14 @@ function convertToUSD() {} Find a way to add 1% to all currency conversions (think about the DRY principle) */ -function convertToBRL() {} +function convertToBRL(amountInPound) { + + var amountBRL = amountInPound * 5.7 + amountBRL += amountBRL*0.01 + return amountBRL + +} +console.log(convertToBRL(30)) /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..33c6a4d 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,27 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return ((num1*10) + (num2*10))/10; } -function multiply() { - +function multiply(num1, num2) { + return num1 * num2; } -function format() { - +function format(num) { + return "£" + num; } -const startingValue = 2 +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +/* we have easier ways applying the already created functions for more flexibility */ +let badCode = "£" + add(startingValue, 10) * 2; /* BETTER PRACTICE */ -let goodCode = +let goodCode = format(multiply(add(startingValue, 10), 2)); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -46,17 +47,17 @@ To run these tests type `node 2-piping.js` into your terminal function test(test_name, expr) { let status; if (expr) { - status = "PASSED" + status = "PASSED"; } else { - status = "FAILED" + status = "FAILED"; } - console.log(`${test_name}: ${status}`) + console.log(`${test_name}: ${status}`); } -test('add function - case 1 works', add(1,3) === 4) -test('add function - case 2 works', add(2.4,5.3) === 7.7) -test('multiply function works', multiply(2,3) === 6) -test('format function works', format(16) === "£16") -test('badCode variable correctly assigned', badCode === "£24") -test('goodCode variable correctly assigned', goodCode === "£24") \ No newline at end of file +test("add function - case 1 works", add(1, 3) === 4); +test("add function - case 2 works", add(2.4, 5.3) === 7.7); +test("multiply function works", multiply(2, 3) === 6); +test("format function works", format(16) === "£16"); +test("badCode variable correctly assigned", badCode === "£24"); +test("goodCode variable correctly assigned", goodCode === "£24"); diff --git a/week-1/3-extra/3-magic-8-ball.js b/week-1/3-extra/3-magic-8-ball.js index 1bb1089..9114845 100644 --- a/week-1/3-extra/3-magic-8-ball.js +++ b/week-1/3-extra/3-magic-8-ball.js @@ -45,17 +45,36 @@ Very doubtful. // This should log "The ball has shaken!" // and return the answer. -function shakeBall() {} +function shakeBall(answer) { + + console.log("The ball has shaken!") + + return "The ball has shaken!" + +} // The answer should come from shaking the ball -let answer; +let answer = ["very positive", "positive", "negative", "very negative"]; // When checking the answer, we should tell someone if the answer is // - very positive // - positive // - negative // - very negative -function checkAnswer() {} +function checkAnswer() { + for(let i=0;i=0 && number <=10){ + return true; +} } /* diff --git a/week-2/1-exercises/E-conditionals/exercise.js b/week-2/1-exercises/E-conditionals/exercise.js index acbaaa8..eb77e1c 100644 --- a/week-2/1-exercises/E-conditionals/exercise.js +++ b/week-2/1-exercises/E-conditionals/exercise.js @@ -8,6 +8,11 @@ var name = "Daniel"; var danielsRole = "mentor"; +if (name === danielsRole) { + console.log("Hi, I'm Daniel, I'm a mentor."); +} else { + console.log("Hi, I'm Daniel, I'm a student."); +} /* EXPECTED RESULT diff --git a/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index 6316fad..1595b56 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -1,21 +1,21 @@ // 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) { + if (isHappy === true) { return "I am happy"; } else { return "I am not happy"; } } -function greaterThan10() { - let num = 10; - let isBigEnough; +function greaterThan10(num) { + num = 10; + //isBigEnough = 10; - if (isBigEnough) { + if (num >= 10) { return "num is greater than or equal to 10"; } else { return "num is not big enough"; @@ -24,21 +24,21 @@ function greaterThan10() { function sortArray() { let letters = ["a", "n", "c", "e", "z", "f"]; - let sortedLetters; + let sortedLetters = letters.sort(); return sortedLetters; } function first5() { let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; - let sliced; + let sliced =numbers.slice(0,5) return sliced; } function get3rdIndex(arr) { let index = 3; - let element; + let element = arr[3]; return element; } diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index bf7ecfd..1a10cef 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,13 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} +function tidyUpString(strArr) { + let newArr = strArr.map(item => item.trim().replace('/', '').toLowerCase()); + return newArr; +} + + + /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +21,14 @@ 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 +39,10 @@ The function must: */ function remove(arr, index) { - return; // complete this statement + let newArr = arr + newArr.splice(index,1) + + return newArr; // complete this statement } /* @@ -38,6 +54,14 @@ Write a function that: */ function formatPercentage(arr) { + let newArr = arr.map( item =>{ + if(item > 100 ){ + item = 100 + } + return `${Number(item.toFixed(2))}%`; + + }) + return newArr } diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..6a2bc9d 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -7,12 +7,19 @@ Answer the following questions: 1. This program throws an error. Why? (If you can't find it, try executing it). + its because b as not defined 2. Remove the line that throws the error. + the error was on console.log(b) now removed 3. What is printed to the console? - 4. How many times is "f1" called? + after removing the error code the output on the console is 2 6 4 9 6 13 8 + 4. How many times is "f1" called? + Twice 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? + Three times + 6. What value does the "a" parameter take in the first "f1" call? + the value as 8 + 7. What is the value of the "a" outer variable when "f1" is called for the first time? + it was 6 */ let x = 2; @@ -28,7 +35,7 @@ const f2 = function(a, b) { console.log(x); console.log(a); -console.log(b); + for (let i = 0; i < 5; ++i) { a = a + 1; @@ -40,3 +47,4 @@ for (let i = 0; i < 5; ++i) { console.log(e); } } + diff --git a/week-2/2-mandatory/4-sorting-algorithm.js b/week-2/2-mandatory/4-sorting-algorithm.js index 3603942..042060d 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -14,7 +14,30 @@ 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 newArr = [] + for(let i=0;i newArr[i]) { + done = false; + var tmp = newArr[i - 1]; + newArr[i - 1] = newArr[i]; + newArr[i] = tmp; + } + } + } + + return newArr; +} + //console.log(newArr) /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/1-exercises/F-array-forEach/exercise.js b/week-3/1-exercises/F-array-forEach/exercise.js index e83e2df..448a8a1 100644 --- a/week-3/1-exercises/F-array-forEach/exercise.js +++ b/week-3/1-exercises/F-array-forEach/exercise.js @@ -8,6 +8,21 @@ */ var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach(function(number){ + if (number % 15 === 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..cca17b4 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,10 @@ var everyone = [ "Swathi" ]; -var firstFive; // complete this statement -var lastFive; // complete this statement +var firstFive =everyone.slice(0,5); // complete this statement +//console.log(firstFive) +var lastFive = everyone.slice(2,7); // complete this statement +//console.log(lastFive) /* 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..99d11f8 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,15 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + + str = str.split(''); + str[0]= str[0].toUpperCase() + + str = str.join('') + return str + +} /* 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 3c02135..e75bb25 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,7 +9,8 @@ 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(oxygenL) { + return oxygenL.find(item=>(item > "19.5%" && item < "23.5%")) } diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..5ed2a2f 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -9,10 +9,28 @@ Use the tests to confirm which message to return */ +/*function barriesCheck(barries) { + + if (barries[j] === "pink") { + return "Bush is safe to eat from" + } + else { + return "Toxic! Leave bush alone!" + } +} +}*/ -function bushChecker() { +//function bushChecker() { + function barriesColor(color) { + return color == "pink" + } + function bushChecker(arr) { -} + if (arr.every(barriesColor)) { + return "Bush is safe to eat from" + } + return "Toxic! Leave bush alone!" + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..b287d95 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,7 +8,8 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { +function colonisers(names) { + return names.filter(element => element.charAt(0)==="A" && element.includes("family")) } diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..6e56bbe 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,9 +7,17 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { +function eligibleStudents(arr) { + let newArr=[] + for(let i=0;i=8){ + newArr.push(arr[i][0]) + } + } + return newArr + } + -} /* ======= 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..e8f431e 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,7 +4,8 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { +function getLanes(lane) { + return lane.filter(element => element.includes("Lane")) } diff --git a/week-3/2-mandatory/7-password-validator.js b/week-3/2-mandatory/7-password-validator.js index 57b3d53..6908d21 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -21,49 +21,65 @@ Expected Result: PasswordValidationResult= [false, false, false, false, true] */ +function validatePasswords(psw) { + var pswRegex = new RegExp( + "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!.#$%])(?=.{5,})" + ); + var resultArr = []; + //let isFirstTime = true; + for (let i = 0; i < psw.length; i++) { + if (i === psw.indexOf(psw[i]) && pswRegex.test(psw[i])) { + //console.log(psw[i]) + //console.log(psw[i+i]) + resultArr.push(true); + } else { + //console.log(psw[i]) + resultArr.push(false); + } + } + console.log(resultArr); + return resultArr; +} -function validatePasswords(passwords) { +// function validatePasswords(passwords) { +// passwords.every(pswValidator) -} +// } /* ======= 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!"]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; - - for (let i = 0; i < a.length; ++i) { - if (a[i] !== b[i]) return false; - } - - return true; + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } - - console.log(`${test_name}: ${status}`); + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } + + console.log(`${test_name}: ${status}`); } test( - "validatePasswords function works - case 1", - arraysEqual( - validatePasswords(passwords1), [false, false, true, false, false] - ) - ); - - test( - "validatePasswords function works - case 2", - arraysEqual( - validatePasswords(passwords2), [true, true, false, false, false] - ) - ); + "validatePasswords function works - case 1", + arraysEqual(validatePasswords(passwords1), [false, false, true, false, false]) +); + +test( + "validatePasswords function works - case 2", + arraysEqual(validatePasswords(passwords2), [true, true, false, false, false]) +);