diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index a7b891d56..cdd70ac87 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -6,13 +6,13 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". ### 1. Run the program -* Open a terminal window -* Change directory to this folder (`cd B-hello-world`) -* Run the program using node (`node exercise.js`) +- Open a terminal window +- Change directory to this folder (`cd B-hello-world`) +- Run the program using node (`node exercise.js`) ### 2. Experiment -* Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. -* Try to console.log() several things at once. -* What happens when you get rid of the quote marks? -* What happens when you console.log() just a number without quotes? +- Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. +- Try to console.log() several things at once. +- What happens when you get rid of the quote marks? Syntax error +- What happens when you console.log() just a number without quotes? It prints the number diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..c3374c133 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ -console.log("Hello world"); +console.log("Hello World. I just started learning JavaScript!"); +console.log(2); diff --git a/exercises/C-variables/README.md b/exercises/C-variables/README.md index 26eccc1e8..7936084db 100644 --- a/exercises/C-variables/README.md +++ b/exercises/C-variables/README.md @@ -12,8 +12,8 @@ The program above will print "Hello world" to the console. Notice how it uses th ## Exercise -* Add a variable `greeting` to exercise.js (make sure it comes _before_ the console.log) -* Print your `greeting` to the console 3 times +- Add a variable `greeting` to exercise.js (make sure it comes _before_ the console.log) +- Print your `greeting` to the console 3 times > Remember: to run this exercise you must change directory to the `D-variables`. If you already have a terminal window open for the previous exercise you can do this by running the command `cd ../D-variables`. diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..c2925e716 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,6 @@ // 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/README.md b/exercises/D-strings/README.md index 1516f536e..fd0a664ae 100644 --- a/exercises/D-strings/README.md +++ b/exercises/D-strings/README.md @@ -19,7 +19,7 @@ console.log(messageType); // logs 'string' ## Exercise -* Write a program that logs a message and its type +- Write a program that logs a message and its type ## Expected result diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..e4017fe0d 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` +const message = "This is a string"; console.log(message); +console.log(typeof message); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..3d25e22e2 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +let greetingStart = "Hello, my name is "; +let name = "Cecilia"; -console.log(message); +let greeting = greetingStart + name; + +console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..be0666203 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` +const name = "Cecilia" -console.log(message); +console.log("My name is " + name + " and my name is " + name.length + " characters long"); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..2fa3aa87e 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,3 @@ const name = " Daniel "; -console.log(message); +console.log("My name is " + name.trim() + " and my name is " + name.trim().length + " characters long"); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..0e721eb3f 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfStudents = 15; +const numberOfMentors = 8; + +console.log("Number of students: " + numberOfStudents); +console.log("Number of mentors: " + numberOfMentors); +console.log("Total number of students and mentors: " + (numberOfMentors + numberOfStudents)); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..dbc18a946 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,5 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +const numberOfStudents = 15; +const numberOfMentors = 8; + +console.log("Percentage students: " + Math.round(numberOfStudents/(numberOfStudents + numberOfMentors)*100)+"%"); +console.log("Percentage mentors: " + Math.round(numberOfMentors/(numberOfStudents + numberOfMentors)*100)+"%"); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..59ae60365 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,11 @@ function halve(number) { // complete the function here + return number/2; } -var result = halve(12); +let result = halve(12); + +let result1 = halve(15); console.log(result); +console.log(result1); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..1a4ac1ee7 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,8 @@ function triple(number) { // complete function here + return number*3; } -var result = triple(12); +let result = triple(12); console.log(result); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..f05588aee 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,10 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { // Calculate the result of the function and return it + return a*b; } // Assign the result of calling the function the variable `result` -var result = multiply(3, 4); +const result = multiply(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..a70898192 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,8 @@ // Declare your function first +function divide(a, b) { + return a/b; +} -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..05624ee16 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ // Write your function here +function createGreeting(greet) { + return "Hello, my name is " + greet; +} -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..852e1111e 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,8 @@ // Declare your function first - +function addition(a,b) { + return a + b; +} // Call the function and assign to a variable `sum` +let sum = addition(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..f8678fb7f 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/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/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..e64158c96 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,16 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +let students = { "type": "students", "number": 15}; +let mentors = { "type": "mentors", "number": 8}; + +function calculatePercentage(a,b) { + return Math.round(a.number / (a.number + b.number) * 100); +} + +function createMessage(cat1, cat2) { + return "Percentage " + cat1.type + ": " + calculatePercentage(cat1, cat2) + "%"; +} + +let percentageOfStudents = createMessage(students, mentors); +let percentageOfMentors = createMessage(mentors, students); + +console.log(percentageOfStudents); +console.log(percentageOfMentors); \ No newline at end of file diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..f2d923d4f --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,19 @@ +let mentor1 = "Daniel"; +let mentor2 = "Irina"; +let mentor3 = "Mimi"; +let mentor4 = "Rob"; +let mentor5 = "Yohannes"; + +function shoutName(name) { + return name.toUpperCase(); +} + +function shoutGreeting(mentor) { + return "HELLO " + shoutName(mentor); +} + +console.log(shoutGreeting(mentor1)); +console.log(shoutGreeting(mentor2)); +console.log(shoutGreeting(mentor3)); +console.log(shoutGreeting(mentor4)); +console.log(shoutGreeting(mentor5)); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..7442ee792 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(price) { + return price * 1.4; +} /* CURRENCY FORMATTING @@ -15,7 +17,9 @@ 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(price) { + return price * 0.99 * 5.7; +} /* ======= 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..37056644e 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,28 @@ 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(num) { + return `£${num}`; } const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. -let badCode = +// It is not clear what's going on for someone who is not familiar with the code. +let badCode = format(multiply(add(startingValue, 10), 2)); /* BETTER PRACTICE */ - -let goodCode = +let addedValue = add(startingValue, 10); +let multipliedValue = multiply(addedValue, 2); +let goodCode = format(multipliedValue); /* ======= 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..b6c1285fa 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,80 +1,139 @@ /** - Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/Magic_8-Ball - - There are a few steps to being able view the future though: - * Ask a question - * Shake the ball - * Get an answer - * Decide if it's positive or negative - - The question can be anything, but the answers are fixed, - and have different levels of positivity or negativity. - - Below are the possible answers: - - ## Very positive - 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. - - ## Very negative - Don't count on it. - My reply is no. - My sources say no. - Outlook not so good. - Very doubtful. +Let's peer into the future using a Magic 8 Ball! +https://en.wikipedia.org/wiki/Magic_8-Ball + +There are a few steps to being able view the future though: +* Ask a question +* Shake the ball +* Get an answer +* Decide if it's positive or negative + +The question can be anything, but the answers are fixed, +and have different levels of positivity or negativity. + +Below are the possible answers: + +## Very positive +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. + +## Very negative +Don't count on it. +My reply is no. +My sources say no. +Outlook not so good. +Very doubtful. */ - +let answers = [ + {"content": "It is certain.", + "type": "very positive" + }, + {"content": "It is decidedly so.", + "type": "very positive" + }, + {"content": "Without a doubt.", + "type": "very positive" + }, + {"content": "Yes - definitely.", + "type": "very positive" + }, + {"content": "You may rely on it.", + "type": "very positive" + }, + {"content": "As I see it, yes.", + "type": "positive" + }, + {"content": "Most likely.", + "type": "positive" + }, + {"content": "Outlook good.", + "type": "positive" + }, + {"content": "Yes.", + "type": "positive" + }, + {"content": "Signs point to yes.", + "type": "positive" + }, + {"content": "Reply hazy, try again.", + "type": "negative" + }, + {"content": "Ask again later.", + "type": "negative" + }, + {"content": "Better not tell you now.", + "type": "negative" + }, + {"content": "Cannot predict now.", + "type": "negative" + }, + {"content": "Concentrate and ask again.", + "type": "negative" + }, + {"content": "Don't count on it.", + "type": "very negative" + }, + {"content": "My reply is no.", + "type": "very negative" + }, + {"content": "My sources say no.", + "type": "very negative" + }, + {"content": "Outlook not so good.", + "type": "very negative" + }, + {"content": "Very doubtful.", + "type": "very negative" + } +] // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - //Write your code in here + let randomAnswer = Math.floor(Math.random() * 20); + console.log("The ball has shaken!") + return answers[randomAnswer].content; } +// This function should say whether the answer it is given is +// - very positive +// - positive +// - negative +// - very negative +// This function should expect to be called with any value which was returned by the shakeBall function. -/* - This function should say whether the answer it is given is - - very positive - - positive - - negative - - very negative - - This function should expect to be called with any value which was returned by the shakeBall function. -*/ function checkAnswer(answer) { - //Write your code in here + let result = answers.filter(obj => { + return obj.content === answer + }) + return result[0].type; } -/* -================================== -======= TESTS - DO NOT MODIFY ===== - +/* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. To run these tests type `node 3-magic-8-ball.js` into your terminal -================================== */ const log = console.log; let logged; -console.log = function () { +console.log = function() { log(...arguments); logged = arguments[0]; }; @@ -103,12 +162,12 @@ function testAll() { test( `checkAnswer("It is decidedly so.") returns "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" - ); + ) test( `checkAnswer returns the level of positivity"`, @@ -120,10 +179,13 @@ function testAll() { for (let i = 0; i < 10; ++i) { answers.add(shakeBall()); } - test(`shakeBall returns different answers`, answers.size > 1); + test( + `shakeBall returns different answers`, + answers.size > 1, + ); test( `checkAnswer returns different answers`, - new Set(Array.from(answers.values()).map(checkAnswer)).size > 1 + new Set(Array.from(answers.values()).map(checkAnswer)).size > 1, ); } diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..f1095a765 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,27 +1,24 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +function addNumbers(a, b, c) { return a + b + c; } -function introduceMe(name, age) -return "Hello, my name is " + name "and I am " age + "years old"; +function introduceMe(name, age) { + return "Hello, my name is " + name + " and I am " + age + " years old"; +} function getTotal(a, b) { - total = a ++ b; + const total = a + b; - return "The total is total" + // Use string interpolation here + return `The total is ${total}`; } -/* -=================================================== -======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - +/* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. To run these tests type `node 1-syntax-errors.js` into your terminal - -=================================================== */ const util = require('util'); diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 3c578ad87..71118b8ee 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,51 +1,36 @@ // 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 BELOW THIS LINE ===== - +/* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. To run these tests type `node 2-logic-error` 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 trimWord function", - trimWord(" CodeYourFuture "), - "CodeYourFuture" -); -test( - "fixed wordLength function", - getWordLength("A wild sentence appeared!"), - 25 -); +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/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..bfca810a1 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,11 @@ // Add comments to explain what this function does. You're meant to use Google! +// This function returns a random number >= 0 and < 10 function getNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// This function returns the two input parameters concatenated (i.e. the second one following the first) function s(w1, w2) { return w1.concat(w2); } @@ -11,6 +13,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 this function is expected to return. + return `${firstWord} ${secondWord} ${thirdWord}`; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..0af0c5a9b 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,9 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + return price += price * 0.2; +} /* CURRENCY FORMATTING @@ -17,7 +19,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + price = calculateSalesTax(price); + return `£${price.toFixed(2)}` +} /* ===================================================