From 15b095ac1041ccc16a154c6e405a0b3aaacb7857 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 04:31:50 +0100 Subject: [PATCH 01/23] addNumbers Missing Commas Added the missing commas between addNumbers function arguments. --- week-1/2-mandatory/1-syntax-errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..71ae678 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -2,7 +2,7 @@ // 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; } From 1cc64e25ff67c1500ceed519dd716af1c78d7f7f Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 04:41:44 +0100 Subject: [PATCH 02/23] Fixed introduceMe function introduceMe function 1- missed the curly bracets 2- missed the + to concatenate the sentences 3- some spaces required to make the sentence readable --- week-1/2-mandatory/1-syntax-errors.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 71ae678..2e841b7 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -7,7 +7,9 @@ function addNumbers(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 getAddition(a, b) { total = a ++ b From 28676e564fd2223e93d7ba6f62c36e0e7dbe0c89 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 04:59:04 +0100 Subject: [PATCH 03/23] Fixed getAddition function 1- took off the additional (+) sign 2- string interpolation uses backstick instead of double quotes and a dollar sign instead of the percent sign. --- week-1/2-mandatory/1-syntax-errors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 2e841b7..4ab0e94 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -12,10 +12,10 @@ return "Hello, my name is " + name + " and I am " + age + " years old"; } function getAddition(a, b) { - total = a ++ b + total = a + b // Use string interpolation here - return "The total is %{total}" + return `The total is ${total}` } /* ======= TESTS - DO NOT MODIFY ===== */ From 648bd21af36b6d8cfdca0a6be66d008f45419ea9 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:12:57 +0100 Subject: [PATCH 04/23] Missing getRemainder function created the missing getRemainder --- week-1/2-mandatory/1-syntax-errors.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 4ab0e94..0ac73c1 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -18,6 +18,11 @@ function getAddition(a, b) { return `The total is ${total}` } +function getRemainder(a,b) +{ + return("The remainder is "+a%b) +} + /* ======= TESTS - DO NOT MODIFY ===== */ // // To run these tests type `node 1-syntax-errors.js` into your terminal From f27e588f1538043e32bc8489850454a6847152f7 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:19:43 +0100 Subject: [PATCH 05/23] Indented the code and took the comments out Indented the code and I took the comments out --- week-1/2-mandatory/1-syntax-errors.js | 55 ++++++++++++--------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 0ac73c1..7071bb0 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,43 +1,38 @@ - - -// There are syntax errors in this code - can you fix it to pass the tests? - -function addNumbers(a ,b ,c) { - return 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"; +function introduceMe(name, age) { + return "Hello, my name is " + name + " and I am " + age + " years old"; } function getAddition(a, b) { - total = a + b + total = a + b; - // Use string interpolation here - return `The total is ${total}` + return `The total is ${total}`; } -function getRemainder(a,b) -{ - return("The remainder is "+a%b) +function getRemainder(a, b) { + return "The remainder is " + (a % b); } -/* ======= TESTS - DO NOT MODIFY ===== */ -// -// To run these tests type `node 1-syntax-errors.js` into your terminal - function test(test_name, expr) { - let status; - if (expr) { - status = "PASSED" - } else { - status = "FAILED" - } - - console.log(`${test_name}: ${status}`) + let status; + if (expr) { + status = "PASSED"; + } else { + status = "FAILED"; + } + console.log(`${test_name}: ${status}`); } -test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) -test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old") -test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3") +test("fixed addNumbers function - case 1", addNumbers(3, 4, 6) === 13); +test( + "fixed introduceMe function", + introduceMe("Sonjide", 27) === + "Hello, my name is Sonjide and I am 27 years old" +); +test( + "fixed getRemainder function", + getRemainder(23, 5) === "The remainder is 3" +); From 2de905bbf9dde38e01e0fe8f680999f0cbc7f07e Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:22:01 +0100 Subject: [PATCH 06/23] Syntax errors fixed --- week-1/2-mandatory/1-syntax-errors.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 7071bb0..b9a44e5 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -1,3 +1,5 @@ +// Syntax errors fixed + function addNumbers(a, b, c) { return a + b + c; } From 00dccfe99d2bce8a8f0891313c7160270421fb2c Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:30:11 +0100 Subject: [PATCH 07/23] Logic errors -fixed trimWord function The function has to return the passed argument wich is word . --- week-1/2-mandatory/2-logic-error.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..d10abad 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,7 +1,7 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return wordtrim(); + return (word); } function getWordLength(word) { From a06cb3a5f1354ed7014c4261a1d0aef9eb3159fd Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:37:46 +0100 Subject: [PATCH 08/23] Logic errors- fixed getWordLength function The function has to return the length of the passed argument --- week-1/2-mandatory/2-logic-error.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index d10abad..9e5208a 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -5,7 +5,7 @@ function trimWord(word) { } function getWordLength(word) { - return "word".length() + return (word.length) } function multiply(a, b, c) { From 957da959a8c663e5fb4592e8d4256718d467bf0f Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 05:48:03 +0100 Subject: [PATCH 09/23] Logic errors-fixed multiply function multiply function returns the multiple of the three arguments. --- week-1/2-mandatory/2-logic-error.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 9e5208a..71e53de 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -9,8 +9,7 @@ function getWordLength(word) { } function multiply(a, b, c) { - a * b * c; - return; + return(a * b * c); } /* ======= TESTS - DO NOT MODIFY ===== From c47a3172557feda9d9b17753d5743cb058402ea0 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 06:00:35 +0100 Subject: [PATCH 10/23] Logic error- trimWord function fixed Trim word should return the text passed into the function without the white space using the method (trim()) --- week-1/2-mandatory/2-logic-error.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 71e53de..a6d4df3 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,7 +1,7 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return (word); + return (word.trim()); } function getWordLength(word) { From 6e5327c09c59668e3a877716a6027fe5dfca13a7 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 06:03:33 +0100 Subject: [PATCH 11/23] Logic errors fixed indented the code and took the comments off --- week-1/2-mandatory/2-logic-error.js | 32 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index a6d4df3..ca76532 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -1,34 +1,32 @@ -// The syntax for this function is valid but it has an error, find it and fix it. - function trimWord(word) { - return (word.trim()); + return word.trim(); } function getWordLength(word) { - return (word.length) + return word.length; } function multiply(a, b, c) { - return(a * b * c); + return a * b * c; } -/* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. - -To run these tests type `node 2-logic-error` into your terminal -*/ - function test(test_name, expr) { let status; if (expr) { - status = "PASSED" + status = "PASSED"; } else { - status = "FAILED" + status = "FAILED"; } - console.log(`${test_name}: ${status}`) + console.log(`${test_name}: ${status}`); } -test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture") -test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25) -test("fixed multiply function", multiply(2,3,6) === 36) \ No newline at end of file +test( + "fixed trimWord function", + trimWord(" CodeYourFuture ") === "CodeYourFuture" +); +test( + "fixed wordLength function", + getWordLength("A wild sentence appeared!") === 25 +); +test("fixed multiply function", multiply(2, 3, 6) === 36); From a467f4eae9791a25b1c33f6c33342bb7de67160b Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 06:33:43 +0100 Subject: [PATCH 12/23] getNumber output comment Added comment explained getNumber output --- week-1/2-mandatory/3-function-output.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index bbb88a2..35cded1 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,4 +1,9 @@ // Add comments to explain what this function does. You're meant to use Google! + +/****************************************** + * This function return a positive + * random number less than 9 including 0 + * ****************************************/ function getNumber() { return Math.random() * 10; } From 17aa903ccc9474ee92eedd1af55700a1f47f8f2c Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 07:07:22 +0100 Subject: [PATCH 13/23] Function s output Function S takes two strings and concatenate them together --- week-1/2-mandatory/3-function-output.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index 35cded1..59bbf70 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -9,6 +9,11 @@ function getNumber() { } // Add comments to explain what this function does. You're meant to use Google! + +/****************************************** + * This function takes two strings and + * join (concatenate) them together + * ****************************************/ function s(w1, w2) { return w1.concat(w2); } From fc519499a3713be7583b7c030c3c94e2a6ab3f53 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 07:19:21 +0100 Subject: [PATCH 14/23] Built Concatenate function --- week-1/2-mandatory/3-function-output.js | 1 + 1 file changed, 1 insertion(+) diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index 59bbf70..6a5d010 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -19,6 +19,7 @@ function s(w1, w2) { } function concatenate(firstWord, secondWord, thirdWord) { + return(firstWord+" ".concat(secondWord+" ",thirdWord)) // Write the body of this function to concatenate three words together // Look at the test case below to understand what to expect in return } From 1ff97dc86af3f2fe9861ea2ac5a0f1402ba11c67 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 07:24:13 +0100 Subject: [PATCH 15/23] Tidied the code Indented the code and took of the comments --- week-1/2-mandatory/3-function-output.js | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index 6a5d010..03c1fcf 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -1,17 +1,13 @@ -// Add comments to explain what this function does. You're meant to use Google! - /****************************************** - * This function return a positive + * This function return a positive * random number less than 9 including 0 * ****************************************/ function getNumber() { return Math.random() * 10; } -// Add comments to explain what this function does. You're meant to use Google! - /****************************************** - * This function takes two strings and + * This function takes two strings and * join (concatenate) them together * ****************************************/ function s(w1, w2) { @@ -19,17 +15,9 @@ function s(w1, w2) { } function concatenate(firstWord, secondWord, thirdWord) { - return(firstWord+" ".concat(secondWord+" ",thirdWord)) - // Write the body of this function to concatenate three words together - // Look at the test case below to understand what to expect in return + return firstWord + " ".concat(secondWord + " ", thirdWord); } -/* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. - -To run these tests type `node 3-function-output` into your terminal -*/ - function test(test_name, expr) { let status; if (expr) { From 508f4805806953f874509e24daf36eae6b8115f6 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 07:35:03 +0100 Subject: [PATCH 16/23] Buit calculateSalesTax function --- week-1/2-mandatory/4-tax.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 6b84208..987db46 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -5,7 +5,10 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax() { + let salesTax = 0.2*Price + return(salesTax) +} /* CURRENCY FORMATTING From c404fcc3546c2d9b72425ba7267a8bc08f9186ea Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 07:46:10 +0100 Subject: [PATCH 17/23] Built formatCurrency function --- week-1/2-mandatory/4-tax.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 987db46..3961bdf 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -20,7 +20,11 @@ function calculateSalesTax() { Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function formatCurrency() {} +function formatCurrency() { + let tax = calculateSalesTax(Price); + lastPrice = Price + tax; + return("£"+lastPrice.toFixed(2)) +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 5a39a5a08848500908d286023139bd0a7232db1e Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 08:20:12 +0100 Subject: [PATCH 18/23] Built currency function ,modified others built the formatCurrency function also built separated fuction to calculate the tax and the function calculateSalesTax(the last price ) wich add the tax to the price --- week-1/2-mandatory/4-tax.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 3961bdf..13fb9fa 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -4,12 +4,17 @@ A business requires a program that calculates how much sales tax to charge Sales tax is 20% of the price of the product */ - -function calculateSalesTax() { +function sTax(Price){ let salesTax = 0.2*Price - return(salesTax) +return(salesTax) +} + +function calculateSalesTax(Price) { + let tax= sTax(Price) + return(tax+Price) } + /* CURRENCY FORMATTING =================== @@ -20,8 +25,8 @@ function calculateSalesTax() { Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function formatCurrency() { - let tax = calculateSalesTax(Price); +function formatCurrency(Price) { + let tax = sTax(Price) lastPrice = Price + tax; return("£"+lastPrice.toFixed(2)) } From 4a208ccf71b53f4d788afdac305128c1aaae15c0 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 08:22:09 +0100 Subject: [PATCH 19/23] Tidied up the code Indentation + comments out --- week-1/2-mandatory/4-tax.js | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 13fb9fa..b90f379 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -1,42 +1,19 @@ -/* - SALES TAX - ========= - A business requires a program that calculates how much sales tax to charge - Sales tax is 20% of the price of the product -*/ -function sTax(Price){ - let salesTax = 0.2*Price -return(salesTax) +function sTax(Price) { + let salesTax = 0.2 * Price; + return salesTax; } function calculateSalesTax(Price) { - let tax= sTax(Price) - return(tax+Price) + let tax = sTax(Price); + return tax + Price; } - -/* - CURRENCY FORMATTING - =================== - The business has informed you that prices must have 2 decimal places - They must also start with the currency symbol - Write a function that transforms numbers into the format £0.00 - - Remember that the prices must include the sales tax (hint: you already wrote a function for this!) -*/ - function formatCurrency(Price) { - let tax = sTax(Price) + let tax = sTax(Price); lastPrice = Price + tax; - return("£"+lastPrice.toFixed(2)) + return "£" + lastPrice.toFixed(2); } -/* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. - -To run these tests type `node 4-tax.js` into your terminal -*/ - function test(test_name, expr) { let status; if (expr) { From 9ef94de885397333edf27d0069b066a078df4dd3 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 09:10:12 +0100 Subject: [PATCH 20/23] Done-Extra-currancy-conversion --- week-1/3-extra/1-currency-conversion.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 7f321d9..f5dd7b7 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-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) { + return(1.4*price) + +} /* CURRENCY FORMATTING @@ -16,7 +19,11 @@ function convertToUSD() {} Find a way to add 1% to all currency conversions (think about the DRY principle) */ -function convertToBRL() {} +function convertToBRL(price) { + let conv=5.7*price + let fee=0.057*price + return(conv+fee) +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 34b3b02d35004a0894f6ef08a2b9ff07b6aa13dd Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 09:43:33 +0100 Subject: [PATCH 21/23] Done Extra piping --- week-1/3-extra/2-piping.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..f531ddc 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -16,26 +16,30 @@ the final result to the variable goodCode */ -function add() { - +function add(a,b) { + let c=a+b + return parseFloat(c.toFixed(1)) } -function multiply() { - +function multiply(a,b) { +return(a*b) } -function format() { - +function format(price) { +return("£"+price) } const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(2*(add(startingValue,10))) /* BETTER PRACTICE */ +let sum=add(startingValue,10) +let mul=multiply(sum,2) +let form=format(mul) -let goodCode = +let goodCode = form /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 4fb0afcb6ec0b6d1fd177e2cd17152cdde319c11 Mon Sep 17 00:00:00 2001 From: hiba Date: Sun, 14 Jun 2020 11:26:51 +0100 Subject: [PATCH 22/23] Extra-Magic 8 ball- Done --- week-1/3-extra/3-magic-8-ball.js | 64 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/week-1/3-extra/3-magic-8-ball.js b/week-1/3-extra/3-magic-8-ball.js index 1bb1089..6865804 100644 --- a/week-1/3-extra/3-magic-8-ball.js +++ b/week-1/3-extra/3-magic-8-ball.js @@ -42,20 +42,76 @@ My sources say no. Outlook not so good. Very doubtful. */ - +var result; // This should log "The ball has shaken!" // and return the answer. -function shakeBall() {} +function shakeBall() { + console.log("The ball has shaken!"); + let num = Math.floor(Math.random() * answer.length); + result = answer[num]; + return result; +} // The answer should come from shaking the ball -let answer; +let answer = ["very positive", "positive", "negative", "very negative"]; // When checking the answer, we should tell someone if the answer is // - very positive // - positive // - negative // - very negative -function checkAnswer() {} +function checkAnswer() { + switch (result) { + case "very positive": { + let vP = [ + "It is certain", + "It is decidedly so", + "Without a doubt", + "Yes - definitely", + "You may rely on it", + ]; + let num = Math.floor(Math.random() * vP.length); + return vP[num]; + } + + case "positive": { + let p = [ + "It is certain", + "It is decidedly so", + "Without a doubt", + " Yes - definitely", + "You may rely on it", + ]; + let num = Math.floor(Math.random() * p.length); + return p[num]; + } + case "negative": { + let n = [ + "Reply hazy", + "try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again", + ]; + let num = Math.floor(Math.random() * n.length); + } + + case "very negative": { + let vN = [ + "Dont count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful", + ]; + let num = Math.floor(Math.random() * vN.length); + } + } + + let num = Math.floor(Math.random() * answer.length); + return answer[num]; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 8ce37fd406d371f133ff0d5bf920b8c17625ec76 Mon Sep 17 00:00:00 2001 From: hiba Date: Mon, 15 Jun 2020 08:40:19 +0100 Subject: [PATCH 23/23] Missed to add a comment I've added acomment to the bad practice .. I've missed it earlier --- week-1/3-extra/2-piping.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index f531ddc..9179823 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -12,7 +12,8 @@ - multiply the result by 2 - format it - 3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign + 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 */ @@ -32,6 +33,7 @@ return("£"+price) const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. +// The reader will take long time to understand whats going on let badCode = format(2*(add(startingValue,10))) /* BETTER PRACTICE */