From 6466854b39b3e3df374b19f1334f7f40be129be0 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 11:30:55 +0100 Subject: [PATCH 01/19] Finished Hello World exercise, experimented and answered the questions in readme file --- exercises/B-hello-world/README.md | 2 ++ exercises/B-hello-world/exercise.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 8c704fb43..8eaf8f0e3 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -15,4 +15,6 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". - Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. - Try to console.log() several things at once. - What happens when you get rid of the quote marks? + We get a syntax error. SyntaxError: missing ) after argument list - What happens when you console.log() just a number without quotes? + It logs a number to the console. Numbers are not like strings. They do not need quotes around them. Otherwise, they would turn into a string. diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..9d4ad15fc 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ console.log("Hello world"); +console.log("Hello World. I just started learning JavaScript!"); +console.log(12); From aac0473e46d680fe90706c9e8ceef5f241a9f1b6 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 11:35:34 +0100 Subject: [PATCH 02/19] Completed C-variables exercise by creating a variable and logging the variable three times to the console --- exercises/C-variables/exercise.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..c2925e716 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `greeting` +const greeting = "Hello world"; console.log(greeting); +console.log(greeting); +console.log(greeting); From fe1a98066ea767032a65536b7dca6e5b841c40d3 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 11:40:32 +0100 Subject: [PATCH 03/19] Finished Strings typeOf exercise by creating myMessage variable and another variable with its typeof and logging both variables to the console --- exercises/D-strings/exercise.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..713063b08 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` +const myMessage = "Hi, I am Humail"; +const myMessageType = typeof myMessage; -console.log(message); +console.log(myMessage); +console.log(myMessageType); From a9cc88c74f5e3853c0662620a0c6b258329bc05c Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 11:47:03 +0100 Subject: [PATCH 04/19] Finished exercise by creating two variables which concatenate in a third variable and logged the third variable to the console --- exercises/E-strings-concatenation/exercise.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..5062e0274 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +const greetingPartOne = "Hi, my name is "; +const firstName = "Humail"; -console.log(message); +const greetingConcatenated = greetingPartOne + firstName; + +console.log(greetingConcatenated); From b614a3a3ca62f33e4ec38823da823bd57d1dcd2b Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 14:29:06 +0100 Subject: [PATCH 05/19] completed both exercises by creating variables to concatenate the first one and .trim for the second one to use the given variable and count the characters. --- exercises/F-strings-methods/exercise.js | 10 +++++++++- exercises/F-strings-methods/exercise2.js | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..d651b333d 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,11 @@ // Start by creating a variable `message` +const firstName = "Humail"; +const lengthOfMyName = firstName.length; +const message = + "My name is " + + firstName + + " and my name is " + + lengthOfMyName + + " characters long."; -console.log(message); +console.log(message.trim()); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..f8bcad671 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,10 @@ const name = " Daniel "; +const lengthOfMyName = name.trim().length; +const message = + "My name is " + + name + + " and my name is " + + lengthOfMyName + + " characters long."; console.log(message); From 845b7b4e0816bb921f82331910414ff91e9ff105 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 14:38:05 +0100 Subject: [PATCH 06/19] completed numbers exercise by creating two variable for total number of students and mentors. Finally, created a third variable with template litterals to give a string including the totals --- exercises/G-numbers/exercise.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..20de184a5 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,8 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfStudents = 20; +const numberOfMentors = 10; +const totalNumbers = `There are ${numberOfStudents} students and ${numberOfMentors} mentors. In total, there are ${ + numberOfStudents + numberOfMentors +} students and mentors.`; + +console.log(totalNumbers); From b6bb4d0410d54a6f8457fbaf92ebec44ee524053 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 14:43:39 +0100 Subject: [PATCH 07/19] made changes to the exercise so they reflect the expected results. concept generally remains the same. --- exercises/G-numbers/exercise.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 20de184a5..6f740f72e 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1,8 +1,8 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` -const numberOfStudents = 20; -const numberOfMentors = 10; -const totalNumbers = `There are ${numberOfStudents} students and ${numberOfMentors} mentors. In total, there are ${ - numberOfStudents + numberOfMentors -} students and mentors.`; +const numberOfStudents = 15; +const numberOfMentors = 8; +const totalNumbers = numberOfStudents + numberOfMentors; -console.log(totalNumbers); +console.log(`Number of students: ${numberOfStudents}`); +console.log(`Number of mentors: ${numberOfMentors}`); +console.log(`Total number of students and mentors: ${totalNumbers}`); From 4923c3370a6ec22c8260472355e7246f9a8e5e9f Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 14:52:55 +0100 Subject: [PATCH 08/19] Completed floats exercise by creating variables for total and percentages of students and mentors. Logged it to console using Math.round inside of template literals --- exercises/I-floats/exercise.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..3831e6ff8 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,8 @@ var numberOfStudents = 15; var numberOfMentors = 8; +let totalNumbers = numberOfStudents + numberOfMentors; +let percentageStudents = (numberOfStudents / totalNumbers) * 100; +let percentageMentors = (numberOfMentors / totalNumbers) * 100; + +console.log(`Percentage students: ${Math.round(percentageStudents)}%`); +console.log(`Percentage mentors: ${Math.round(percentageMentors)}%`); From 743145573a50af5ac2a26fbc5c2d3facd5d6d3b4 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 14:59:01 +0100 Subject: [PATCH 09/19] completed both exercises for Functions by adding return code blocks inside of the functions to halve and triple the numbers respectively. --- exercises/J-functions/exercise.js | 5 +++++ exercises/J-functions/exercise2.js | 1 + 2 files changed, 6 insertions(+) diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..aa2ca8f2c 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,12 @@ function halve(number) { // complete the function here + return number / 2; } var result = halve(12); console.log(result); + +let resultTwo = halve(19); + +console.log(resultTwo); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..468d45596 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,6 @@ function triple(number) { // complete function here + return number * 3; } var result = triple(12); From 577c13ec70e11090a4e731660994cf81d18c0057 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 15:14:49 +0100 Subject: [PATCH 10/19] Completed all function parameters exercises by creating functions that take numbers and strings as parameters --- exercises/K-functions-parameters/exercise.js | 3 ++- exercises/K-functions-parameters/exercise2.js | 3 +++ exercises/K-functions-parameters/exercise3.js | 3 +++ exercises/K-functions-parameters/exercise4.js | 6 ++++-- exercises/K-functions-parameters/exercise5.js | 3 +++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..a4eed9d76 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1, num2) { // Calculate the result of the function and return it + return num1 * num2; } // Assign the result of calling the function the variable `result` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..0d0c4b3c0 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,4 +1,7 @@ // Declare your function first +function divide(num1, num2) { + return num1 / num2; +} var result = divide(3, 4); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..3341b2910 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,7 @@ // Write your function here +function createGreeting(name) { + return "Hello, my name is " + name; +} var greeting = createGreeting("Daniel"); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..57c2f51b0 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function addingNumbers(num1, num2) { + return num1 + num2; +} // Call the function and assign to a variable `sum` - +let sum = addingNumbers(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..8973b5d54 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,7 @@ // Declare your function here +function createLongGreeting(name, age) { + return `Hello, my name is ${name} and I'm ${age} years old`; +} const greeting = createLongGreeting("Daniel", 30); From c904d863eed783129708cf1b2076415074a448b6 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Mon, 21 Jun 2021 15:40:47 +0100 Subject: [PATCH 11/19] completed nested function exercise by creating a function which works inside another function. --- exercises/L-functions-nested/exercise.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..7110f1212 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,19 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function createUppercaseNames(string) { + return string.toUpperCase(); +} + +function createShoutyGreeting(name) { + let mentorName = createUppercaseNames(name); + let message = "HELLO " + mentorName; + return message; +} + +console.log(createShoutyGreeting(mentor1)); +console.log(createShoutyGreeting(mentor2)); +console.log(createShoutyGreeting(mentor3)); +console.log(createShoutyGreeting(mentor4)); +console.log(createShoutyGreeting(mentor5)); From 4868a4b10e153d60eafee778532bbeba4da95714 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 00:56:38 +0100 Subject: [PATCH 12/19] Fixed syntax errors in mandatory exercise 1 and tests pass --- mandatory/1-syntax-errors.js | 11 ++++++----- package.json | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..98360ac6b 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 " + name "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/package.json b/package.json index 58555936d..778df9683 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/issues" }, "jest": { - "setupFilesAfterEnv": ["jest-extended"] + "setupFilesAfterEnv": [ + "jest-extended" + ] }, "homepage": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1#readme", "devDependencies": { From 10d2c3eed1f38e567712d1e5a3d33f491480a071 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 01:05:15 +0100 Subject: [PATCH 13/19] Fixed .trim(), .length, return a*b*c in logic errors exercise 2 in mandatory folder --- mandatory/2-logic-error.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..37c4ebc15 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // The syntax for this function is valid but it has an error, find it and fix it. 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; } /* From 9e0181e5b4b9df4c5c3bf248c9359558613b6a7a Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 01:24:46 +0100 Subject: [PATCH 14/19] Explained what the first two function do and added code block to function 3 where three parameters are concatenated using the + operator. --- mandatory/3-function-output.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..0dd2a0a80 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! +// Whenever this function is called, it generates a random number and multiplies it by 10. function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +//This function takes two parameters which are strings and concatenates them into one string. function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +13,8 @@ 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. + // We could not use concat() because it only accepts strings. It would not meet all conditions of the test. + return firstWord + " " + secondWord + " " + thirdWord; } /* From bd7e90fc446417900f6588a7fb33ba98bd2f7dbc Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 01:40:58 +0100 Subject: [PATCH 15/19] added code to first function for price + price *0.2 and second function calling first function to give total and returning value in pounds with toFixed at 2 decimals --- mandatory/4-tax.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..cf7b4798b 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 price + price * 0.2; +} /* CURRENCY FORMATTING @@ -17,7 +19,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) { + let totalAmount = calculateSalesTax(price); + return `£${totalAmount.toFixed(2)}`; +} /* =================================================== From 43ab58b01688a9e1f541425e605264d5849d6d8f Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 02:13:53 +0100 Subject: [PATCH 16/19] completed extra 1 - converted pound to USD by calling function with pound *1.4, second function used parseFloat to return decimal number value with toFixed at 2 decimals and calculated 99% of total by multiplying by 0.99 --- extra/1-currency-conversion.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index d82e59480..b57907298 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(pound) { + return pound * 1.4; +} /* CURRENCY CONVERSION @@ -15,7 +17,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(pound) { + let percentageToConvert = pound * 0.99; + let totalBrazilianAmount = percentageToConvert * 5.7; + return parseFloat(totalBrazilianAmount.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 96fc41413dc82b6ffe28af13349d9ea1bffedef5 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Tue, 22 Jun 2021 10:04:40 +0100 Subject: [PATCH 17/19] completed extra exercise 2 by returning all three functions and declaring badCode variable and two more variables to go along goodCode variable. Added parseFloat toFixed at 1 decimal so it meets the requirements to cater decimal numbers with 1 decimal --- extra/2-piping.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index 9c8ebc76a..77fddb501 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,27 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return num1 + num2; } -function multiply() { - +function multiply(a, b) { + return a * b; } -function format() { - +function format(amount) { + return `£${parseFloat(amount.toFixed(1))}`; } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(multiply(add(startingValue, 10), 2)); /* BETTER PRACTICE */ - -let goodCode = +let addedNumbers = add(startingValue, 10); +let multipliedNumbers = multiply(addedNumbers, 2); +let goodCode = format(multipliedNumbers); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 397457f374fb916e415bd0dc460c3d5bdb2520e2 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Wed, 23 Jun 2021 11:33:35 +0100 Subject: [PATCH 18/19] Completed the magic ball 8 extra challenge by generating a random number for shakeball function and using else if statements for both functions as conditions to pass the tests --- extra/3-magic-8-ball.js | 90 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index f3adbefa5..3cbce79f6 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,11 +43,61 @@ Very doubtful. */ +let veryPositive = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", +]; + +let positive = [ + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", +]; + +let negative = [ + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", +]; + +let veryNegative = [ + "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. function shakeBall() { //Write your code in here + let randomNumber = Math.floor(Math.random() * 4); + + console.log("The ball has shaken!"); + + if (randomNumber === 0) { + let index = Math.floor(Math.random() * veryPositive.length); + return veryPositive[index]; + } else if (randomNumber === 1) { + index = Math.floor(Math.random() * positive.length); + return positive[index]; + } else if (randomNumber === 2) { + index = Math.floor(Math.random() * negative.length); + return negative[index]; + } else if (randomNumber === 3) { + index = Math.floor(Math.random() * veryNegative.length); + return veryNegative[index]; + } } +let ans = shakeBall(); /* This function should say whether the answer it is given is @@ -58,9 +108,45 @@ 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 + + if ( + answer === "It is certain." || + answer === "It is decidedly so." || + answer === "Without a doubt." || + answer === "Yes - definitely." || + answer === "You may rely on it." + ) { + return "very positive"; + } else if ( + answer === "As I see it, yes." || + answer === "Most likely." || + answer === "Outlook good." || + answer === "Yes." || + answer === "Signs point to yes." + ) { + return "positive"; + } else if ( + answer === "Reply hazy, try again." || + answer === "Ask again later." || + answer === "Better not tell you now." || + answer === "Cannot predict now." || + answer === "Concentrate and ask again." + ) { + return "negative"; + } else if ( + answer === "Don't count on it." || + answer === "My reply is no." || + answer === "My sources say no." || + answer === "Outlook not so good." || + answer === "Very doubtful." + ) { + return "very negative"; + } } +checkAnswer(ans); /* ================================== @@ -101,7 +187,9 @@ test("magic 8 ball returns different values each time", () => { ); } - let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); + let seenPositivities = new Set( + Array.from(seenAnswers.values()).map(checkAnswer) + ); if (seenPositivities.size < 2) { throw Error( "Expected to random answers with different positivities each time shakeBall was called, but always got the same one" From a4df901edec07f7759b0941db2cc8d6192adf817 Mon Sep 17 00:00:00 2001 From: humailhasankhan Date: Fri, 25 Jun 2021 16:18:47 +0100 Subject: [PATCH 19/19] Refactored the code with the help of one of the mentors and made it much shorter --- extra/3-magic-8-ball.js | 59 +++++++++-------------------------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 3cbce79f6..9821141e6 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,7 +43,7 @@ Very doubtful. */ -let veryPositive = [ +const veryPositive = [ "It is certain.", "It is decidedly so.", "Without a doubt.", @@ -51,7 +51,7 @@ let veryPositive = [ "You may rely on it.", ]; -let positive = [ +const positive = [ "As I see it, yes.", "Most likely.", "Outlook good.", @@ -59,7 +59,7 @@ let positive = [ "Signs point to yes.", ]; -let negative = [ +const negative = [ "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", @@ -67,7 +67,7 @@ let negative = [ "Concentrate and ask again.", ]; -let veryNegative = [ +const veryNegative = [ "Don't count on it.", "My reply is no.", "My sources say no.", @@ -75,27 +75,18 @@ let veryNegative = [ "Very doubtful.", ]; +const allAnswers = veryPositive.concat(positive, negative, veryNegative); + // This should log "The ball has shaken!" // and return the answer. function shakeBall() { //Write your code in here - let randomNumber = Math.floor(Math.random() * 4); console.log("The ball has shaken!"); - if (randomNumber === 0) { - let index = Math.floor(Math.random() * veryPositive.length); - return veryPositive[index]; - } else if (randomNumber === 1) { - index = Math.floor(Math.random() * positive.length); - return positive[index]; - } else if (randomNumber === 2) { - index = Math.floor(Math.random() * negative.length); - return negative[index]; - } else if (randomNumber === 3) { - index = Math.floor(Math.random() * veryNegative.length); - return veryNegative[index]; - } + const answerIndex = Math.floor(Math.random() * allAnswers.length); + + return allAnswers[answerIndex]; } let ans = shakeBall(); @@ -112,37 +103,13 @@ let ans = shakeBall(); function checkAnswer(answer) { //Write your code in here - if ( - answer === "It is certain." || - answer === "It is decidedly so." || - answer === "Without a doubt." || - answer === "Yes - definitely." || - answer === "You may rely on it." - ) { + if (veryPositive.includes(answer)) { return "very positive"; - } else if ( - answer === "As I see it, yes." || - answer === "Most likely." || - answer === "Outlook good." || - answer === "Yes." || - answer === "Signs point to yes." - ) { + } else if (positive.includes(answer)) { return "positive"; - } else if ( - answer === "Reply hazy, try again." || - answer === "Ask again later." || - answer === "Better not tell you now." || - answer === "Cannot predict now." || - answer === "Concentrate and ask again." - ) { + } else if (negative.includes(answer)) { return "negative"; - } else if ( - answer === "Don't count on it." || - answer === "My reply is no." || - answer === "My sources say no." || - answer === "Outlook not so good." || - answer === "Very doubtful." - ) { + } else if (veryNegative.includes(answer)) { return "very negative"; } }