From 091370b9e2494ba45f14830046d8aa403ed83905 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Mon, 20 Feb 2023 13:14:32 +0000 Subject: [PATCH 1/5] Completed the mandatory part and most of extras Completed the mandatory part and most of extras . Need help in the last task in extra. --- .github/pull_request_template.md | 12 ++++---- .vscode/settings.json | 4 +++ extra/1-currency-conversion.js | 9 ++++-- extra/2-piping.js | 20 ++++++++----- extra/3-magic-8-ball.js | 50 ++++++++++++++++++++++++++++++-- index.js | 1 + mandatory/1-syntax-errors.js | 12 ++++---- mandatory/2-logic-error.js | 7 ++--- mandatory/3-function-output.js | 3 ++ mandatory/4-tax.js | 8 +++-- 10 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 index.js diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 46c150e15..a8d9ffba3 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -16,18 +16,18 @@ Please complete the details below this message # Your Details -- Your Name: -- Your City: -- Your Slack Name: +- Your Name: Afsha Hossain +- Your City: London +- Your Slack Name: Afsha10 # Homework Details -- Module: -- Week: +- Module: JS +- Week: 1 # Notes -- What did you find easy? +- What did you find easy? - What did you find hard? diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..fe95ac313 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.formatOnPaste": true, + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..b7c879957 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(price) { + return price * 1.4; +} /* CURRENCY CONVERSION @@ -15,7 +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(price) { + price = price * 0.99 * 5.7; + return parseFloat(price.toFixed(2)); +} /* ======= 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..b62cc230d 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,30 @@ the final result to the variable goodCode */ -function add() { - +function add(number1, number2) { + return number1 + number2; } -function multiply() { - +function multiply(number1, number2) { + return number1 * number2; } -function format() { - +function format(number) { + return "£" + number; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = "£" + ((10 + startingValue) * 2) /* BETTER PRACTICE */ -let goodCode = +const betterStartingValue = 2; +additionResult = betterStartingValue + 10; +multiplicationResult = additionResult * 2; + +let goodCode = "£" + multiplicationResult; /* ======= 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..02d059108 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,8 +45,12 @@ // This should log "The ball has shaken!" // and return the answer. -function shakeBall() { - //Write your code in here + +function shakeBall(herebetterStartingValue) { + answerRandom = Math.floor(Math.random() * 16); + return answerRandom; + //Write your code in herebetterStartingValue + } /* @@ -58,10 +62,50 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { + +const answer = [ + "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." +]; + +function checkAnswer() { + if answer = "It is certain." || "It is decidedly so." || "Without a doubt." || "Yes - definitely." || "You may rely on it." { + return "The answer is very positive: ${randomAnswer}"; + } else if answer = "As I see it, yes." || "Most likely." || "Outlook good." || "Yes." || "Signs point to yes." { + "The answer is positive: ${randomAnswer}"; + } else if answer = "Reply hazy, try again." || "Ask again later." || "Better not tell you now." || "Cannot predict now." || "Concentrate and ask again." { + "The answer is negative: ${randomAnswer}"; + } else { + return "The answer is very negative: ${randomAnswer}"; + } + + //Write your code in here } +// "very positive", +// "positive", +// "negative", +// "very negative" + /* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/index.js b/index.js new file mode 100644 index 000000000..8db055fbf --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +console.log("Hello World again"); diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..0cbe7c893 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 "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..85b4adff0 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // 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..dd17dc694 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 Return a random number between 0 and 10: function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// The concat() function in JavaScript is a string method that is used to combine two strings into a new string. It does not modify the original string, but returns a new string that is the result of joining the original string with one additional strings. 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 + " " + secondWord + " " + thirdWord; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..25aa9c245 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,9 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + return 1.20 * price; +} /* 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(price) { + return "£" + calculateSalesTax(price).toFixed(2); +} /* =================================================== From 35255822b0b67624101571d7330d0dd0093f1bee Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Thu, 23 Feb 2023 00:16:31 +0000 Subject: [PATCH 2/5] Made some changes to the mandatory and extra sections --- extra/2-piping.js | 5 +++-- mandatory/3-function-output.js | 4 ++-- mandatory/4-tax.js | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index b62cc230d..b2bfe9723 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -25,12 +25,13 @@ function multiply(number1, number2) { } function format(number) { - return "£" + number; + return `£${number}`; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. +// It's bad code because the functions are not clearly defined systematically or step by step. let badCode = "£" + ((10 + startingValue) * 2) /* BETTER PRACTICE */ @@ -39,7 +40,7 @@ const betterStartingValue = 2; additionResult = betterStartingValue + 10; multiplicationResult = additionResult * 2; -let goodCode = "£" + multiplicationResult; +let goodCode = `£${multiplicationResult}`; /* ======= 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/3-function-output.js b/mandatory/3-function-output.js index dd17dc694..e09bba6a3 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,5 +1,5 @@ // Add comments to explain what this function does. You're meant to use Google! -// This function Return a random number between 0 and 10: +// This function returns a random number between 0 and 10: function getRandomNumber() { return Math.random() * 10; } @@ -13,7 +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 + " " + secondWord + " " + thirdWord; + return `${firstWord } ${secondWord} ${thirdWord}`; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 25aa9c245..fe33bc77c 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -6,7 +6,7 @@ */ function calculateSalesTax(price) { - return 1.20 * price; + return 1.2 * price; } /* @@ -20,7 +20,7 @@ function calculateSalesTax(price) { */ function addTaxAndFormatCurrency(price) { - return "£" + calculateSalesTax(price).toFixed(2); + return `£${calculateSalesTax(price).toFixed(2)}`; } /* From 6c324c5e976ec75ba12213dd64bee19451e9bab0 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Fri, 24 Feb 2023 23:18:01 +0000 Subject: [PATCH 3/5] Made some changes to variable names for better review --- extra/1-currency-conversion.js | 4 ++-- extra/3-magic-8-ball.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index b7c879957..957484d68 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -18,8 +18,8 @@ function convertToUSD(price) { */ function convertToBRL(price) { - price = price * 0.99 * 5.7; - return parseFloat(price.toFixed(2)); + let priceCoverted = price * 0.99 * 5.7; + return parseFloat(priceCoverted.toFixed(2)); } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 02d059108..227d1508e 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -47,7 +47,7 @@ // and return the answer. function shakeBall(herebetterStartingValue) { - answerRandom = Math.floor(Math.random() * 16); + answerRandom = Math.floor(Math.random() * allAnswers.length); return answerRandom; //Write your code in herebetterStartingValue From 03ad6b18c0929bf061112e57f984789f6625389d Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 1 Mar 2023 19:06:35 +0000 Subject: [PATCH 4/5] COrrected Magic ball --- extra/3-magic-8-ball.js | 87 +++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 227d1508e..c4a8ee961 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,60 +46,79 @@ // This should log "The ball has shaken!" // and return the answer. -function shakeBall(herebetterStartingValue) { - answerRandom = Math.floor(Math.random() * allAnswers.length); - return answerRandom; - //Write your code in herebetterStartingValue - -} - -/* - 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. -*/ -const answer = [ - "It is certain.", +const veryPositive = + ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", - "You may rely on it.", + "You may rely on it."] + +const positive = [ + "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", - "Signs point to 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.", - "Don't count on it.", + "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." -]; - -function checkAnswer() { - if answer = "It is certain." || "It is decidedly so." || "Without a doubt." || "Yes - definitely." || "You may rely on it." { - return "The answer is very positive: ${randomAnswer}"; - } else if answer = "As I see it, yes." || "Most likely." || "Outlook good." || "Yes." || "Signs point to yes." { - "The answer is positive: ${randomAnswer}"; - } else if answer = "Reply hazy, try again." || "Ask again later." || "Better not tell you now." || "Cannot predict now." || "Concentrate and ask again." { - "The answer is negative: ${randomAnswer}"; +] + +const allAnswers = [...veryPositive, ...positive, ...negative, ...veryNegative] + +function shakeBall() { + console.log("The ball has shaken!"); + numberRandom = Math.floor(Math.random() * allAnswers.length); + return allAnswers[numberRandom]; + //Write your code in herebetterStartingValue + +} + +/* + 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 (veryPositive.includes(answer)) { + return "very positive"; + } else if (positive.includes(answer)) { + return "positive"; + } else if (negative.includes(answer)) { + return "negative"; } else { - return "The answer is very negative: ${randomAnswer}"; + return "very negative" } - +} + + //Write your code in here -} + // "very positive", // "positive", From 8a7c77a115816d6ec1af674167e1d8676fdc1c4f Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Sun, 26 Mar 2023 21:45:12 +0100 Subject: [PATCH 5/5] Made some changes, added some comments for my understanding and made a few corrections based on the reviewers comments. --- Terminal-Coursework-Week1 | 1 + extra/1-currency-conversion.js | 5 ++-- extra/2-piping.js | 21 ++++++++------- extra/3-magic-8-ball.js | 49 +++++++++++++++++----------------- mandatory/3-function-output.js | 10 ++++--- mandatory/4-tax.js | 3 ++- 6 files changed, 49 insertions(+), 40 deletions(-) create mode 160000 Terminal-Coursework-Week1 diff --git a/Terminal-Coursework-Week1 b/Terminal-Coursework-Week1 new file mode 160000 index 000000000..58201d43c --- /dev/null +++ b/Terminal-Coursework-Week1 @@ -0,0 +1 @@ +Subproject commit 58201d43c40492fd45cda90595a028cfc20c9070 diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 957484d68..c1d1a0dbb 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -18,8 +18,9 @@ function convertToUSD(price) { */ function convertToBRL(price) { - let priceCoverted = price * 0.99 * 5.7; - return parseFloat(priceCoverted.toFixed(2)); + let priceAfterFee = price * 0.99; + let priceConvertedToBRL = priceAfterFee * 5.7; + return parseFloat(priceConvertedToBRL.toFixed(2)); // parseFloat converts into a floating number; we could also use Math.round(num * 100) / 100 i.e Math.round(priceConvertedToBRL * 100) / 100 } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/extra/2-piping.js b/extra/2-piping.js index b2bfe9723..1828d91e4 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,31 +16,32 @@ the final result to the variable goodCode */ -function add(number1, number2) { - return number1 + number2; +function add(a, b) { + return a + b; } -function multiply(number1, number2) { - return number1 * number2; +function multiply(a, b) { + return a * b; } -function format(number) { - return `£${number}`; +function format(value) { + return `£${value}`; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. // It's bad code because the functions are not clearly defined systematically or step by step. -let badCode = "£" + ((10 + startingValue) * 2) +let badCode = "£" + ((10 + startingValue) * 2) // we could also write let badCode = format(multiply(add(startingValue, 10), 2)); /* BETTER PRACTICE */ const betterStartingValue = 2; -additionResult = betterStartingValue + 10; -multiplicationResult = additionResult * 2; +additionResult = betterStartingValue + 10; // or we could write: let sum = add(startingValue, 10); -let goodCode = `£${multiplicationResult}`; +multiplicationResult = additionResult * 2; // we could also write: let doubledSum = multiply(sum, 2); + +let goodCode = `£${multiplicationResult}`; // connected to above comments, we could also write: let goodCode = format(doubledSum); /* ======= 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 c4a8ee961..77e9bcd29 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -47,46 +47,52 @@ // and return the answer. -const veryPositive = +const veryPositiveAnswers = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it."] -const positive = [ - +const positiveAnswers = [ "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes."] -const negative = [ +const negativeAnswers = [ "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.", +const veryNegativeAnswers = [ + "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful." ] -const allAnswers = [...veryPositive, ...positive, ...negative, ...veryNegative] +const allAnswers = [...veryPositiveAnswers, ...positiveAnswers, ...negativeAnswers, ...veryNegativeAnswers] function shakeBall() { console.log("The ball has shaken!"); - numberRandom = Math.floor(Math.random() * allAnswers.length); - return allAnswers[numberRandom]; - //Write your code in herebetterStartingValue - + let randomIndex = Math.floor(Math.random() * allAnswers.length); + return allAnswers[randomIndex]; } +/* +The line "randomIndex = Math.floor(Math.random() * allAnswers.length);" in our solution generates a random integer between 0 (inclusive) and the length of the array allAnswers (exclusive). + +Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive), and multiplying it by allAnswers.length gives us a random number between 0 (inclusive) and the length of the array. +Math.floor() then rounds down the result to the nearest integer, so that randomIndex is an integer between 0 (inclusive) and the last index of the array allAnswers (exclusive). + +This line of code is used in the shakeBall() function to select a random answer from the allAnswers array, so that each time the function is called, it returns a different answer. +*/ + /* This function should say whether the answer it is given is - very positive @@ -98,20 +104,15 @@ function shakeBall() { */ - - - - - function checkAnswer(answer) { - if (veryPositive.includes(answer)) { + if (veryPositiveAnswers.includes(answer)) { return "very positive"; - } else if (positive.includes(answer)) { + } else if (positiveAnswers.includes(answer)) { return "positive"; - } else if (negative.includes(answer)) { + } else if (negativeAnswers.includes(answer)) { return "negative"; - } else { - return "very negative" + } else if (veryNegativeAnswers.includes(answer)) { + return "very negative"; } } @@ -121,9 +122,9 @@ function checkAnswer(answer) { // "very positive", -// "positive", -// "negative", -// "very negative" +// "positive", +// "negative", +// "very negative" /* ================================== diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index e09bba6a3..6f73ded7a 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,11 +1,15 @@ // Add comments to explain what this function does. You're meant to use Google! -// This function returns a random number between 0 and 10: +// This function Math.random returns a random number between 0 and 10: +// We then take this random value and 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() function in JavaScript is a string method that is used to combine two strings into a new string. It does not modify the original string, but returns a new string that is the result of joining the original string with one additional strings. +// The concat() function is used to combine two strings into a new string. +// It does not modify the original string, but returns a new string that is the result of joining the original string with one additional string. +// The concat function will add the argument on to the end of the string. +// The combine2Words() function will return a string with the word2 added to the end of word1. function combine2Words(word1, word2) { return word1.concat(word2); } @@ -13,7 +17,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 } ${secondWord} ${thirdWord}`; + return `${firstWord} ${secondWord} ${thirdWord}`; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index fe33bc77c..521668a9a 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -20,7 +20,8 @@ function calculateSalesTax(price) { */ function addTaxAndFormatCurrency(price) { - return `£${calculateSalesTax(price).toFixed(2)}`; + let priceWithTax = calculateSalesTax(price); + return `£${priceWithTax.toFixed(2)}`; } /*