From 2fbdb554ad6ffa5730456bfb46d7b9eee35e5a19 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Fri, 17 Feb 2023 21:28:13 +0000 Subject: [PATCH 1/8] Update 1-syntax-errors.js --- mandatory/1-syntax-errors.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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; } /* From b2d02c16da756a805d5e58aaf4d6cfd74bc2c013 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Fri, 17 Feb 2023 22:18:03 +0000 Subject: [PATCH 2/8] 2-3 --- mandatory/2-logic-error.js | 8 ++++---- mandatory/3-function-output.js | 3 +++ mandatory/4-tax.js | 7 ++++++- 3 files changed, 13 insertions(+), 5 deletions(-) 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..691a85c7a 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 From ab8871a2ad5d2948d561c7b25f108f27842f5cf7 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Fri, 17 Feb 2023 22:36:23 +0000 Subject: [PATCH 3/8] mandatory --- mandatory/4-tax.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 691a85c7a..2aa4e527d 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -9,7 +9,7 @@ let total ; function calculateSalesTax(price) { total = price + (0.20 * price); - return "£" + total; + return total; } /* @@ -22,7 +22,11 @@ function calculateSalesTax(price) { 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}`; +} /* =================================================== From 912dcb14bebe5e6d5958fb89a8679bb639087888 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Fri, 17 Feb 2023 23:41:21 +0000 Subject: [PATCH 4/8] extra 1 & 2 --- extra/1-currency-conversion.js | 17 +++++++++++++++-- extra/2-piping.js | 24 ++++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..14233860b 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(gbp) { + let usd = gbp * 1.4; + return usd; +} /* CURRENCY CONVERSION @@ -15,7 +18,17 @@ 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() {} +let total = 0.00; +let gbp = 0.00; +let brl = 0.00; + +function convertToBRL(gbp) { + +brl = ( gbp - (gbp*0.01)) * 5.7 ; +total = Number(brl).toFixed(2); +return total; + +} /* ======= 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. From 49fd1785db025806df8f873e577a2c2840b9c5b3 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Sat, 18 Feb 2023 23:07:40 +0000 Subject: [PATCH 5/8] extra - 3 - in progress --- extra/3-magic-8-ball.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..a28106516 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,10 +45,22 @@ // This should log "The ball has shaken!" // and return the answer. +const arrayOfMessages = ["It is certain." ,"It is decidedly so.", "Without a doubt." , + "Yes - definitely.", "You may rely on it."]; + function shakeBall() { - //Write your code in here + + console.log("The ball has shaken!"); + //let randomItem = arrayOfMessages[Math.floor((Math.random()*arrayOfMessages.length))]; + let randomItem = Math.floor(Math.random() * arrayOfMessages.length); + console.log("the random item is " + randomItem) + + let randomMessage = arrayOfMessages [randomItem]; + console.log("the random message is: " + randomMessage); + return randomMessage; } + /* This function should say whether the answer it is given is - very positive @@ -58,8 +70,14 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ +//let answer = ["1" , "2" , "3"]; + function checkAnswer(answer) { //Write your code in here + // let temp2 = answer[Math.floor((Math.random()*answer.length))]; + // audioElement.setAttribute('src', textArray[randomNumber]); + // return temp2; + } /* From 905d09d70b8ef66ace5942080f9214637bd426c2 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Sun, 19 Feb 2023 13:18:06 +0000 Subject: [PATCH 6/8] trying Switch to check the answers --- extra/3-magic-8-ball.js | 77 +++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index a28106516..fad3b1310 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,19 +45,46 @@ // This should log "The ball has shaken!" // and return the answer. -const arrayOfMessages = ["It is certain." ,"It is decidedly so.", "Without a doubt." , - "Yes - definitely.", "You may rely on it."]; -function shakeBall() { +//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."] ]; + +let random1; +let random2; +function shakeBall() { console.log("The ball has shaken!"); - //let randomItem = arrayOfMessages[Math.floor((Math.random()*arrayOfMessages.length))]; - let randomItem = Math.floor(Math.random() * arrayOfMessages.length); - console.log("the random item is " + randomItem) - - let randomMessage = arrayOfMessages [randomItem]; - console.log("the random message is: " + randomMessage); - return randomMessage; + + random1 = Math.floor(Math.random() * arrayOfMessages.length); + random2 = Math.floor(Math.random() * arrayOfMessages.length); + + // console.log(arrayOfMessages[random1][random2]); + return arrayOfMessages[random1][random2]; + } @@ -70,13 +97,33 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -//let answer = ["1" , "2" , "3"]; + function checkAnswer(answer) { - //Write your code in here - // let temp2 = answer[Math.floor((Math.random()*answer.length))]; - // audioElement.setAttribute('src', textArray[randomNumber]); - // return temp2; + let type; + switch (random1) + { + case 0: + //console.log("very positive"); + type = "very positive"; + break; + + case 1: + //console.log("positive"); + type = "positive"; + break; + + case 2: + //console.log("negative"); + type = "negative"; + break; + + case 3: + //console.log("very negative"); + type = "very negative"; + break; + } + return type; } From 593499b38033b030c00094472402558475e499e9 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Sun, 19 Feb 2023 14:05:18 +0000 Subject: [PATCH 7/8] adding for loop and comments I've added for loop to be able to search for any string passed to the function. --- extra/3-magic-8-ball.js | 81 ++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 53 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index fad3b1310..3fed6dad3 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,50 +1,5 @@ -/** - - 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. -*/ +// Rahma _ Berhan -// This should log "The ball has shaken!" -// and return the answer. //declare 2D array to contain all messages each type in separated row @@ -73,16 +28,19 @@ const arrayOfMessages = [ "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() { console.log("The ball has shaken!"); random1 = Math.floor(Math.random() * arrayOfMessages.length); random2 = Math.floor(Math.random() * arrayOfMessages.length); - // console.log(arrayOfMessages[random1][random2]); return arrayOfMessages[random1][random2]; } @@ -98,28 +56,42 @@ 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) { - let type; - switch (random1) + 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: - //console.log("very positive"); type = "very positive"; break; case 1: - //console.log("positive"); type = "positive"; break; case 2: - //console.log("negative"); type = "negative"; break; case 3: - //console.log("very negative"); type = "very negative"; break; } @@ -127,6 +99,9 @@ function checkAnswer(answer) { } + + + /* ================================== ======= TESTS - DO NOT MODIFY ===== From 91279046b9a170f65dfd5d41cd2156492f2541e1 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Sun, 19 Feb 2023 14:54:45 +0000 Subject: [PATCH 8/8] fixing extra 1 converting from string to number --- extra/1-currency-conversion.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 14233860b..71a559d6b 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,6 +5,11 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ +let total = 0.00; +let gbp = 0.00; + +let brl = 0.00; + function convertToUSD(gbp) { let usd = gbp * 1.4; return usd; @@ -18,15 +23,11 @@ function convertToUSD(gbp) { 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. */ -let total = 0.00; -let gbp = 0.00; -let brl = 0.00; function convertToBRL(gbp) { - -brl = ( gbp - (gbp*0.01)) * 5.7 ; -total = Number(brl).toFixed(2); -return total; +brl = gbp * 0.99 * 5.7 ; +brl = parseFloat(brl.toFixed(2)); +return brl; }