diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a070426 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // 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-1\\2-mandatory\\3-function-output.js" + } + ] +} \ No newline at end of file diff --git a/week-1/1-exercises/B-hello-world/exercise.js b/week-1/1-exercises/B-hello-world/exercise.js index b179ee9..af90bfd 100644 --- a/week-1/1-exercises/B-hello-world/exercise.js +++ b/week-1/1-exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ +var name = "Hello world!"; console.log("Hello world"); diff --git a/week-1/1-exercises/C-variables/exercise.js b/week-1/1-exercises/C-variables/exercise.js index a6bbb97..980ecbd 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..b9433fe 100644 --- a/week-1/1-exercises/D-strings/exercise.js +++ b/week-1/1-exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` +var message = "This is a string"; +var messageType = typeof message; -console.log(message); +console.log(messageType); diff --git a/week-1/1-exercises/E-strings-concatenation/exercise.js b/week-1/1-exercises/E-strings-concatenation/exercise.js index 2cffa6a..e4483c3 100644 --- a/week-1/1-exercises/E-strings-concatenation/exercise.js +++ b/week-1/1-exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `message` +var greetingStart = "Hello, my name is "; +var name = "Nihal"; + +var greeting = greetingStart + name; + +console.log(greeting); -console.log(message); diff --git a/week-1/1-exercises/F-strings-methods/exercise.js b/week-1/1-exercises/F-strings-methods/exercise.js index 2cffa6a..5011ff9 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 = "Daniel"; +var nameLength = name.length; + +console.log(nameLength); -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..87c3ae2 100644 --- a/week-1/1-exercises/F-strings-methods/exercise2.js +++ b/week-1/1-exercises/F-strings-methods/exercise2.js @@ -1,3 +1,4 @@ -const name = " Daniel "; +var name = "Daniel"; +var nameLowerCase = name.toLowerCase(); -console.log(message); +console.log(nameLowerCase); \ No newline at end of file diff --git a/week-1/1-exercises/G-numbers/exercise.js b/week-1/1-exercises/G-numbers/exercise.js index 49e7bc0..c69e7c9 100644 --- a/week-1/1-exercises/G-numbers/exercise.js +++ b/week-1/1-exercises/G-numbers/exercise.js @@ -1 +1,4 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudent = 15; +var numberOfMentors = 8; +var sum = numberOfStudent + numberOfMentors; \ No newline at end of file diff --git a/week-1/1-exercises/I-floats/exercise.js b/week-1/1-exercises/I-floats/exercise.js index a5bbcd8..9aaae41 100644 --- a/week-1/1-exercises/I-floats/exercise.js +++ b/week-1/1-exercises/I-floats/exercise.js @@ -1,2 +1,4 @@ var numberOfStudents = 15; -var numberOfMentors = 8; +var numberOfStudents = 8; +var total = numberOfStudents + numberOfStudents; +var percentagOfStudents = Math.round((numberOfStudents/total)*100); \ No newline at end of file diff --git a/week-1/1-exercises/K-functions-parameters/exercise.js b/week-1/1-exercises/K-functions-parameters/exercise.js index 8d5db5e..ccc3641 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise.js +++ b/week-1/1-exercises/K-functions-parameters/exercise.js @@ -1,6 +1,6 @@ // Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it +function multiply(a, b) { + return a*b; } // 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..8b6ae2a 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise2.js +++ b/week-1/1-exercises/K-functions-parameters/exercise2.js @@ -1,4 +1,6 @@ -// Declare your function first +function divide(a, b) { + return a/b; +} var result = divide(3, 4); diff --git a/week-1/1-exercises/K-functions-parameters/exercise3.js b/week-1/1-exercises/K-functions-parameters/exercise3.js index 537e9f4..0a724ea 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise3.js +++ b/week-1/1-exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,7 @@ -// Write your function here +function createGreeting(num1, num2) { + return num1 + num2; +} -var greeting = createGreeting("Daniel"); +var greeting = createGreeting("Hello, my name is ", "Nihal."); console.log(greeting); diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..415720b 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -2,15 +2,15 @@ // 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"; +return 'Hello, my name is ' + name + 'and I am '+ age + 'years old'; function getAddition(a, b) { - total = a ++ b + total = a + b // Use string interpolation here return "The total is %{total}" diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..43da8cf 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // 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 ===== @@ -22,14 +21,23 @@ To run these tests type `node 2-logic-error` 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("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture") -test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25) -test("fixed multiply function", multiply(2,3,6) === 36) \ No newline at end of file +test( + "fixed trimWord function", + trimWord(" CodeYourFuture ") === "CodeYourFuture" + ); +test( + "fixed wordLength function", + getWordLength("A wild sentence appeared!") === 25 + ); +test( + "fixed multiply function", + multiply(2,3,6) === 36 + ); \ No newline at end of file diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index bbb88a2..3956bdf 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,9 +1,14 @@ // Add comments to explain what this function does. You're meant to use Google! + +//returen random number between 0 ans 9.99 function getNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! + +// this function takes one array and merges into another +// if first parameter w1 is a string, then it will do just a string concatenation function s(w1, w2) { return w1.concat(w2); } @@ -11,6 +16,7 @@ function s(w1, w2) { 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 ===== @@ -22,9 +28,9 @@ To run these tests type `node 3-function-output` 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}`); @@ -32,13 +38,13 @@ function test(test_name, expr) { test( "concatenate function - case 1 works", - concatenate("code", "your", "future") === "code your future" + concatenate('code', 'your', 'future') === 'code your future' ); test( "concatenate function - case 2 works", - concatenate("I", "like", "pizza") === "I like pizza" + concatenate('I', 'like', 'pizza') === 'I like pizza' ); test( "concatenate function - case 3 works", - concatenate("I", "am", 13) === "I am 13" + concatenate('I', 'am', 13) === 'I am 13' ); diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 6b84208..cbd343e 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -5,7 +5,10 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + const tax = price * 0.2; + const totalPrice = tax + price; + return totalPrice; /* CURRENCY FORMATTING @@ -28,9 +31,9 @@ To run these tests type `node 4-tax.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}`); @@ -51,4 +54,6 @@ test( "formatCurrency function - case 2 works", formatCurrency(17.5) === "£21.00" ); -test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80"); +test( + "formatCurrency function - case 3 works", formatCurrency(34) + === "£40.80"); diff --git a/week-2/1-exercises/B-boolean-literals/exercise.js b/week-2/1-exercises/B-boolean-literals/exercise.js index 6c5060f..6bc05c6 100644 --- a/week-2/1-exercises/B-boolean-literals/exercise.js +++ b/week-2/1-exercises/B-boolean-literals/exercise.js @@ -5,12 +5,14 @@ Add the required variables with the correct boolean values assigned. */ -var codeYourFutureIsGreat = true; - -/* - DO NOT EDIT BELOW THIS LINE - --------------------------- */ +let codeYourFutureIsGreat = true; +let mozafarIsCool = false; +let calculationCorrect = true; +let moreThan10Students = false; + /* DO NOT EDIT BELOW THIS LINE + --------------------------- +*/ console.log("Is Code Your Future great?", codeYourFutureIsGreat); console.log("Is Mozafar cool?", mozafarIsCool); console.log("Does 1 + 1 = 2?", calculationCorrect); diff --git a/week-2/1-exercises/C-comparison-operators/exercise.js b/week-2/1-exercises/C-comparison-operators/exercise.js index 58aee1c..5086189 100644 --- a/week-2/1-exercises/C-comparison-operators/exercise.js +++ b/week-2/1-exercises/C-comparison-operators/exercise.js @@ -7,14 +7,15 @@ var studentCount = 16; var mentorCount = 9; -var moreStudentsThanMentors; // finish this statement +var sum = studentCount + mentorCount; +var moreStudentsThanMentors = studentCount > mentorCount; // finish this statement var roomMaxCapacity = 25; -var enoughSpaceInRoom; // finish this statement +var enoughSpaceInRoom = roomMaxCapacity === sum; // finish this statement var personA = "Daniel"; var personB = "Irina"; -var sameName; // finish this statement +var sameName = personA === personB; // finish this statement /* 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..1b3df09 100644 --- a/week-2/1-exercises/D-predicates/exercise.js +++ b/week-2/1-exercises/D-predicates/exercise.js @@ -7,12 +7,13 @@ // Finish the predicate function to test if the passed number is negative (less than zero) function isNegative(number) { + return number > 0; } // Finish the predicate function to test if the passed number is between 0 and 10 function isBetweenZeroAnd10(number) { - +return number > 0 && number < 10; } /* diff --git a/week-2/1-exercises/E-conditionals/exercise.js b/week-2/1-exercises/E-conditionals/exercise.js index acbaaa8..363c612 100644 --- a/week-2/1-exercises/E-conditionals/exercise.js +++ b/week-2/1-exercises/E-conditionals/exercise.js @@ -8,6 +8,12 @@ var name = "Daniel"; var danielsRole = "mentor"; +if (danielsRole === `mentor`) { + 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/1-exercises/F-logical-operators/exercise.js b/week-2/1-exercises/F-logical-operators/exercise.js index a8f2945..90469ca 100644 --- a/week-2/1-exercises/F-logical-operators/exercise.js +++ b/week-2/1-exercises/F-logical-operators/exercise.js @@ -11,14 +11,14 @@ var cssLevel = 4; // Finish the statement to check whether HTML, CSS knowledge are above 5 // (hint: use the comparison operator from before) -var htmlLevelAbove5; -var cssLevelAbove5; +var htmlLevelAbove5 = htmlLevel > 5; +var cssLevelAbove5 = cssLevel > 5; // Finish the next two statement // Use the previous variables and logical operators // Do not "hardcode" the answers -var cssAndHtmlAbove5; -var cssOrHtmlAbove5; +var cssAndHtmlAbove5 = htmlLevelAbove5 && cssLevelAbove5; +var cssOrHtmlAbove5 = htmlLevelAbove5 || cssLevelAbove5; /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/F-logical-operators/exercise2.js b/week-2/1-exercises/F-logical-operators/exercise2.js index 6f4199c..96bc021 100644 --- a/week-2/1-exercises/F-logical-operators/exercise2.js +++ b/week-2/1-exercises/F-logical-operators/exercise2.js @@ -5,7 +5,29 @@ Update the code so that you get the expected result. */ -function isNegative() {} +function isNegative(number) { +return number < 0; +} + + +function isNegative(number) { + return number < 0; + } + + + function isBetween5and10(number) { + return number >= 5 && number <= 10; + } + + + function isShortName(name) { + return name.length >= 6 && name.length <= 10; + } + + + function startsWithD(name) { + return name[0] === "D"; + } /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-1.js b/week-2/1-exercises/G-conditionals-2/exercise-1.js index 54708ef..528233c 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-1.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-1.js @@ -6,10 +6,33 @@ - if number is more or equal to zero, return the word "positive" */ -function negativeOrPositive(number) { - +function negativeOrPositive(number1) { +if (number1 < 0) { + return "negative"; +} +else { + return "positive"; +} } +function negativeOrPositive(number2) { + if (number2 < 0) { + return "negative"; + } + else { + return "positive"; + } + } + function negativeOrPositive(number3) { + if (number3 < 0) { + return "negative"; + } + else { + return "positive"; + } + } + + /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/1-exercises/G-conditionals-2/exercise-2.js b/week-2/1-exercises/G-conditionals-2/exercise-2.js index 313f3fb..2f9fee9 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-2.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-2.js @@ -8,9 +8,47 @@ */ function studentPassed(grade) { + if (grade < 50) { + return "failed"; + } +else { + return "passed"; +} +} + + +/* +function studentPassed(grade1) { + if (grade1 < 50) { + return "failed"; + } +else { + return "passed"; +} } + + +function studentPassed(grade2) { + if (grade2 < 50) { + return "failed"; + } +else { + return "passed"; +} +} + + +function studentPassed(grade3) { + if (grade3 < 50) { + return "failed"; + } +else { + return "passed"; +} +} +*/ /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/1-exercises/G-conditionals-2/exercise-3.js b/week-2/1-exercises/G-conditionals-2/exercise-3.js index a79cf30..08d3991 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-3.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-3.js @@ -9,8 +9,85 @@ */ function calculateGrade(mark) { + if (mark >= 80) { + return "A"; + } +else if (mark < 80 && mark >60) { + return "B"; +} +else if (mark <= 60 && mark >= 50) { + return "C" +} +else { + return "F" +} +} +/* +function calculateGrade(grade1) { + if (grade1 >= 80) { + return "A"; + } +else if (grade1 < 80 && grade1 >60) { + return "B"; +} +else if (grade1 <= 60 && grade1 >= 50) { + return "C" +} +else { + return "F" +} +} + +function calculateGrade(grade2) { + if (grade2 >= 80) { + return "A"; + } +else if (grade2 < 80 && grade2 >60) { + return "B"; +} +else if (grade2 <= 60 && grade2 >= 50) { + return "C" } +else { + return "F" +} +} + + +function calculateGrade(grade3) { + if (grade3 >= 80) { + + return "A"; + } +else if (grade3 < 80 && grade3 >60) { + return "B"; +} +else if (grade3 <= 60 && grade3 >= 50) { + return "C" +} +else { + return "F" +} +} + + +function calculateGrade(grade4) { + if (grade4 >= 80) { + return "A"; + } +else if (grade4 < 80 && grade4 >60) { + return "B"; +} +else if (grade4 <= 60 && grade4 >= 50) { + return "C" +} +else { + return "F" +} +}*/ + + /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/G-conditionals-2/exercise-4.js b/week-2/1-exercises/G-conditionals-2/exercise-4.js index bd5bb1e..4a4a7d2 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-4.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-4.js @@ -10,6 +10,12 @@ function containsCode(sentence) { + if (sentence.includes("code")) { + return true; + } + else { + return false; + } } /* diff --git a/week-2/1-exercises/H-array-literals/exercise.js b/week-2/1-exercises/H-array-literals/exercise.js index d6dc556..cf961ea 100644 --- a/week-2/1-exercises/H-array-literals/exercise.js +++ b/week-2/1-exercises/H-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -var numbers = []; // add numbers from 1 to 10 into this array -var mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +var numbers = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array +var mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/I-array-properties/exercise.js b/week-2/1-exercises/I-array-properties/exercise.js index f9aec89..d05d607 100644 --- a/week-2/1-exercises/I-array-properties/exercise.js +++ b/week-2/1-exercises/I-array-properties/exercise.js @@ -6,8 +6,10 @@ */ function isEmpty(arr) { - return; // complete this statement -} + return arr.length == 0; + } + + /* DO NOT EDIT BELOW THIS LINE diff --git a/week-2/1-exercises/J-array-get-set/exercise.js b/week-2/1-exercises/J-array-get-set/exercise.js index 3b95694..18d072c 100644 --- a/week-2/1-exercises/J-array-get-set/exercise.js +++ b/week-2/1-exercises/J-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1]; // complete this statement } /* diff --git a/week-2/1-exercises/J-array-get-set/exercises2.js b/week-2/1-exercises/J-array-get-set/exercises2.js index 97f126f..962bcda 100644 --- a/week-2/1-exercises/J-array-get-set/exercises2.js +++ b/week-2/1-exercises/J-array-get-set/exercises2.js @@ -7,7 +7,8 @@ */ var numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers.push(4); +numbers[0]; /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index 6316fad..56db952 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -4,7 +4,7 @@ function mood() { let isHappy = true; - if (isHappy) { + if (isHappy === false) { return "I am happy"; } else { return "I am not happy"; @@ -13,9 +13,9 @@ function mood() { function greaterThan10() { let num = 10; - let isBigEnough; + let isBigEnough = 11; - if (isBigEnough) { + if (isBigEnough >= 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[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..9cf2ed3 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,10 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} +function tidyUpString(strArr) { + let tidyStrArr = strArr.map(item => item.trim().replace('/', '').toLowerCase()); + return tidyStrArr; +} /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +18,16 @@ Complete the function to check if the variable `num` satisfies the following req Tip: use logical operators */ -function validate(num) {} +function validate(num) { + let isNumber = typeof(num) === 'number'; + + let isEven = num % 2 == 0; + let isLessThanOrEqual100 = num <= 100; + if(isNumber && isEven && isLessThanOrEqual100) { + return true; + } + return false; +} /* Write a function that removes an element from an array @@ -26,7 +38,9 @@ The function must: */ function remove(arr, index) { - return; // complete this statement + let newArr = arr; + newArr.splice(index, 1); + return newArr; // complete this statement } /* @@ -37,9 +51,31 @@ Write a function that: - numbers greater 100 must be replaced with 100 */ +/*function formatPercentage(arr) { + let newArr = arr.map(item => { + if(item > 100){ + item = 100; + } + return `${Number(item.toFixed(2))}%`; + }) + return newArr; +}*/ + function formatPercentage(arr) { + for( let i = 0; i < arr.length; i++){ + if (arr[i] > 100){ + arr[i] = 100; + } + let j = arr[i].toFixed(2) + j = parseFloat(j); + arr[i] = j.toString() + "%"; + } + return arr; + } -} + + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..4b4bccd 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -15,6 +15,17 @@ 7. What is the value of the "a" outer variable when "f1" is called for the first time? */ +/* +ANSWERS: +1. b, d and e is not defined; +2. removed: console.log(b); +3. Will be printed: 2, 6,4,9,6,13,8; +4. f1 is called 2 times; +5. f2 is called 3 times; +6. In the first f1 call a will be equal to 1; +7. The value of the "a" outer variable when 'f1' is called fir the first time is 8; +*/ + let x = 2; let a = 6; @@ -28,13 +39,13 @@ 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; if (i % 2 === 0) { const d = f2(i, x); - console.log(d); + //console.log(d); } else { const e = f1(i, a); 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..041eff3 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -7,15 +7,36 @@ Today, you will be applying the sorting algorithm you used in that exercise in c Create a function called sortAges which: - takes an array of mixed data types as input - removes any non-number data types without using the built-in javascript filter method -- returns an array of sorted ages in ascending order +- eturnrs an array of sorted ages in ascending order - HARD MODE - without using the built-in javascript sort method 😎 You don't have to worry about making this algorithm work fast! The idea is to get you to "think" like a computer and practice your knowledge of basic JavaScript. */ -function sortAges(arr) {} +function sortAges(arr) { + //let swap; + let newArr = []; + for (let i = 0; i < arr.length; i++){ + if (typeof(arr[i]) === "number"){ + newArr.push(arr[i]); + } + } + for (i = 0; i < newArr.length; i++){ + for (let j = i+1; j < newArr.length; j++){ + if (newArr[j] < newArr[i] + ){ +swap = newArr[i]; +newArr[i] = newArr[j]; +newArr[j]= swap; + } + } + } + return newArr; +} + + /* ======= TESTS - DO NOT MODIFY ===== */ const agesCase1 = [ diff --git a/week-2/3-extra/1-radio-stations.js b/week-2/3-extra/1-radio-stations.js index 95c0e56..7948820 100644 --- a/week-2/3-extra/1-radio-stations.js +++ b/week-2/3-extra/1-radio-stations.js @@ -26,6 +26,11 @@ */ // `getStations` goes here +function getAllFrequencies(list) { + var freq = [] + +} + /* ======= 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..9194741 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -5,11 +5,16 @@ // write your code here -var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; - -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); -console.log(longNameThatStartsWithA); +function longNameThatStartsWithA(nams) { + return (nams.length > 7 && nams.charAt(0) === "A"); +} +var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; +let firstnames = names.find(longNameThatStartsWithA); +console.log(firstnames); +/*var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +} +console.log(); /* EXPECTED OUTPUT */ -// "Alexandra" +// "Alexandra"*/ diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..119abea 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -12,12 +12,17 @@ var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; // 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) { + 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/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..d809683 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -1,18 +1,43 @@ /* + This program should check if the array `group` contains only students + */ + + var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; + var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement + // complete this statement + var groupIsOnlyStudents = group.every(students); + +function studentArrey(arr) { + + + return studentArrey(groupIsOnlyStudents); +} if (groupIsOnlyStudents) { + console.log("The group contains only students"); + } else { + console.log("The group does not contain only students"); + } + + /* EXPECTED RESULT */ + + // The group does not contain only students + + +// var studentNameLength = students.every(isAboveThreshold); + +// console.log(studentNameLength); // logs true \ No newline at end of file diff --git a/week-3/2-mandatory/1-oxygen-levels.js b/week-3/2-mandatory/1-oxygen-levels.js index 3c02135..35de4ed 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,9 +9,16 @@ 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(oxygen) { + let safety = oxygen.find(oxygen =>{ + if (oxygen > `19.5%` && oxygen < `23.5%`){ + return oxygen; + } + }) + return safety; + } + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..e3a5f1f 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,8 +10,12 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(arr) { + let berry = arr.every(x => x==="pink"); + if (berry === true) { + 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..a5dbc2e 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,7 +8,12 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { + + + +function colonisers(arr) { + let family = arr.filter(x=> x.length > 9 && x.charAt(0) === "A"); + return family; } diff --git a/week-3/2-mandatory/4-eligible-students.js b/week-3/2-mandatory/4-eligible-students.js index 6424b01..b49e468 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,10 +7,23 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { + +function eligibleStudents(arr) { + let result = arr.filter(checkTheAtendace); + function checkTheAtendace(students) { + return students[1] >= 8; + } +console.log(result); + let getStudent = result.map(bringNames); + function bringNames(students) { + return students[0]; + } +console.log(getStudent); +return getStudent; } + /* ======= TESTS - DO NOT MODIFY ===== */ const attendances = [ diff --git a/week-3/2-mandatory/5-journey-planner.js b/week-3/2-mandatory/5-journey-planner.js index 53499c3..88c4f6a 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,8 +7,16 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { + +function journeyPlanner(location, transport) { + let avilabelLocations = []; +for (i = 0; i < location.length; i++) { + if (location[i].includes(transport)) { + avilabelLocations.push(location[i][0]); + } +} +return avilabelLocations; } /* ======= 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..6c1c5d8 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,10 +4,22 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { + + +function getLanes(arr) { + let street = []; //blank array to push the new values in + let lane = arr.map(element => element.indexOf("Lane")); + for(let i = 0; i < arr.length; i++) { + if (lane[i] !== -1) { + street.push(arr[i]); + } + } + return street; } + + /* ======= 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 57b3d53..40a41eb 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -22,9 +22,39 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) { +// function validatePasswords(passwords) { + +// } +function fiveOrMoreLetters(word) { + return word.length >= 5; +} +function checkUpperCase(word) { +let regex = /[A-Z]/; +return regex.test(word); +} +function checkLowerCase(word) { +let regex = /[a-z]/; +return regex.test(word); +} +function checkNumbers(word) { +let regex = /[0-9]/; +return regex.test(word); } +function checkSymbols(word) { +let regex = /[!#$%.*&]/; +return regex.test(word); +} +// function containsRepeat(word) { +// } +function validatePasswords(passwords) { +return passwords.map(word => fiveOrMoreLetters(word) +&& checkUpperCase(word) +&& checkLowerCase(word) +&& checkNumbers(word) +&& checkSymbols(word)); +} + /* ======= TESTS - DO NOT MODIFY ===== */