From de8014470007166e928cb18af3b7b4b82b5b13a8 Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:50:58 +0000 Subject: [PATCH 1/7] i solved mandatory practises --- mandatory/1-syntax-errors.js | 12 ++++++------ mandatory/2-logic-error.js | 8 ++++---- mandatory/3-function-output.js | 5 ++++- mandatory/4-tax.js | 11 +++++++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..6df0ed926 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,16 @@ // 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..568320e01 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; + multyple = a * b * c; + return multyple; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..8b26ce017 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,4 +1,5 @@ // Add comments to explain what this function does. You're meant to use Google! +//The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0 function getRandomNumber() { return Math.random() * 10; } @@ -7,10 +8,12 @@ function getRandomNumber() { function combine2Words(word1, word2) { return word1.concat(word2); } +// The concat() method joins two or more strings 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. + // 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..7d00a9db3 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,11 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(price) { +return price + (price/100)*20 ; + + +} /* CURRENCY FORMATTING @@ -17,7 +21,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) { + total=calculateSalesTax(price).toFixed(2) + return "£"+total; +} /* =================================================== From e35beb604bc23c5ca08d171cb12a74efa2fe3845 Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Mon, 20 Feb 2023 12:25:14 +0000 Subject: [PATCH 2/7] doing Extra practice --- extra/1-currency-conversion.js | 11 +++++++++-- extra/2-piping.js | 22 ++++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..79f4eada5 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(price) { + let total=price * 1.4; + return total; +} /* CURRENCY CONVERSION @@ -15,7 +18,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(price) { + let newprice=(price/100)*99; + let total=newprice*5.7; + 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..f24986709 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,27 +16,37 @@ the final result to the variable goodCode */ -function add() { - +function add(a,b) { +let sum=a+b; +return sum; } -function multiply() { +function multiply(a,b) { +let mult=a*b; +return mult; } -function format() { - +function format(num) { +return "£"+num; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +//const is local variabe and we cant use it in deferent functions + +let badCode = 12 4 2 + /* BETTER PRACTICE */ +//we have to use let to create variable let goodCode = + + + /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 64fa1e362ec71b1e1d4b69f3c76ea4cf10257644 Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Tue, 21 Feb 2023 18:40:28 +0000 Subject: [PATCH 3/7] working on practices --- .DS_Store | Bin 0 -> 8196 bytes extra/1-currency-conversion.js | 3 +-- extra/2-piping.js | 9 +++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..77ab3e457b427668703063bbd56898ee03711037 GIT binary patch literal 8196 zcmeHMT~8B16ukpQwo*+cn)JnF6JKdc|<%(+=-)|qo&)!)cbJ#2-;LABL?!L^2zetX|;wV!+4Q8Rn_o(#g4 z7j*lI;B~qf@^ZrqI&xH%!=N)z+`8^?(oVXW&F${qDKEIC^6k9^cehkrL}h8QxVM*f zu3Wo*cfGmo?*#Ir+A|4rWzYp(KPW%Jik#?Oy&w28*g`8a&eMHbhXO0KN{?X|XootH z?;EVo7v!iu^Ynn8&<5qH0nCF{gOx{XjULg{q&WodB05JlHbFemIknc!!xMo@N?uj%*19mipTyL<)z^X zHRaQ5zyyyL>=98$20nljVNXL?o61h&u@^^q4!kyi4|&L}N$klH?6rR!e;T}~eCx*V z`{ib7=^yTw2_0fS1Q~3y3Y>@n(|Xk$$N#mXzyF`e-t3}Q0jt3AC?HZR^_42t+W$j2 zzAVSuF7j(+PCRa`QBhE+a2%+@ap1u}3}L$%%AAV9u|~8Y9s7rXzlbmF`(OP;wm5?d F`~o1!l)(T1 literal 0 HcmV?d00001 diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 79f4eada5..d25c8ac2d 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -19,8 +19,7 @@ function convertToUSD(price) { */ function convertToBRL(price) { - let newprice=(price/100)*99; - let total=newprice*5.7; + let total=parseFloat((((price/100)*99)*5.7).toFixed(2)); return total; } diff --git a/extra/2-piping.js b/extra/2-piping.js index f24986709..27c031d13 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -32,17 +32,22 @@ return "£"+num; } const startingValue = 2; +let numberadd=10; +let numbermult=2; +add(startingValue,numberadd); +multiply(startingValue,numbermult); +format(startingValue); // Why can this code be seen as bad practice? Comment your answer. //const is local variabe and we cant use it in deferent functions -let badCode = 12 4 2 +let badCode ="my reply is no" /* BETTER PRACTICE */ //we have to use let to create variable -let goodCode = +let goodCode = //creating variable with let;// From 796a8720a6184c5427f749228140fcc6273980d7 Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Tue, 21 Feb 2023 22:31:06 +0000 Subject: [PATCH 4/7] change some code in extra --- extra/2-piping.js | 23 +++++++++---------- extra/3-magic-8-ball.js | 49 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index 27c031d13..eb1f7bfb8 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -31,26 +31,25 @@ function format(num) { return "£"+num; } -const startingValue = 2; -let numberadd=10; -let numbermult=2; -add(startingValue,numberadd); -multiply(startingValue,numbermult); -format(startingValue); + // Why can this code be seen as bad practice? Comment your answer. -//const is local variabe and we cant use it in deferent functions +//const is local variabe and we can't change its value +const startingValue = 2; -let badCode ="my reply is no" +let badCode =format(multiply(add(startingValue,10),2)); +console.log(badCode); /* BETTER PRACTICE */ -//we have to use let to create variable - -let goodCode = //creating variable with let;// - +//it is not easy to read and understand/ +//creating variable with let;// +let summ= add(startingValue,10); +let multiplyy=multiply(summ,2); +let goodCode=format(multiplyy); +/* we can understand this code better / /* ======= 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..6970fdba0 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,9 +45,43 @@ // This should log "The ball has shaken!" // and return the answer. +let answers=[ + "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 posetive=[ + +] + +console.log("ask a question") function shakeBall() { - //Write your code in here +console.log("The ball has shaken!"); +let search=Math.floor(Math.random() * answers.length); + let result=answers[search]; + return result; + } +let answer = shakeBall(); +console.log("The answer is " + answer); /* This function should say whether the answer it is given is @@ -58,9 +92,18 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { - //Write your code in here +function checkAnswer(check) { +if (answers.indexOf(check)<5) +return "very positive"; +else if (answers.indexOf(check) < 10) { + return "positive"; + } else if (answers.indexOf(check) < 15) { + return "negative"; + } else { + return "very negative"; } +} +console.log(checkAnswer(answer)); /* ================================== From c09afeb405b74ac75fbc31293ee44fa4088a660e Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Wed, 22 Feb 2023 20:35:05 +0000 Subject: [PATCH 5/7] i completed extra --- Untitled/.DS_Store | Bin 0 -> 6148 bytes extra/3-magic-8-ball.js | 15 ++------------- 2 files changed, 2 insertions(+), 13 deletions(-) create mode 100644 Untitled/.DS_Store diff --git a/Untitled/.DS_Store b/Untitled/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Wed, 22 Feb 2023 22:16:18 +0000 Subject: [PATCH 6/7] some changes --- extra/3-magic-8-ball.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index aa6d1f0ce..1c914fd4e 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -76,23 +76,21 @@ let search=Math.floor(Math.random() * answers.length); return result; } -/* -let answer = shakeBall(); -console.log("The answer is " + answer); -*/ +const answer=shakeBall(); -function checkAnswer(check) { -if (answers.indexOf(check)<5) +function checkAnswer(answer) { +if (answers.indexOf(answer)<5) return "very positive"; -else if (answers.indexOf(check) < 10) { +else if (answers.indexOf(answer) < 10) { return "positive"; - } else if (answers.indexOf(check) < 15) { + } else if (answers.indexOf(answer) < 15) { return "negative"; } else { return "very negative"; } -} -console.log(checkAnswer(answer)); +}; + + /* ================================== From 07b95ba3363b5bd2e183dfedea7e566d071bf8ef Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Wed, 22 Feb 2023 23:33:18 +0000 Subject: [PATCH 7/7] final extra i tried to change my codes --- extra/3-magic-8-ball.js | 137 +++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 57 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 1c914fd4e..d85419e68 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,40 +1,32 @@ /** - 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 + ## 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. @@ -43,75 +35,110 @@ Very doubtful. */ + +const veryPositive = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", +]; + +const positive = [ + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", +]; + +const negative = [ + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", +]; + +const veryNegative = [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; + +const answers = [ + "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.", +]; + // This should log "The ball has shaken!" // and return the answer. -let answers=[ - "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.", -] - -console.log("ask a question") function shakeBall() { -console.log("The ball has shaken!"); -let search=Math.floor(Math.random() * answers.length); - let result=answers[search]; - return result; -} -const answer=shakeBall(); + console.log("The ball has shaken!"); + let finalAnswer = Math.floor(Math.random() * answers.length); + return answers[finalAnswer]; +}; +/* + This function should say whether the answer it is given is + - very positive + - positive + - negative + - very negative + This function should expect to be called with any value which was returned by the shakeBall function. +*/ function checkAnswer(answer) { -if (answers.indexOf(answer)<5) -return "very positive"; -else if (answers.indexOf(answer) < 10) { + if (veryPositive.includes(answer)) { + return "very positive"; + } + else if(positive.includes(answer)) { return "positive"; - } else if (answers.indexOf(answer) < 15) { + } + else if (negative.includes(answer)) { return "negative"; - } else { + } + else { return "very negative"; -} + } }; - - /* ================================== ======= TESTS - DO NOT MODIFY ===== - There are some Tests in this file that will help you work out if your code is working. - To run the tests for just this one file, type `npm test -- --testPathPattern 3-magic-8-ball` into your terminal (Reminder: You must have run `npm install` one time before this will work!) ================================== */ +const { toBeOneOf } = require("jest-extended"); test("whole magic 8 ball sequence", () => { const consoleLogSpy = jest.spyOn(global.console, "log"); const answer = shakeBall(); - expect(typeof answer).toEqual("string"); - expect(consoleLogSpy).toHaveBeenCalledTimes(1); expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - expect(checkAnswer(answer)).toBeOneOf([ "very positive", "positive", @@ -119,7 +146,6 @@ test("whole magic 8 ball sequence", () => { "very negative", ]); }); - test("magic 8 ball returns different values each time", () => { const seenAnswers = new Set(); for (let i = 0; i < 10; ++i) { @@ -130,7 +156,6 @@ test("magic 8 ball returns different values each time", () => { "Expected to get different random answers each time shakeBall was called, but always got the same one" ); } - let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); if (seenPositivities.size < 2) { throw Error( @@ -138,11 +163,9 @@ test("magic 8 ball returns different values each time", () => { ); } }); - test("checkAnswer works for `It is decidedly so.`", () => { expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); }); - test("checkAnswer works for `My reply is no.`", () => { expect(checkAnswer("My reply is no.")).toEqual("very negative"); -}); +}); \ No newline at end of file