From 5e0a5fbe5973825cea59c6670f0076142594506a Mon Sep 17 00:00:00 2001 From: Osman Date: Fri, 19 Jun 2020 16:33:00 +0100 Subject: [PATCH 1/9] First commit Commiting changes for the homework, exercise and some of the extras. --- week-1/1-exercises/B-hello-world/exercise.js | 4 +- week-1/1-exercises/C-variables/exercise.js | 4 +- week-1/1-exercises/D-strings/exercise.js | 3 +- .../E-strings-concatenation/exercise.js | 6 ++- .../1-exercises/F-strings-methods/exercise.js | 7 ++- .../F-strings-methods/exercise2.js | 13 +++-- week-1/1-exercises/G-numbers/exercise.js | 6 +++ week-1/1-exercises/I-floats/exercise.js | 5 ++ week-1/1-exercises/J-functions/exercise.js | 11 ++-- week-1/1-exercises/J-functions/exercise2.js | 1 + .../K-functions-parameters/exercise.js | 5 +- .../K-functions-parameters/exercise2.js | 4 +- .../K-functions-parameters/exercise3.js | 4 +- .../K-functions-parameters/exercise4.js | 6 ++- .../K-functions-parameters/exercise5.js | 4 +- .../L-functions-nested/exercise.js | 7 +++ week-1/2-mandatory/1-syntax-errors.js | 51 ++++++++++--------- week-1/2-mandatory/2-logic-error.js | 35 +++++++------ week-1/2-mandatory/3-function-output.js | 39 +++++++------- week-1/2-mandatory/4-tax.js | 38 ++++++++------ week-1/3-extra/1-currency-conversion.js | 25 +++++---- week-1/3-extra/2-piping.js | 46 ++++++++--------- 22 files changed, 200 insertions(+), 124 deletions(-) diff --git a/week-1/1-exercises/B-hello-world/exercise.js b/week-1/1-exercises/B-hello-world/exercise.js index b179ee9..ab3aedc 100644 --- a/week-1/1-exercises/B-hello-world/exercise.js +++ b/week-1/1-exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ -console.log("Hello world"); +console.log("Hello world. I just started learning JavaScript!."); +console.log("My name is Osman."); +console.log("I'm " + 27 + " years old."); \ No newline at end of file diff --git a/week-1/1-exercises/C-variables/exercise.js b/week-1/1-exercises/C-variables/exercise.js index a6bbb97..f7d3dfc 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` - +let greeting = "Hello world"; console.log(greeting); +console.log(greeting); +console.log(greeting); \ No newline at end of file diff --git a/week-1/1-exercises/D-strings/exercise.js b/week-1/1-exercises/D-strings/exercise.js index 2cffa6a..f3bb9ed 100644 --- a/week-1/1-exercises/D-strings/exercise.js +++ b/week-1/1-exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - +let message = "This is a string"; console.log(message); +console.log(typeof 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..df58a86 100644 --- a/week-1/1-exercises/E-strings-concatenation/exercise.js +++ b/week-1/1-exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - -console.log(message); +let name = "Osman."; +let greeting = "Hello, my name is "; +let introduction = greeting + name; +console.log(introduction); diff --git a/week-1/1-exercises/F-strings-methods/exercise.js b/week-1/1-exercises/F-strings-methods/exercise.js index 2cffa6a..4a5c0b1 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` - -console.log(message); +let name = "Osman"; +let nameLength = name.length; +console.log( + "My name is " + name + "and my name is " + nameLength + " characters long" +); diff --git a/week-1/1-exercises/F-strings-methods/exercise2.js b/week-1/1-exercises/F-strings-methods/exercise2.js index b4b4694..87cdadb 100644 --- a/week-1/1-exercises/F-strings-methods/exercise2.js +++ b/week-1/1-exercises/F-strings-methods/exercise2.js @@ -1,3 +1,10 @@ -const name = " Daniel "; - -console.log(message); +let name = " Daniel "; +let nameTrim = name.trim(); +let lengthOfName = nameTrim.length; +console.log( + "My name is " + + name.trim() + + " and my name is " + + lengthOfName + + " characters long." +); diff --git a/week-1/1-exercises/G-numbers/exercise.js b/week-1/1-exercises/G-numbers/exercise.js index 49e7bc0..3020e49 100644 --- a/week-1/1-exercises/G-numbers/exercise.js +++ b/week-1/1-exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 15; +let numberOfMentors = 8; +let sum = numberOfStudents + numberOfMentors; +console.log("Number of students: " + numberOfStudents); +console.log("Number of mentors: " + numberOfMentors); +console.log("Total number 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..46cc094 100644 --- a/week-1/1-exercises/I-floats/exercise.js +++ b/week-1/1-exercises/I-floats/exercise.js @@ -1,2 +1,7 @@ var numberOfStudents = 15; var numberOfMentors = 8; +let sum = numberOfStudents + numberOfMentors; +let studentsPercentage = Math.round((numberOfStudents / sum) * 100); +let mentorsPercentage = Math.round((numberOfMentors / sum) * 100); +console.log("Percentage students: " + studentsPercentage + "%"); +console.log("Percentage mentors: " + mentorsPercentage + "%"); diff --git a/week-1/1-exercises/J-functions/exercise.js b/week-1/1-exercises/J-functions/exercise.js index 0ae5850..fdaaf8b 100644 --- a/week-1/1-exercises/J-functions/exercise.js +++ b/week-1/1-exercises/J-functions/exercise.js @@ -1,7 +1,12 @@ function halve(number) { - // complete the function here + // complete the function here + return number / 2; } - +var divide = halve(65); var result = halve(12); - +var num = halve(109); +var output = halve(34); +console.log(divide); console.log(result); +console.log(num); +console.log(output); 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..f66c880 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() { - // Calculate the result of the function and return it +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..ecab367 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise2.js +++ b/week-1/1-exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,7 @@ // Declare your function first - +function divide(num1, num2) { + return num1 / num2; +} 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..71671e9 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(name) { + return "Hello, my name is " + name; +} var greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/week-1/1-exercises/K-functions-parameters/exercise4.js b/week-1/1-exercises/K-functions-parameters/exercise4.js index 7ab4458..cb1e137 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise4.js +++ b/week-1/1-exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function add(num1, num2){ + return num1 + num2; +} // Call the function and assign to a variable `sum` - +let sum = add(13, 124); console.log(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..047a600 100644 --- a/week-1/1-exercises/K-functions-parameters/exercise5.js +++ b/week-1/1-exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,7 @@ // 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); console.log(greeting); diff --git a/week-1/1-exercises/L-functions-nested/exercise.js b/week-1/1-exercises/L-functions-nested/exercise.js index a5d3774..584b602 100644 --- a/week-1/1-exercises/L-functions-nested/exercise.js +++ b/week-1/1-exercises/L-functions-nested/exercise.js @@ -1,3 +1,10 @@ +let studentsNumber = 15; +let mentorsNumber = 8; +let sum = studentsNumber + mentorsNumber; +function percentage(prcntg){ + return Math.round(prcntg / sum * 100); +} +function getPrcentage() var mentor1 = "Daniel"; var mentor2 = "Irina"; var mentor3 = "Mimi"; diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..018e6f4 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,36 +1,41 @@ - - // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { - return 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 getAddition(a, b) { - total = a ++ b +function introduceMe(name, age) { + return "Hello, my name is " + name + " and I am " + age + " years old"; +} +function getRemainder(a, b) { + let total = a % b; - // Use string interpolation here - return "The total is %{total}" + // Use string interpolation here + return `The remainder is ${total}`; } /* ======= TESTS - DO NOT MODIFY ===== */ -// +// // To run these tests type `node 1-syntax-errors.js` into your terminal 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("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) -test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old") -test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3") +test("fixed addNumbers function - case 1", addNumbers(3, 4, 6) === 13); +test( + "fixed introduceMe function", + introduceMe("Sonjide", 27) === + "Hello, my name is Sonjide and I am 27 years old" +); +test( + "fixed getRemainder function", + getRemainder(23, 5) === "The remainder is 3" +); diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..82d8e88 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 ===== @@ -20,16 +19,22 @@ To run these tests type `node 2-logic-error` into your terminal */ 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("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); diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index bbb88a2..d8a56b9 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,16 +1,21 @@ // Add comments to explain what this function does. You're meant to use Google! +// a function with no parameters passed. The function is to multiply the given value with 10. function getNumber() { - return Math.random() * 10; + // It will return numbers from 0 to 9 multiplied by 10. + return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// This is a function with 2 parameters to concatenate 2 values function s(w1, w2) { - return w1.concat(w2); + // The value of (w2) will be added to after the value of (w1); + return w1.concat(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 + // Write the body of this function to concatenate three words together + return firstWord.concat(" ", secondWord, " ", thirdWord); + // Look at the test case below to understand what to expect in return } /* ======= TESTS - DO NOT MODIFY ===== @@ -20,25 +25,25 @@ To run these tests type `node 3-function-output` into your terminal */ function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } - console.log(`${test_name}: ${status}`); + console.log(`${test_name}: ${status}`); } test( - "concatenate function - case 1 works", - concatenate("code", "your", "future") === "code your future" + "concatenate function - case 1 works", + concatenate("code", "your", "future") === "code your future" ); test( - "concatenate function - case 2 works", - concatenate("I", "like", "pizza") === "I like pizza" + "concatenate function - case 2 works", + concatenate("I", "like", "pizza") === "I like pizza" ); test( - "concatenate function - case 3 works", - concatenate("I", "am", 13) === "I am 13" + "concatenate function - case 3 works", + 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..3e50b1a 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(priceUnit) { + let taxedPrice = priceUnit + (priceUnit * 1) / 5; + return taxedPrice; +} /* CURRENCY FORMATTING @@ -17,7 +20,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function formatCurrency() {} +function formatCurrency(priceFormat) { + let formatedPrice = calculateSalesTax(priceFormat); + return "£" + formatedPrice.toFixed(2); +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -26,29 +32,29 @@ To run these tests type `node 4-tax.js` into your terminal */ 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("calculateSalesTax function - case 1 works", calculateSalesTax(15) === 18); test( - "calculateSalesTax function - case 2 works", - calculateSalesTax(17.5) === 21 + "calculateSalesTax function - case 2 works", + calculateSalesTax(17.5) === 21 ); test( - "calculateSalesTax function - case 3 works", - calculateSalesTax(34) === 40.8 + "calculateSalesTax function - case 3 works", + calculateSalesTax(34) === 40.8 ); test("formatCurrency function - case 1 works", formatCurrency(15) === "£18.00"); test( - "formatCurrency function - case 2 works", - formatCurrency(17.5) === "£21.00" + "formatCurrency function - case 2 works", + formatCurrency(17.5) === "£21.00" ); test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80"); diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 7f321d9..fee10ed 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,9 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(priceToConvert) { + return priceToConvert * 1.4; +} /* CURRENCY FORMATTING @@ -16,7 +18,10 @@ function convertToUSD() {} Find a way to add 1% to all currency conversions (think about the DRY principle) */ -function convertToBRL() {} +function convertToBRL(priceConver) { + let convertedPrice = priceConver * 5.7; + return convertedPrice + convertedPrice * 0.01; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -25,14 +30,14 @@ To run these tests type `node 1-currency-conversion` into your terminal */ 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("convertToUSD function works", convertToUSD(32) === 44.8); diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..d340dd5 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,26 @@ the final result to the variable goodCode */ -function add() { - +function add(a, b) { + return a + b; } -function multiply() { - +function multiply(a, b) { + return a * b; } -function format() { - +function format(a) { + return "£" + a; } -const startingValue = 2 +let startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = "£" + (startingValue + 10) * 2; /* BETTER PRACTICE */ -let goodCode = +let goodCode = badCode; /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -44,19 +44,19 @@ To run these tests type `node 2-piping.js` into your terminal */ 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('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"); From db1f51cf371cb74ff67ada260157a70e5c16edd5 Mon Sep 17 00:00:00 2001 From: Osman Date: Sat, 20 Jun 2020 17:24:03 +0100 Subject: [PATCH 2/9] Making commit Commiting my answers to the exercises --- week-2/1-exercises/B-boolean-literals/exercise.js | 4 +++- week-2/1-exercises/C-comparison-operators/exercise.js | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/week-2/1-exercises/B-boolean-literals/exercise.js b/week-2/1-exercises/B-boolean-literals/exercise.js index 6c5060f..4db415c 100644 --- a/week-2/1-exercises/B-boolean-literals/exercise.js +++ b/week-2/1-exercises/B-boolean-literals/exercise.js @@ -6,7 +6,9 @@ */ var codeYourFutureIsGreat = true; - +let mozafarIsCool = false; +let calculationCorrect = true; +let moreThan10Students = false; /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/week-2/1-exercises/C-comparison-operators/exercise.js b/week-2/1-exercises/C-comparison-operators/exercise.js index 58aee1c..f17bcb0 100644 --- a/week-2/1-exercises/C-comparison-operators/exercise.js +++ b/week-2/1-exercises/C-comparison-operators/exercise.js @@ -7,22 +7,22 @@ var studentCount = 16; var mentorCount = 9; -var moreStudentsThanMentors; // finish this statement +var moreStudentsThanMentors = studentCount > mentorCount; // finish this statement var roomMaxCapacity = 25; -var enoughSpaceInRoom; // finish this statement +var enoughSpaceInRoom = roomMaxCapacity >= moreStudentsThanMentors; // 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 --------------------------- */ console.log("Are there more students than mentors?", moreStudentsThanMentors); console.log( - "Is there enough space in the room for all students and mentors?", - enoughSpaceInRoom + "Is there enough space in the room for all students and mentors?", + enoughSpaceInRoom ); console.log("Do person A and person B have the the same name?", sameName); From c3d43841bc0c367a6e035780c9ba4fbd8c6408f8 Mon Sep 17 00:00:00 2001 From: Osman Date: Sat, 20 Jun 2020 22:21:19 +0100 Subject: [PATCH 3/9] comitting exercises comitting answers to exercises --- week-2/1-exercises/D-predicates/exercise.js | 5 ++--- week-2/1-exercises/E-conditionals/exercise.js | 5 +++++ week-2/1-exercises/F-logical-operators/exercise.js | 9 +++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/week-2/1-exercises/D-predicates/exercise.js b/week-2/1-exercises/D-predicates/exercise.js index f600521..924f1ff 100644 --- a/week-2/1-exercises/D-predicates/exercise.js +++ b/week-2/1-exercises/D-predicates/exercise.js @@ -7,12 +7,12 @@ // 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; } /* @@ -32,4 +32,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/1-exercises/E-conditionals/exercise.js b/week-2/1-exercises/E-conditionals/exercise.js index acbaaa8..b8efa59 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 (danielsRole) { + console.log(`Hi, I'm ${name} , I'm a ${danielsRole}`); +} else { + console.log(`Hi, I'm ${name} , 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..03dc4a9 100644 --- a/week-2/1-exercises/F-logical-operators/exercise.js +++ b/week-2/1-exercises/F-logical-operators/exercise.js @@ -11,8 +11,8 @@ 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 = 5; +var cssLevelAbove5 = 5; // Finish the next two statement // Use the previous variables and logical operators @@ -27,10 +27,7 @@ var cssOrHtmlAbove5; console.log("Is Html knowledge above 5?", htmlLevelAbove5); console.log("Is CSS knowledge above 5?", cssLevelAbove5); console.log("Is Html And CSS knowledge above 5?", cssAndHtmlAbove5); -console.log( - "Is either Html or CSS knowledge above 5?", - cssOrHtmlAbove5 -); +console.log("Is either Html or CSS knowledge above 5?", cssOrHtmlAbove5); /* EXPECTED RESULT From 42fb0fff200f158d289815ea5503b2f30820c763 Mon Sep 17 00:00:00 2001 From: Osman Date: Sun, 21 Jun 2020 00:10:47 +0100 Subject: [PATCH 4/9] commit more commit to exercise and mandatory --- .../F-logical-operators/exercise.js | 8 +- week-2/2-mandatory/1-fix-functions.js | 97 ++++++++++--------- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/week-2/1-exercises/F-logical-operators/exercise.js b/week-2/1-exercises/F-logical-operators/exercise.js index 03dc4a9..3ee8418 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 = 5; -var cssLevelAbove5 = 5; +var htmlLevelAbove5 = true; +var cssLevelAbove5 = false; // Finish the next two statement // Use the previous variables and logical operators // Do not "hardcode" the answers -var cssAndHtmlAbove5; -var cssOrHtmlAbove5; +var cssAndHtmlAbove5 = false; +var cssOrHtmlAbove5 = true; /* 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..f29b1fc 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -2,89 +2,90 @@ // Look at the tests and see how you can fix them. function mood() { - let isHappy = true; + let isHappy = false; - if (isHappy) { - return "I am happy"; - } else { - return "I am not happy"; - } + if (isHappy) { + return "I am happy"; + } else { + return "I am not happy"; + } } function greaterThan10() { - let num = 10; - let isBigEnough; - - if (isBigEnough) { - return "num is greater than or equal to 10"; - } else { - return "num is not big enough"; - } + let num = 10; + let isBigEnough = true; + + if (isBigEnough) { + return "num is greater than or equal to 10"; + } else { + return "num is not big enough"; + } } -function sortArray() { - let letters = ["a", "n", "c", "e", "z", "f"]; - let sortedLetters; +function sortArray(a, b) { + let letters = ["a", "n", "c", "e", "z", "f"]; - return sortedLetters; + let sortedLetters = letters.sort(a, b); + + return sortedLetters; } function first5() { - let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; - let sliced; + let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; + let sliced = numbers.slice(0, 5); - return sliced; + return sliced; } function get3rdIndex(arr) { - let index = 3; - let element; + let index = 3; + let element = arr[index]; - return element; + return element; } /* ======= TESTS - DO NOT MODIFY ===== */ 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}`); } function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + 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; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } test("mood function works", mood() === "I am not happy"); test( - "greaterThanTen function works", - greaterThan10() === "num is greater than or equal to 10" + "greaterThanTen function works", + greaterThan10() === "num is greater than or equal to 10" ); test( - "sortArray function works", - arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"]) + "sortArray function works", + arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"]) ); test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5])); test( - "get3rdIndex function works - case 1", - get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) === - "strawberry" + "get3rdIndex function works - case 1", + get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) === + "strawberry" ); test( - "get3rdIndex function works - case 2", - get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18 + "get3rdIndex function works - case 2", + get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18 ); From d5e52adf1b39d6ddeb44ea02278dc44070526b79 Mon Sep 17 00:00:00 2001 From: Osman Date: Mon, 22 Jun 2020 23:44:26 +0100 Subject: [PATCH 5/9] Exercise commits --- .../F-logical-operators/exercise2.js | 30 +++++++++++++++++-- .../G-conditionals-2/exercise-1.js | 6 +++- .../G-conditionals-2/exercise-2.js | 12 +++++--- .../G-conditionals-2/exercise-3.js | 12 ++++++-- .../G-conditionals-2/exercise-4.js | 14 +++++---- .../1-exercises/H-array-literals/exercise.js | 4 +-- .../I-array-properties/exercise.js | 7 ++++- .../1-exercises/J-array-get-set/exercise.js | 4 +-- .../1-exercises/J-array-get-set/exercises2.js | 2 +- 9 files changed, 71 insertions(+), 20 deletions(-) diff --git a/week-2/1-exercises/F-logical-operators/exercise2.js b/week-2/1-exercises/F-logical-operators/exercise2.js index 6f4199c..2213c43 100644 --- a/week-2/1-exercises/F-logical-operators/exercise2.js +++ b/week-2/1-exercises/F-logical-operators/exercise2.js @@ -5,8 +5,34 @@ Update the code so that you get the expected result. */ -function isNegative() {} - +function isNegative(num) { + if (num < 0) { + return true; + } else { + return false; + } +} +function isBetween5and10(num) { + if (num >= 5 && num <= 10) { + return true; + } else { + return false; + } +} +function isShortName(name) { + if (name.length < 10) { + return true; + } else { + return false; + } +} +function startsWithD(name) { + if (name[0] === "D") { + return "Yes"; + } else { + return "No"; + } +} /* 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..f3a4cbe 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-1.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-1.js @@ -7,7 +7,11 @@ */ function negativeOrPositive(number) { - + if (number < 0) { + return "negative"; + } else if (number >= 0) { + return "positive"; + } } /* 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..3650e85 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-2.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-2.js @@ -8,7 +8,11 @@ */ function studentPassed(grade) { - + if (grade < 50) { + return "failed"; + } else { + return "passed"; + } } /* @@ -18,9 +22,9 @@ var grade1 = 49; var grade2 = 50; var grade3 = 100; -console.log("'" + grade1 + "': " + studentPassed(grade1)) -console.log("'" + grade2 + "': " + studentPassed(grade2)) -console.log("'" + grade3 + "': " + studentPassed(grade3)) +console.log("'" + grade1 + "': " + studentPassed(grade1)); +console.log("'" + grade2 + "': " + studentPassed(grade2)); +console.log("'" + grade3 + "': " + studentPassed(grade3)); /* EXPECTED RESULT 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..ff5373c 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-3.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-3.js @@ -9,7 +9,15 @@ */ 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"; + } } /* @@ -25,7 +33,7 @@ console.log("'" + grade2 + "': " + calculateGrade(grade2)); console.log("'" + grade3 + "': " + calculateGrade(grade3)); console.log("'" + grade4 + "': " + calculateGrade(grade4)); - /* +/* EXPECTED RESULT --------------- '49': F 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..439c6cc 100644 --- a/week-2/1-exercises/G-conditionals-2/exercise-4.js +++ b/week-2/1-exercises/G-conditionals-2/exercise-4.js @@ -9,7 +9,11 @@ */ function containsCode(sentence) { - + if (sentence.includes("code")) { + return true; + } else { + return false; + } } /* @@ -19,11 +23,11 @@ var sentence1 = "code your future"; var sentence2 = "draw your future"; var sentence3 = "design your future"; -console.log("'" + sentence1 + "': " + containsCode(sentence1)) -console.log("'" + sentence2 + "': " + containsCode(sentence2)) -console.log("'" + sentence3 + "': " + containsCode(sentence3)) +console.log("'" + sentence1 + "': " + containsCode(sentence1)); +console.log("'" + sentence2 + "': " + containsCode(sentence2)); +console.log("'" + sentence3 + "': " + containsCode(sentence3)); - /* +/* EXPECTED RESULT --------------- 'code your future': true diff --git a/week-2/1-exercises/H-array-literals/exercise.js b/week-2/1-exercises/H-array-literals/exercise.js index d6dc556..9826af8 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, 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..5ed1b2c 100644 --- a/week-2/1-exercises/I-array-properties/exercise.js +++ b/week-2/1-exercises/I-array-properties/exercise.js @@ -6,7 +6,12 @@ */ function isEmpty(arr) { - return; // complete this statement + if (arr.length === 0) { + return true; + } else { + return false; + } + // complete this statement } /* 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..41ec66f 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..79dc14e 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,7 @@ */ var numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers.push(4); /* DO NOT EDIT BELOW THIS LINE --------------------------- */ From bd97b59acc04d45ab9be94a7fc6c315fea728649 Mon Sep 17 00:00:00 2001 From: Osman Date: Fri, 26 Jun 2020 23:12:41 +0100 Subject: [PATCH 6/9] Homework commit --- week-2/2-mandatory/2-function-creation.js | 123 +++++++++++++--------- week-2/2-mandatory/3-playing-computer.js | 26 ++++- week-2/2-mandatory/4-sorting-algorithm.js | 85 +++++++++------ 3 files changed, 147 insertions(+), 87 deletions(-) diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index bf7ecfd..e57c421 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -5,7 +5,15 @@ Write a function that: - removes any forward slashes (/) in the strings - makes the string all lowercase */ -function tidyUpString(strArr) {} +function tidyUpString(strArr) { + for (let i = 0; i < strArr.length; i++) { + strArr[i] = strArr[i].toLowerCase(); + strArr[i] = strArr[i].replace(/\//g, ""); + strArr[i] = strArr[i].trim(); + } + + return strArr; +} /* Complete the function to check if the variable `num` satisfies the following requirements: @@ -15,7 +23,13 @@ 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 +40,9 @@ The function must: */ function remove(arr, index) { - return; // complete this statement + let removeItem = arr.slice(0, index); + let returnArr = arr.slice(index + 1, arr); + return removeItem.concat(returnArr); // complete this statement } /* @@ -38,49 +54,56 @@ Write a function that: */ function formatPercentage(arr) { - + for (let j = 0; j < arr.length; j++) { + if (arr[j] > 100) { + arr[j] = 100; + } + let a = arr[j].toFixed(2); + let b = a.toString() + "%"; + + } } /* ======= TESTS - DO NOT MODIFY ===== */ function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + 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; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + 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( - "tidyUpString function works - case 1", - arraysEqual(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [ - "daniel", - "irina", - "gordon", - "ashleigh" - ]) + "tidyUpString function works - case 1", + arraysEqual(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [ + "daniel", + "irina", + "gordon", + "ashleigh", + ]) ); test( - "tidyUpString function works - case 2", - arraysEqual( - tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]), - ["sanyia", "michael", "anthony", "tim"] - ) + "tidyUpString function works - case 2", + arraysEqual( + tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]), + ["sanyia", "michael", "anthony", "tim"] + ) ); test("validate function works - case 1", validate(10) === true); @@ -90,27 +113,27 @@ test("validate function works - case 4", validate("Ten") === false); test("validate function works - case 5", validate(108) === false); test( - "remove function works - case 1", - arraysEqual(remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29]) + "remove function works - case 1", + arraysEqual(remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29]) ); test( - "remove function works - case 1", - arraysEqual(remove(["a", "b", "c", "d", "e", "f", "g"], 6), [ - "a", - "b", - "c", - "d", - "e", - "f" - ]) + "remove function works - case 1", + arraysEqual(remove(["a", "b", "c", "d", "e", "f", "g"], 6), [ + "a", + "b", + "c", + "d", + "e", + "f", + ]) ); test( - "formatPercentage function works - case 1", - arraysEqual(formatPercentage([23, 18, 187.2, 0.372]), [ - "23%", - "18%", - "100%", - "0.37%" - ]) -); \ No newline at end of file + "formatPercentage function works - case 1", + arraysEqual(formatPercentage([23, 18, 187.2, 0.372]), [ + "23%", + "18%", + "100%", + "0.37%", + ]) +); diff --git a/week-2/2-mandatory/3-playing-computer.js b/week-2/2-mandatory/3-playing-computer.js index 0fa7c04..457d580 100644 --- a/week-2/2-mandatory/3-playing-computer.js +++ b/week-2/2-mandatory/3-playing-computer.js @@ -7,12 +7,32 @@ Answer the following questions: 1. This program throws an error. Why? (If you can't find it, try executing it). + //Because the "console.log(b);" was not defined. + 2. Remove the line that throws the error. + // Console.log(b); is removed 3. What is printed to the console? + 2 + 6 + 4 + 9 + 6 + 13 + 8 4. How many times is "f1" called? + + // "f1" is called 2 times. + 5. How many times is "f2" called? + + // "f2" is called 3 times. + 6. What value does the "a" parameter take in the first "f1" call? + + // The parameter "a" value will be 4. + 7. What is the value of the "a" outer variable when "f1" is called for the first time? + */ let x = 2; @@ -23,14 +43,14 @@ const f1 = function(a, b) { }; const f2 = function(a, b) { - return a + b + x; + return a + b + x; }; console.log(x); console.log(a); -console.log(b); -for (let i = 0; i < 5; ++i) { + +for (let i = 0; i < 5; i++) { a = a + 1; if (i % 2 === 0) { const d = f2(i, x); diff --git a/week-2/2-mandatory/4-sorting-algorithm.js b/week-2/2-mandatory/4-sorting-algorithm.js index 3603942..d1eca88 100644 --- a/week-2/2-mandatory/4-sorting-algorithm.js +++ b/week-2/2-mandatory/4-sorting-algorithm.js @@ -13,58 +13,75 @@ Create a function called sortAges which: 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) {} +let newArray = []; +let temp; +function sortAges(arr) { + for (let i = 0; i < arr.length; i++) { + if (typeof arr[i] === 'number') { + newArray.push(arr[i]); + } + } + for (let j = 0; j < newArray.length; j++) { + for (let k = j + 1; k < newArray.length; k++) { + if (newArray[j] > newArray[k]) { + temp = newArray[j]; + newArray[j] = newArray[k]; + newArray[k] = temp; + } + } + } + return newArray; +} /* ======= TESTS - DO NOT MODIFY ===== */ const agesCase1 = [ - "🎹", - 100, - "💩", - 55, - "🥵", - "🙈", - 45, - "🍕", - "Sanyia", - 66, - "James", - 23, - "🎖", - "Ismeal", + "🎹", + 100, + "💩", + 55, + "🥵", + "🙈", + 45, + "🍕", + "Sanyia", + 66, + "James", + 23, + "🎖", + "Ismeal", ]; const agesCase2 = ["28", 100, 60, 55, "75", "🍕", "Elamin"]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + 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; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + return true; } function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED"; - } else { - status = "FAILED"; - } + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } - console.log(`${test_name}: ${status}`); + console.log(`${test_name}: ${status}`); } test( - "sortAges function works - case 1", - arraysEqual(sortAges(agesCase1), [23, 45, 55, 66, 100]) + "sortAges function works - case 1", + arraysEqual(sortAges(agesCase1), [23, 45, 55, 66, 100]) ); test( - "sortAges function works - case 2", - arraysEqual(sortAges(agesCase2), [55, 60, 100]) + "sortAges function works - case 2", + arraysEqual(sortAges(agesCase2), [55, 60, 100]) ); From b252a0c1ae1d2547af58552d0607cac857a7ea0e Mon Sep 17 00:00:00 2001 From: Osman-Hajr <63974847+Osman-Hajr@users.noreply.github.com> Date: Fri, 26 Jun 2020 23:18:08 +0100 Subject: [PATCH 7/9] Update 2-function-creation.js --- week-2/2-mandatory/2-function-creation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-2/2-mandatory/2-function-creation.js b/week-2/2-mandatory/2-function-creation.js index e57c421..a0f3608 100644 --- a/week-2/2-mandatory/2-function-creation.js +++ b/week-2/2-mandatory/2-function-creation.js @@ -41,7 +41,7 @@ The function must: function remove(arr, index) { let removeItem = arr.slice(0, index); - let returnArr = arr.slice(index + 1, arr); + let returnArr = arr.slice(index + 1, arr.length); return removeItem.concat(returnArr); // complete this statement } From 058349cb73d0d4b3be9cac3756191b4480d38c61 Mon Sep 17 00:00:00 2001 From: Osman Date: Mon, 6 Jul 2020 17:38:40 +0100 Subject: [PATCH 8/9] commits --- week-2/3-extra/1-radio-stations.js | 9 +- week-3/1-exercises/A-array-find/exercise.js | 6 +- week-3/1-exercises/B-array-some/exercise.js | 11 ++- week-3/1-exercises/C-array-every/exercise.js | 6 +- week-3/1-exercises/D-array-filter/exercise.js | 14 +-- week-3/2-mandatory/1-oxygen-levels.js | 35 +++---- week-3/2-mandatory/2-bush-berries.js | 40 +++++--- week-3/2-mandatory/3-space-colonies.js | 69 +++++++------- week-3/2-mandatory/4-eligible-students.js | 73 ++++++++------- week-3/2-mandatory/5-journey-planner.js | 92 +++++++++++-------- week-3/2-mandatory/6-lane-names.js | 62 +++++++------ week-3/2-mandatory/7-password-validator.js | 76 ++++++++------- 12 files changed, 282 insertions(+), 211 deletions(-) diff --git a/week-2/3-extra/1-radio-stations.js b/week-2/3-extra/1-radio-stations.js index 95c0e56..6519607 100644 --- a/week-2/3-extra/1-radio-stations.js +++ b/week-2/3-extra/1-radio-stations.js @@ -12,8 +12,11 @@ * - Create an array starting at 87 and ending in 108 * - Should return this array to use in other functions */ - + // `getAllFrequencies` goes here +function getAllFrequencies(array){ + return array; +} /** * Next, let's write a function that gives us only the frequencies that are radio stations. @@ -25,7 +28,9 @@ * - Return only the frequencies that are radio stations. */ // `getStations` goes here - +function getStations(){ + +} /* ======= 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..d8deea9 100644 --- a/week-3/1-exercises/A-array-find/exercise.js +++ b/week-3/1-exercises/A-array-find/exercise.js @@ -6,8 +6,10 @@ // write your code here var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"]; - -var longNameThatStartsWithA = findLongNameThatStartsWithA(names); +function findLongNameThatStartsWithA(names){ + return names.length > 7; +} +var longNameThatStartsWithA = names.find(findLongNameThatStartsWithA); console.log(longNameThatStartsWithA); diff --git a/week-3/1-exercises/B-array-some/exercise.js b/week-3/1-exercises/B-array-some/exercise.js index 965c052..d0f1e3d 100644 --- a/week-3/1-exercises/B-array-some/exercise.js +++ b/week-3/1-exercises/B-array-some/exercise.js @@ -15,10 +15,13 @@ 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) { - var student = students[indexes[0]]; - var mentor = mentors[indexes[1]]; - return [student, mentor]; +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]; }); console.log(pairs); diff --git a/week-3/1-exercises/C-array-every/exercise.js b/week-3/1-exercises/C-array-every/exercise.js index b515e94..f19c7fe 100644 --- a/week-3/1-exercises/C-array-every/exercise.js +++ b/week-3/1-exercises/C-array-every/exercise.js @@ -5,8 +5,10 @@ var students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; var group = ["Austine", "Dany", "Swathi", "Daniel"]; -var groupIsOnlyStudents; // complete this statement - +var groupIsOnlyStudents = group.every(studentsIncludeNames); // complete this statement +function studentsIncludeNames(names){ + return students.includes(names); +} 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..232d81a 100644 --- a/week-3/1-exercises/D-array-filter/exercise.js +++ b/week-3/1-exercises/D-array-filter/exercise.js @@ -8,15 +8,17 @@ var pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -var pairsByIndex; // Complete this statement - +var pairsByIndex = pairsByIndexRaw.filter(filteredArray); // Complete this statement +function filteredArray(array){ + return array. +} var students = ["Islam", "Lesley", "Harun", "Rukmini"]; 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]; +var pairs = pairsByIndex.map(function (indexes) { + var student = students[indexes[0]]; + var mentor = mentors[indexes[1]]; + 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..5af1b09 100644 --- a/week-3/2-mandatory/1-oxygen-levels.js +++ b/week-3/2-mandatory/1-oxygen-levels.js @@ -9,32 +9,33 @@ 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) { + + return oxygen.find(items => (items > "19.5%" && items < "23.5%")); } /* ======= 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%"]; 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( - "safeLevels function works - case 2", - safeLevels(oxygenLevels1) === "19.9%" + "safeLevels function works - case 2", + safeLevels(oxygenLevels1) === "19.9%" ); test( - "safeLevels function works - case 2", - safeLevels(oxygenLevels2) === "20.2%" -); \ No newline at end of file + "safeLevels function works - case 2", + safeLevels(oxygenLevels2) === "20.2%" +); diff --git a/week-3/2-mandatory/2-bush-berries.js b/week-3/2-mandatory/2-bush-berries.js index d900323..fe2630b 100644 --- a/week-3/2-mandatory/2-bush-berries.js +++ b/week-3/2-mandatory/2-bush-berries.js @@ -10,25 +10,37 @@ Use the tests to confirm which message to return */ -function bushChecker() { - +function bushChecker(bush) { + if (bush.filter(color => color !== "pink").length > 0) { + return "Toxic! Leave bush alone!" + } + else{ + return "Bush is safe to eat from" +} + } /* ======= 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"]; 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("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" +); diff --git a/week-3/2-mandatory/3-space-colonies.js b/week-3/2-mandatory/3-space-colonies.js index f99891a..94df030 100644 --- a/week-3/2-mandatory/3-space-colonies.js +++ b/week-3/2-mandatory/3-space-colonies.js @@ -8,50 +8,55 @@ NOTE: don't include any element that is not a "family". */ -function colonisers() { - +function colonisers(names) { + return names.filter(items => items.charAt(0) === "A" && items.includes('family')); } /* ======= TESTS - DO NOT MODIFY ===== */ const voyagers = [ - "Adam family", - "Potter family", - "Eric", - "Aldous", - "Button family", - "Jude", - "Carmichael", - "Bunny", - "Asimov", - "Oscar family", - "Avery family", - "Archer family" + "Adam family", + "Potter family", + "Eric", + "Aldous", + "Button family", + "Jude", + "Carmichael", + "Bunny", + "Asimov", + "Oscar family", + "Avery family", + "Archer family", ]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + 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; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + 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("colonisers function works", - arraysEqual(colonisers(voyagers), ["Adam family", "Avery family", "Archer family"]) -) \ No newline at end of file +test( + "colonisers function works", + arraysEqual(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 6424b01..3e983e6 100644 --- a/week-3/2-mandatory/4-eligible-students.js +++ b/week-3/2-mandatory/4-eligible-students.js @@ -7,46 +7,57 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function eligibleStudents() { - +function eligibleStudents(array) { + let newArray = array.filter(function (name) { + return name[1] >= 8; + }); + let qulifiedAttendant = newArray.map(function (names) { + return names[0]; + }); + console.log(qulifiedAttendant); + return qulifiedAttendant; } /* ======= TESTS - DO NOT MODIFY ===== */ const attendances = [ - ["Ahmed", 8], - ["Clement", 10], - ["Elamin", 6], - ["Adam", 7], - ["Tayoa", 11], - ["Nina", 10] -] + ["Ahmed", 8], + ["Clement", 10], + ["Elamin", 6], + ["Adam", 7], + ["Tayoa", 11], + ["Nina", 10], +]; 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("eligibleStudents function works", - arraysEqual( - eligibleStudents(attendances), ["Ahmed", "Clement", "Tayoa", "Nina"] - ) -) \ No newline at end of file +test( + "eligibleStudents function works", + arraysEqual(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 53499c3..847873e 100644 --- a/week-3/2-mandatory/5-journey-planner.js +++ b/week-3/2-mandatory/5-journey-planner.js @@ -7,59 +7,71 @@ NOTE: only the names should be returned, not the means of transport. */ -function journeyPlanner() { - +function journeyPlanner(array, transport) { + let newArr = []; + array.map(item => { + if (item.includes(transport)) { + newArr.push(item[0]); + } + }) + console.log(newArr) + return newArr; } /* ======= TESTS - DO NOT MODIFY ===== */ const londonLocations = [ - ["Angel", "tube", "bus"], - ["London Bridge", "tube", "river boat"], - ["Tower Bridge", "tube", "bus"], - ["Greenwich", "bus", "river boat"] -] + ["Angel", "tube", "bus"], + ["London Bridge", "tube", "river boat"], + ["Tower Bridge", "tube", "bus"], + ["Greenwich", "bus", "river boat"], +]; function arraysEqual(a, b) { - if (a === b) return true; - if (a == null || b == null) return false; - if (a.length != b.length) return false; + 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; - } + for (let i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } - return true; + 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("journeyPlanner function works - case 1", - arraysEqual( - journeyPlanner(londonLocations, "river boat"), - ["London Bridge", "Greenwich"] - ) -) +test( + "journeyPlanner function works - case 1", + arraysEqual(journeyPlanner(londonLocations, "river boat"), [ + "London Bridge", + "Greenwich", + ]) +); -test("journeyPlanner function works - case 2", - arraysEqual( - journeyPlanner(londonLocations, "bus"), - ["Angel", "Tower Bridge", "Greenwich"] - ) -) +test( + "journeyPlanner function works - case 2", + arraysEqual(journeyPlanner(londonLocations, "bus"), [ + "Angel", + "Tower Bridge", + "Greenwich", + ]) +); -test("journeyPlanner function works - case 3", - arraysEqual( - journeyPlanner(londonLocations, "tube"), - ["Angel", "London Bridge", "Tower Bridge"] - ) -) +test( + "journeyPlanner function works - case 3", + arraysEqual(journeyPlanner(londonLocations, "tube"), [ + "Angel", + "London Bridge", + "Tower Bridge", + ]) +); diff --git a/week-3/2-mandatory/6-lane-names.js b/week-3/2-mandatory/6-lane-names.js index eddfe44..dd123b9 100644 --- a/week-3/2-mandatory/6-lane-names.js +++ b/week-3/2-mandatory/6-lane-names.js @@ -4,43 +4,47 @@ Write a function that will return all street names which contain 'Lane' in their name. */ -function getLanes() { - +function getLanes(arr) { + + + return arr.filter(items => items.includes("Lane")); + } /* ======= TESTS - DO NOT MODIFY ===== */ const streetNames = [ - "Abchurch Lane", - "Adam's Court", - "Addle Hill", - "Addle Lane", - "Alban Highwalk" -] + "Abchurch Lane", + "Adam's Court", + "Addle Hill", + "Addle Lane", + "Alban Highwalk", +]; 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("getLanes function works", - arraysEqual(getLanes(streetNames), ["Abchurch Lane", "Addle Lane"]) -) \ No newline at end of file +test( + "getLanes function works", + arraysEqual(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 57b3d53..9f9482e 100644 --- a/week-3/2-mandatory/7-password-validator.js +++ b/week-3/2-mandatory/7-password-validator.js @@ -23,47 +23,59 @@ PasswordValidationResult= [false, false, false, false, true] */ function validatePasswords(passwords) { - + let checkPassword = passwords.map((pass, index, arr) => { + if ( + pass.length >= 5 && + /[A-Z]/.test(pass) && + /[a-z]/.test(pass) && + /[0-9]/.test(pass) && + /[!#$%.*&]/.test(pass) && + arr.indexOf(pass) === index + ) { + console.log(pass); + return true; + } else { + return false; + } + }); + console.log(checkPassword); + return checkPassword; } /* ======= 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]) +); From 75dabe3670204fb70ceb8137295448b1742ee6b3 Mon Sep 17 00:00:00 2001 From: Osman-Hajr <63974847+Osman-Hajr@users.noreply.github.com> Date: Mon, 6 Jul 2020 18:10:55 +0100 Subject: [PATCH 9/9] Update 1-fix-functions.js --- week-2/2-mandatory/1-fix-functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-2/2-mandatory/1-fix-functions.js b/week-2/2-mandatory/1-fix-functions.js index f29b1fc..ce80e39 100644 --- a/week-2/2-mandatory/1-fix-functions.js +++ b/week-2/2-mandatory/1-fix-functions.js @@ -40,7 +40,7 @@ function first5() { function get3rdIndex(arr) { let index = 3; let element = arr[index]; - + console.log(element); return element; }