From 4df05a0f1c44fe91abba4784121fd5f0ad715a4d Mon Sep 17 00:00:00 2001 From: Pouya <113245009+85pouya@users.noreply.github.com> Date: Fri, 24 Feb 2023 13:36:36 +0000 Subject: [PATCH 1/2] solved the issues in exercises 1-4 --- mandatory/1-syntax-errors.js | 11 ++++++----- mandatory/2-logic-error.js | 8 ++++---- mandatory/3-function-output.js | 4 ++++ mandatory/4-tax.js | 10 ++++++++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..deb5a89c8 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 "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; + total = a + b; - return "The total is total"; + return `The total is ${total}`; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9eb8c8cd7..e8eefc55a 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,16 @@ // The syntax for these functions is valid but there are some errors, find them and fix them 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; + let multiply = a * b * c; + return(multiply); } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..6f4d41965 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! +// In JavaScript, random() is a built-in method that is used to return a random number in the range 0 to 1 (with 0 being inclusive and 1 exclusive). The distribution of the generated random numbers is approximately uniform. It does not actually return a whole number. Instead, it returns a floating point (decimal) value. function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +13,8 @@ function combine2Words(word1, word2) { 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. + let sentence= firstWord.concat(" ", secondWord, " ", thirdWord); + return sentence; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..9ca42d9fa 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,10 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + let taxprice = price + price*0.2; + return taxprice; +} /* 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 addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + let taxprice = price + price*0.2; + return taxprice = '£' + taxprice.toFixed(2); +} /* =================================================== From f0ec751f9b07e3170644a5e63901fe8cd7a7ee83 Mon Sep 17 00:00:00 2001 From: 85pouya <113245009+85pouya@users.noreply.github.com> Date: Mon, 6 Mar 2023 11:34:02 +0000 Subject: [PATCH 2/2] add let before total at line 12 I added let to assign total as the sum of a and b. --- mandatory/1-syntax-errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index deb5a89c8..6e29e952c 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -9,7 +9,7 @@ function introduceMe(name, age) { } function getTotal(a, b) { - total = a + b; + let total = a + b; return `The total is ${total}`; }