diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..1053b8fe2 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none", + "printWidth": 100 +} diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..8073ba0a9 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1 @@ -console.log("Hello world"); +console.log("Hello world I'm learning JavaScript"); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..ec3442dee 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +const greeting = "Hello world"; +console.log(greeting); +console.log(greeting); console.log(greeting); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..137ad6b84 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,3 @@ // Start by creating a variable `message` - +const message = `Current Month January\n ${typeof "Current Month January"}`; console.log(message); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..275f812bb 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,3 @@ // Start by creating a variable `message` - +const message = "Hello, my name is " + "Junior"; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..33056167b 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +const message = `My name is Junior and my name is ${ + "Junior".length +} characters long`; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..4431c74a1 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ const name = " Daniel "; +const message = `My name is ${name.trim()} and name is ${ + name.trim().length +} characters long`; console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..0d9b65fca 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,9 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfStudents = 25; +const numberOfMentors = 25; + +console.log(`Total number of students: ${numberOfStudents}`); +console.log(`Totla number of mentors; ${numberOfMentors}`); +console.log( + `Total number of students and mentors: ${numberOfStudents + numberOfMentors}` +); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..54daa298f 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,12 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +const numberOfStudents = 15; +const numberOfMentors = 8; + +//Getting the total number of people either students or mentors +const totalNumberOfPeople = numberOfMentors + numberOfStudents; + +//Getting the percentage of mentors and students +const studentPercentage = (numberOfStudents / totalNumberOfPeople) * 100; +const mentorPercentage = (numberOfMentors / totalNumberOfPeople) * 100; + +console.log(`Percentage students: ${Math.round(studentPercentage)}%`); +console.log(`Percentage mentors: ${Math.round(mentorPercentage)}%`); diff --git a/exercises/J-functions/README.md b/exercises/J-functions/README.md index a5c765cef..22105ca2a 100644 --- a/exercises/J-functions/README.md +++ b/exercises/J-functions/README.md @@ -20,8 +20,8 @@ console.log(result); // 4 ## Exercise -* Complete the function in exercise.js so that it halves the input -* Try calling the function more than once with some different numbers +- Complete the function in exercise.js so that it halves the input +- Try calling the function more than once with some different numbers > Remember to use the return keyword to get a value out of the function @@ -33,7 +33,7 @@ console.log(result); // 4 ## Exercise 2 -* Complete the function in exercise2.js so that it triples the input +- Complete the function in exercise2.js so that it triples the input ## Expected result diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..e523dc9ea 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,7 @@ function halve(number) { // complete the function here + + return number / 2; } var result = halve(12); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..1949bb03f 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,7 @@ function triple(number) { // complete function here + + return number * 3; } var result = triple(12); diff --git a/exercises/K-functions-parameters/README.md b/exercises/K-functions-parameters/README.md index 02ab2a844..1614e221b 100644 --- a/exercises/K-functions-parameters/README.md +++ b/exercises/K-functions-parameters/README.md @@ -20,7 +20,7 @@ function add(num1, num2) { ## Exercise -* Write a function that multiplies two numbers together +- Write a function that multiplies two numbers together ## Expected result @@ -30,7 +30,7 @@ function add(num1, num2) { ## Exercise 2 -* From scratch, write a function that divides two numbers +- From scratch, write a function that divides two numbers ## Expected result @@ -40,7 +40,7 @@ function add(num1, num2) { ## Exercise 3 -* Write a function that takes a name (a string) and returns a greeting +- Write a function that takes a name (a string) and returns a greeting ## Expected result @@ -50,8 +50,8 @@ Hello, my name is Daniel ## Exercise 4 -* Write a function that adds two numbers together -* Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` +- Write a function that adds two numbers together +- Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` ## Expected result @@ -61,7 +61,7 @@ Hello, my name is Daniel ## Exercise 5 -* Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) +- Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) ## Expected result diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..4fb1a0382 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,8 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1, num2) { // Calculate the result of the function and return it + + return num1 * num2; } // Assign the result of calling the function the variable `result` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..a6caeea94 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,8 @@ // Declare your function first +const divide = (num1, num2) => { + return num1 / num2; +}; -var result = divide(3, 4); +const result = divide(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..03a7a75db 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ // Write your function here +const createGreeting = (name) => { + return `Hello, my name is ${name}`; +}; -var greeting = createGreeting("Daniel"); +const greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..cd8aadc36 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ // Declare your function first +const Add = (num1, num2) => { + return num1 + num2; +}; // Call the function and assign to a variable `sum` +const sum = Add(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..b00575249 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,7 @@ // Declare your function here +const createLongGreeting = (name, age) => { + return `Hello, my name is ${name} and I'm ${age} years old`; +}; const greeting = createLongGreeting("Daniel", 30); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..faba97154 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,17 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +const numberOfStudents = 15; +const numberOfMentors = 20; +const totalNumberOfPeople = numberOfMentors + numberOfStudents; + +const GetPercentage = (number_to_get_ratio) => { + return (number_to_get_ratio / totalNumberOfPeople) * 100; +}; + +const DisplayPercentage = () => { + const studentPercentage = Math.round(GetPercentage(numberOfStudents)); + const mentorPercentage = Math.round(GetPercentage(numberOfMentors)); + + console.log(`Percentage students: ${studentPercentage}%`); + console.log(`Percentage mentors: ${mentorPercentage}%`); +}; + +DisplayPercentage(); diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..4ec3e57fa --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,11 @@ +const mentor1 = "Daniel"; +const mentor2 = "Irina"; +const mentor3 = "Mimi"; +const mentor4 = "Rob"; +const mentor5 = "Yohannes"; + +console.log(mentor1); +console.log(mentor2); +console.log(mentor3); +console.log(mentor4); +console.log(mentor5); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..21c8b4855 100644 --- a/extra/1-currency-conversion.js +++ b/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(amount) { + return amount * 1.4; +} /* CURRENCY FORMATTING @@ -15,7 +17,10 @@ function convertToUSD() {} They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. */ -function convertToBRL() {} +function convertToBRL(amount) { + const percentageAmountToConvert = amount - (1 / 100) * amount; + return 5.7 * percentageAmountToConvert; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/2-piping.js b/extra/2-piping.js index 067dd0f5b..9463af8b6 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,32 @@ the final result to the variable goodCode */ -function add() { +function add(num1, num2) { + return num1 + num2; +} +function multiply(num1, num2) { + return num1 * num2 } -function multiply() { - -} - -function format() { - +function format(num) { + return `£${num}` } const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. -let badCode = -/* BETTER PRACTICE */ +/** + * Chaining of this methods all in one line is unclear on what is happening at first glance + * Prone to making errors while chaining this methods +**/ +let badCode = format(multiply(add(startingValue, 10), 2)) -let goodCode = +/* BETTER PRACTICE */ +const sum = add(startingValue, 10); +const multipleOfSum = multiply(sum,2) +let goodCode = format(multipleOfSum) /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index e32182cfe..7d234382f 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -47,6 +47,38 @@ // and return the answer. function shakeBall() { //Write your code in here + + const possibleAnswers = [ + 'It is certain.', + 'It is decidedly so.', + 'Without a doubt.', + 'Yes - definitely.', + 'You may rely on it.', + + 'As I see it, yes.', + 'Most likely.', + 'Outlook good.', + 'Yes.', + 'Signs point to yes.', + + 'Reply hazy, try again.', + 'Ask again later.', + 'Better not tell you now.', + 'Cannot predict now.', + 'Concentrate and ask again.', + + "Don't count on it.", + 'My reply is no.', + 'My sources say no.', + 'Outlook not so good.', + 'Very doubtful.' + ]; + + console.log('The ball has shaken!'); + + const possibleAnswerIndex = Math.floor(Math.random() * 20); + + return possibleAnswers[possibleAnswerIndex]; } /* @@ -60,6 +92,60 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + + const possibleAnswers = [ + { + veryPositive: [ + 'It is certain.', + 'It is decidedly so.', + 'Without a doubt.', + 'Yes - definitely.', + 'You may rely on it.' + ], + positive: [ + 'As I see it, yes.', + 'Most likely.', + 'Outlook good.', + 'Yes.', + 'Signs point to yes.' + ], + + negative: [ + 'Reply hazy, try again.', + 'Ask again later.', + 'Better not tell you now.', + 'Cannot predict now.', + 'Concentrate and ask again.' + ], + + veryNegative: [ + "Don't count on it.", + 'My reply is no.', + 'My sources say no.', + 'Outlook not so good.' + ] + } + ]; + + let status = ''; + possibleAnswers.forEach((array) => { + if (array.veryPositive.includes(answer)) { + status = 'very positive'; + } + + if (array.positive.includes(answer)) { + status = 'positive'; + } + + if (array.negative.includes(answer)) { + status = 'negative'; + } + + if (array.veryNegative.includes(answer)) { + status = 'very negative'; + } + }); + return status; } /* @@ -82,9 +168,9 @@ console.log = function () { function test(test_name, expr) { let status; if (expr) { - status = "PASSED"; + status = 'PASSED'; } else { - status = "FAILED"; + status = 'FAILED'; } logged = undefined; @@ -94,27 +180,22 @@ function test(test_name, expr) { const validAnswers = []; function testAll() { const answer = shakeBall(); - test( - `shakeBall logs "The ball has shaken!"`, - logged === "The ball has shaken!" - ); - test(`shakeBall returns an string answer`, typeof answer === "string"); + test(`shakeBall logs "The ball has shaken!"`, logged === 'The ball has shaken!'); + test(`shakeBall returns an string answer`, typeof answer === 'string'); test( `checkAnswer("It is decidedly so.") returns "very positive`, - checkAnswer("It is decidedly so.") === "very positive" + checkAnswer('It is decidedly so.') === 'very positive' ); test( `checkAnswer("My reply is no.") returns "very negative`, - checkAnswer("My reply is no.") === "very negative" + checkAnswer('My reply is no.') === 'very negative' ); test( `checkAnswer returns the level of positivity"`, - ["very positive", "positive", "negative", "very negative"].includes( - checkAnswer(answer) - ) + ['very positive', 'positive', 'negative', 'very negative'].includes(checkAnswer(answer)) ); const answers = new Set(); for (let i = 0; i < 10; ++i) { diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..0591e38d8 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,17 @@ // 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 introduceMe(name, age) { + return "Hello, my name is " + name + " and I am " + age + " years old"; +} function getTotal(a, b) { - total = a ++ b; + const total = a + b; - return "The total is total" + return `The total is ${total}`; } /* @@ -24,19 +25,25 @@ To run these tests type `node 1-syntax-errors.js` into your terminal =================================================== */ -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } test("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 getTotal function", getTotal(23, 5), "The total is 28"); +test( + "fixed introduceMe function - case 2", + introduceMe("Sonjide", 27), + "Hello, my name is Sonjide and I am 27 years old" +); +test("fixed getTotal function - case 3", getTotal(23, 5), "The total is 28"); diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 3c578ad87..1f1245b39 100644 --- a/mandatory/2-logic-error.js +++ b/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; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..27067c28d 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,22 @@ // Add comments to explain what this function does. You're meant to use Google! +/** + + * @description Function returns a random floating numbers bounded from [0, 10) + + **/ function getNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! + +/** + * @description Function concatenates 2 values together + * @param w1 + * @param w2 + * @returns Joint result of @param w1 and @param w2 + * @example w1='J' and w2='mac' result is 'Jmac' + **/ function s(w1, w2) { return w1.concat(w2); } @@ -11,6 +24,8 @@ 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 this function is expected to return. + + return `${firstWord} ${secondWord} ${thirdWord}`; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..bb5fc6fb4 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,11 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + const chargeAmount = price + (20 / 100) * price; + + return chargeAmount; +} /* CURRENCY FORMATTING @@ -17,7 +21,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(amount) { + const taxedAmount = calculateSalesTax(amount).toFixed(2); + return `£${taxedAmount}`; +} /* ===================================================