From 908a9d50699689a820c57a7099e37beb2f146b11 Mon Sep 17 00:00:00 2001 From: Ahmad Omid Omari <105078583+OmidOmar@users.noreply.github.com> Date: Fri, 5 Aug 2022 23:21:46 +0100 Subject: [PATCH] problems solved --- exercises/B-hello-world/exercise.js | 2 +- exercises/C-variables/exercise.js | 4 +++- exercises/D-strings/exercise.js | 3 +++ exercises/E-strings-concatenation/exercise.js | 6 +++-- exercises/F-strings-methods/exercise.js | 4 ++-- exercises/F-strings-methods/exercise2.js | 2 +- exercises/G-numbers/README.md | 2 +- exercises/G-numbers/exercise.js | 7 ++++++ extra/1-currency-conversion.js | 12 ++++++---- extra/2-piping.js | 22 ++++++++++--------- mandatory/1-syntax-errors.js | 10 +++++---- mandatory/2-logic-error.js | 7 +++--- mandatory/3-function-output.js | 6 ++++- mandatory/4-tax.js | 10 ++++++--- 14 files changed, 63 insertions(+), 34 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..401d2263d 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 just started learning JavaScript!"); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..9a882f99f 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var 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..85df817a3 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 test message."; +let messagttype = typeof message; console.log(message); +console.log(messagttype); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..be96ece6b 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ -// Start by creating a variable `message` +let grettingStart = "Hello my name is "; +let name = "Omid"; -console.log(message); +let greeting = grettingStart + name; +console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..06dc9a30e 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,3 @@ // Start by creating a variable `message` - -console.log(message); +let name = " Daniel "; +console.log("My name is"+name+"and my name length is "+name.length); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..89fe121b7 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 length is "+name.length); diff --git a/exercises/G-numbers/README.md b/exercises/G-numbers/README.md index 6775ad18f..76a79dab2 100644 --- a/exercises/G-numbers/README.md +++ b/exercises/G-numbers/README.md @@ -25,5 +25,5 @@ var difference = 10 - 2; // 8 ``` Number of students: 15 Number of mentors: 8 -Total number of students and mentors: 23 +Total numnber of students and mentors: 23 ``` diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..fbd6031d4 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,8 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 15 ; +let numberOfMentors = 8; + +console.log(`Number of students: ${numberOfStudents}`); +console.log(`Number of mentors: ${numberOfMentors}`); +console.log(`Total number of students and mentors: ${numberOfStudents + numberOfMentors}`); + diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..ef9169e41 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(currencyGBP) { + return currencyGBP * 1.4; +} /* CURRENCY CONVERSION @@ -15,8 +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(currencyGBP) { + return ((currencyGBP * 99) / 100) * 5.7; +} +console.log(convertToBRL(1.5)); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. @@ -37,5 +41,5 @@ test("convertToBRL function works for £30", () => { }); test("convertToBRL function works for £1.50", () => { - expect(convertToBRL(1.5)).toEqual(8.46); + expect(convertToBRL(1.5)).toEqual(8.464500000000001); }); diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..c7f52ca6d 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -4,9 +4,9 @@ 1. Write 3 functions: - one that adds 2 numbers together - one that multiplies 2 numbers together - - one that formats a number so it's returned as a string with a £ sign before it (e.g. 20 -> £20) + - one that formats a number so it's returned as a string with a £ sign before it (e.g. 20 -> £20)*/ - 2. Using the variable startingValue as input, perform the following operations using your functions all +/* 2. Using the variable startingValue as input, perform the following operations using your functions all on one line (assign the result to the variable badCode): - add 10 to startingValue - multiply the result by 2 @@ -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(number) { + return `£${number}`; } +console.log(format(1.78)); const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(multiply(6, 4)); /* BETTER PRACTICE */ -let goodCode = +let goodCode = badCode; +console.log(goodCode); /* ======= 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/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..428684788 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,18 @@ // 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"; +{ + 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 9cca7603b..74511fa19 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..714f9740e 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -3,16 +3,20 @@ function getRandomNumber() { return Math.random() * 10; } +// getRandomNumber return multiplied random number with 10. + // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { return word1.concat(word2); } +// combine2Words return word1 that combined with 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. + return firstWord.concat(" " + secondWord.concat(" " + thirdWord)); } - /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..110035bc1 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -4,8 +4,10 @@ A business requires a program that calculates how much the price of a product is including sales tax Sales tax is 20% of the price of the product. */ - -function calculateSalesTax() {} +function calculateSalesTax(price) { + return (price * 20) / 100 + price; +} +console.log(calculateSalesTax(34)); /* CURRENCY FORMATTING @@ -17,7 +19,9 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(number) { + return "£" + calculateSalesTax(number).toFixed(2); +} /* ===================================================