diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..71a559d6b 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,15 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +let total = 0.00; +let gbp = 0.00; + +let brl = 0.00; + +function convertToUSD(gbp) { + let usd = gbp * 1.4; + return usd; +} /* CURRENCY CONVERSION @@ -15,7 +23,13 @@ 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(gbp) { +brl = gbp * 0.99 * 5.7 ; +brl = parseFloat(brl.toFixed(2)); +return brl; + +} /* ======= 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..c7ddb60cf 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -15,27 +15,35 @@ 3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign the final result to the variable goodCode */ +let num1; +let num2; -function add() { - +function add(num1, num2) { + return num1 + num2; } -function multiply() { - +function multiply(num1, num2) { + return num1 * num2; } -function format() { - +function format(num1) { + return "£" + num1; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = add(10, startingValue); +badCode = multiply(2,badCode); +badCode= format(badCode); + /* BETTER PRACTICE */ -let goodCode = + +let addition = add(10,startingValue); +let multiplication = multiply(2, addition); +let goodCode = "£" + multiplication; /* ======= 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..3fed6dad3 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,54 +1,51 @@ -/** - - 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. -*/ - -// This should log "The ball has shaken!" -// and return the answer. +// Rahma _ Berhan + + +//declare 2D array to contain all messages each type in separated row + +const arrayOfMessages = [ + ["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."] ]; + +// declare random1 and random2 variables- and their values will be generated rondomly +// will use these two numbers to choose message from the array + +let random1; +let random2; + +// this function will print message first, then will generate two numbers , then return message function shakeBall() { - //Write your code in here + console.log("The ball has shaken!"); + + random1 = Math.floor(Math.random() * arrayOfMessages.length); + random2 = Math.floor(Math.random() * arrayOfMessages.length); + + return arrayOfMessages[random1][random2]; + } + /* This function should say whether the answer it is given is - very positive @@ -58,10 +55,53 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ + +// this function will check the array one by one to search for the message stored in answer +// when the message is found ,it will save the row number in variable rowNum +// then it will move to switch to store the message type in type variable and then return it + function checkAnswer(answer) { - //Write your code in here + let type = ""; + let rowNum = 0; + + //search for answer + for (let i = 0 ; i <= 3 ; i++) + { + for (let j= 0 ;j<=4 ; j++) + { + if (answer == arrayOfMessages[i][j]) + { + rowNum = i; + } + } + } + +//return message type depending on the rowNum + switch (rowNum) + { + case 0: + type = "very positive"; + break; + + case 1: + type = "positive"; + break; + + case 2: + type = "negative"; + break; + + case 3: + type = "very negative"; + break; + } + return type; + } + + + /* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..da559d4ec 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`; +{ + return "Hello, my name is " + name + " and I am " + age + " years old"; +} function getTotal(a, b) { - total = a ++ b; - - return "The total is total"; + total = a + b; + return "The total is " + total; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9eb8c8cd7..5152c982c 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; + + return a * b * c; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..099c42c29 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 will generate a random number between 0 & 1 then multiply it by 10. function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +//The concat() method joins two or more strings. So this function will join word1 & word2 in one sentence. function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +13,7 @@ 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. + return firstWord.concat(" ", secondWord, " ", thirdWord); } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..2aa4e527d 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -4,8 +4,13 @@ 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. */ +let price ; +let total ; -function calculateSalesTax() {} +function calculateSalesTax(price) { + total = price + (0.20 * price); + return total; +} /* CURRENCY FORMATTING @@ -17,7 +22,11 @@ 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 tax = calculateSalesTax(price); + tax = parseFloat(tax).toFixed(2); + return `£${tax}`; +} /* ===================================================