diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 27282c4af..ba17f3371 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -15,4 +15,6 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". - 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? + It shows this error : SyntaxError: missing ) after argument list - What happens when you console.log() just a number without quotes? + When I changed it to : console.log(1234); , it works normally and runs withnout errors. diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..2ef7f760e 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1 @@ -console.log("Hello world"); +console.log(1234); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..233a97fe8 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `greeting` +let 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..aba14c990 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` +let message = "This is a string"; +let messageType = typeof message; console.log(message); +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..062b3af93 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` +let greetingStart = "Hello, My name is "; +let myName = "Mohammed"; +let message = greetingStart + myName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..0b5d7e26f 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,10 @@ // Start by creating a variable `message` - +let myName = "Mohammed"; +let myNameLength = myName.length; +let message = + "My name is " + + myName + + " and my name is " + + myNameLength + + " characters long"; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..e9536f774 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ const name = " Daniel "; - +var nameTrimmed = name.trim(); +var nameLength = nameTrimmed.length; +message = + "My Name is " + nameTrimmed + " and my name is " + nameLength + " long"; console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..90dbb4e87 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,13 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` + +let numberOfStudents = 36; +let numberOfMentors = 5; +let total = numberOfMentors + numberOfStudents; + +let message1 = "Number of students: " + numberOfStudents; +let message2 = "Number of mentors: " + numberOfMentors; +let message3 = "Total number of students and mentors: " + total; + +console.log(message1); +console.log(message2); +console.log(message3); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..cf68eb302 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,12 @@ var numberOfStudents = 15; var numberOfMentors = 8; +const total = numberOfMentors + numberOfStudents; + +let studentPct = numberOfStudents / total; +studentPct = Math.round(studentPct * 100) + "%"; + +let mentorsPct = numberOfMentors / total; +mentorsPct = Math.round(mentorsPct * 100) + "%"; + +console.log("Percentage students: " + studentPct); +console.log("Percentage mentors: " + mentorsPct); diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..66ea87bea 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,6 @@ function halve(number) { - // complete the function here + let temp = number / 2; + return temp; } var result = halve(12); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..49843f6c1 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,6 @@ function triple(number) { - // complete function here + let temp = number * 3; + return temp; } var result = triple(12); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..dbd50454d 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // 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` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..47dd9d711 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); console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..0f2106b52 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ // Write your function here -var greeting = createGreeting("Daniel"); +function createGreeting(name) { + return "Hello, my name is " + name; +} +var greeting = createGreeting("Mohammed"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..4c8e5825b 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -2,4 +2,9 @@ // Call the function and assign to a variable `sum` +function addNumbers(number1, number2) { + return number1 + number2; +} + +sum = addNumbers(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..a5968c0c2 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,11 @@ // Declare your function here -const greeting = createLongGreeting("Daniel", 30); +function createLongGreeting(name, age) { + var greeting = "Hello, my name is "; + greeting = greeting + name; + greeting = greeting + " and I'm " + age + " years old"; + return greeting; +} +const greeting = createLongGreeting("Mohammed", 38); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..074f9258a 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,18 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function upperCase(name) { + return name.toUpperCase(); +} + +function greeting(name) { + var nameUpperCase = upperCase(name); + return "HELLO " + nameUpperCase; +} + +console.log(greeting(mentor1)); +console.log(greeting(mentor2)); +console.log(greeting(mentor3)); +console.log(greeting(mentor4)); +console.log(greeting(mentor5)); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..0ad40e4df 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 CONVERSION @@ -15,7 +17,11 @@ 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) { + var result = amount * 0.99 * 5.7; + result = result.toFixed(2); + return Number(result); +} /* ======= 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 b4f8c4c1b..71abcf981 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,27 +16,29 @@ the final result to the variable goodCode */ -function add() { - +function add(number1, number2) { + return number1 + number2; } -function multiply() { - +function multiply(number1, number2) { + return number1 * number2; } -function format() { - +function format(amount) { + return "£" + amount; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = -/* BETTER PRACTICE */ - -let goodCode = +//I think it's bad because it's not readable and not maintanable, and not easy to debug. +let badCode = format(multiply(add(startingValue, 10), 2)); +/* BETTER PRACTICE */ +let result = add(startingValue, 10); +result = multiply(result, 2); +let goodCode = format(result); /* ======= 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 46f65f928..018b4000c 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,9 +46,13 @@ // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + return "It is certain."; + console.log("The ball has shaken!"); + //Write your code in here } +let answer = shakeBall(); /* This function should say whether the answer it is given is - very positive @@ -59,6 +63,7 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ function checkAnswer(answer) { + return "very positive"; //Write your code in here } @@ -101,7 +106,9 @@ test("magic 8 ball returns different values each time", () => { ); } - let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); + let seenPositivities = new Set( + Array.from(seenAnswers.values()).map(checkAnswer) + ); if (seenPositivities.size < 2) { throw Error( "Expected to random answers with different positivities each time shakeBall was called, but always got the same one" diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..388ff43f9 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) { +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) { + var message = `Hello, my name is ${name} and I am ${age} years old`; + return message; +} function getTotal(a, b) { - total = a ++ b; - - return "The total is total"; + var total = a + b; + return `The total is ${total}`; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..37c4ebc15 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 getStringLength(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 5a953ba60..f390f90f0 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,14 +1,17 @@ // Add comments to explain what this function does. You're meant to use Google! function getRandomNumber() { + // this function return a random floating-point number in the range 0 to less than 1 return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { + //this concatenate the word in the parameter in this case word2 , with the word1. return word1.concat(word2); } function concatenate(firstWord, secondWord, thirdWord) { + return firstWord.concat(" " + secondWord + " ").concat(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. } diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..bc38682ad 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(sales) { + return sales + sales * 0.2; +} /* CURRENCY FORMATTING @@ -17,7 +19,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(sales) { + var salesIncludeTax = calculateSalesTax(sales); + var result = "£" + salesIncludeTax.toFixed(2); + return result; +} /* ===================================================