From 9aac1968e8c5069c25f4e11a42f233c63f610a44 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:02:43 +0000 Subject: [PATCH 01/23] Completed Ex b hello world Completed the exercises in b --- exercises/B-hello-world/exercise.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..3e923cfb8 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,15 @@ console.log("Hello world"); + +//* Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. +console.log('Hello World. I just started learning JavaScript!'); + +// * Try to console.log() several things at once. +let greeting = "Hello"; +let firstName = "Kara"; +console.log(`${greeting} ${firstName}`); + +//* What happens when you get rid of the quote marks? +//console.log(Hello, World!); SyntaxError: missing ) after argument list + +//* What happens when you console.log() just a number without quotes? +console.log(12345); From d524e42975b30333b138681db4318f60d81c951a Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:06:37 +0000 Subject: [PATCH 02/23] Completed Ex c Variables Completed exercise c Variables --- exercises/C-variables/exercise.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..4a7dd9e11 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `greeting` - -console.log(greeting); +var greeting = "Welcome to CYF!"; +//* Print your `greeting` to the console 3 times +for (i = 0; i < 3; i++){ + console.log(greeting); +} \ No newline at end of file From 21bb8c8270735116ce75e558abc83a00be60a906 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:41:36 +0000 Subject: [PATCH 03/23] Updated Ex c Changed message --- exercises/C-variables/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index 4a7dd9e11..da6348264 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,5 +1,5 @@ // Start by creating a variable `greeting` -var greeting = "Welcome to CYF!"; +var greeting = "Hello world"; //* Print your `greeting` to the console 3 times for (i = 0; i < 3; i++){ console.log(greeting); From 70903a56915a51a020a9e274e9683a4dcee49077 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:41:58 +0000 Subject: [PATCH 04/23] Completed Ex d Strings Completed Exercise D --- exercises/D-strings/exercise.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..aa5c65ca9 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +//* Write a program that logs a message and its type +let message = "This is a string"; console.log(message); +console.log(typeof message); From 35679852c0a97ce875e53dd6e51f0f0bb45761c0 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:46:20 +0000 Subject: [PATCH 05/23] Completed Ex e Concatenation Completed Exercise E --- exercises/E-strings-concatenation/exercise.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..999734039 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` - +//* Write a program that logs a message with a greeting and your name +let greetingStart = "Hello, my name is "; +let myName = "Kara"; +let message = greetingStart + myName; console.log(message); From a07a5f425392915aaecaba1cb85b7f1494ee323b Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 17:59:29 +0000 Subject: [PATCH 06/23] Completed Ex f String Methods Completed Exercise F String Methods --- exercises/F-strings-methods/exercise.js | 4 +++- exercises/F-strings-methods/exercise2.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..c26eca748 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +//* Log a message that includes the length of your name +let myName = "Kara"; +let message = `My name is ${myName} and my name is ${myName.length} characters long`; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..9f9afa155 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ -const name = " Daniel "; +//* Log the same message using the variable, `name` provided +//* Use the `.trim` method to remove the extra whitespace -console.log(message); +const name = " Daniel "; +let message = `My name is ${name.trim()} and my name is ${name.length} characters long`; +console.log(message); \ No newline at end of file From 8ed0f2fe2211bc47ec27079217b6ed60a58e17f8 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 18:04:15 +0000 Subject: [PATCH 07/23] Completed Ex g Numbers Completed Exercise G Numbers --- exercises/G-numbers/exercise.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..c0ac27e99 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +//* Create two variables `numberOfStudents` and `numberOfMentors` +//* Log a message that displays the total number of students and mentors +let numberOfStudents = 15; +let numberOfMentors = 8; +let total = numberOfStudents + numberOfMentors +console.log(`Number of Students: ${numberOfStudents}\nNumber of Mentors: ${numberOfMentors}\nTotal number of students and mentors: ${total}`); \ No newline at end of file From 0bb67e1ddb9b4f1612d0465c1aa83ff6726be895 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 18:11:53 +0000 Subject: [PATCH 08/23] Completed Ex i Floats Completed Exercise i Floats --- exercises/I-floats/exercise.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..284da2c4e 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,9 @@ var numberOfStudents = 15; var numberOfMentors = 8; + +//* Using the variables provided in the exercise calculate the percentage of mentors and students in the group +let total = numberOfStudents + numberOfMentors; +let percentageStudents = Math.round((numberOfStudents / total) * 100); +let percentageMentors = Math.round((numberOfMentors / total) * 100); +console.log(`Percentage students: ${percentageStudents}%`); +console.log(`Percentage mentors: ${percentageMentors}%`); \ No newline at end of file From f17533b3a947e2a709b8cd9497869d0925270900 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 18:23:27 +0000 Subject: [PATCH 09/23] Completed Ex j Functions Completed Exercise J Functions --- exercises/J-functions/exercise.js | 10 ++++++++-- exercises/J-functions/exercise2.js | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..c8b0373db 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,13 @@ +//* Complete the function in exercise.js so that it halves the input +//* Try calling the function more than once with some different numbers + function halve(number) { - // complete the function here + return number / 2; } var result = halve(12); - console.log(result); +result = halve(14); +console.log(result); +result = halve(16); +console.log(result); \ No newline at end of file diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..6deefdc5d 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,7 @@ +//* Complete the function in exercise2.js so that it triples the input + function triple(number) { - // complete function here + return number * 3; } var result = triple(12); From 661c538f97f0114662bfdf6ae17f6431a6a10f75 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 18:39:37 +0000 Subject: [PATCH 10/23] Completed Ex k Functions-parameters Completed Exercise K Functions-parameters --- exercises/K-functions-parameters/exercise.js | 4 +++- exercises/K-functions-parameters/exercise2.js | 5 ++++- exercises/K-functions-parameters/exercise3.js | 5 ++++- exercises/K-functions-parameters/exercise4.js | 10 +++++++--- exercises/K-functions-parameters/exercise5.js | 4 ++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..768d0b9c0 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,8 @@ +//* Write a function that multiplies two numbers together // 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..4eafe7a0a 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,8 @@ +//* From scratch, write a function that divides two numbers // Declare your function first - +function divide(num1, num2){ + return num1 / num2; +} var result = divide(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..dec5ecbf7 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,8 @@ +//* Write a function that takes a name (a string) and returns a greeting // Write your function here - +function createGreeting(name){ + return `Hello, my name is ${name}`; +} var greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..3b2a4473e 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ +//* Write a function that adds two numbers together +//* Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` // Declare your function first - +function addNumbers(num1, num2){ + return num1 + num2; +} // Call the function and assign to a variable `sum` - -console.log(sum); +let sum = addNumbers(13, 124); +console.log(sum); \ No newline at end of file diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..52046558c 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,8 @@ +//* Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) // Declare your function here +function createLongGreeting(name, age){ + return `Hello, my name is ${name} and I am ${age} years old`; +} const greeting = createLongGreeting("Daniel", 30); From 9c974d72ab206246083a8e215170295590eb5bf9 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 19:07:33 +0000 Subject: [PATCH 11/23] Completed Ex l Functions-nested Completed Exercise L Functions-nested --- exercises/L-functions-nested/exercise.js | 17 ++++++++++++----- exercises/L-functions-nested/exercise2.js | 13 +++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 exercises/L-functions-nested/exercise2.js diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..0693d91a7 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,12 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +//- In `exercise.js` write a program that displays the percentage of students and mentors in the group +//- The percentage should be rounded to the nearest whole number (use a search engine to find out how to this with JavaScript) +//- You should have one function that calculates the percentage, and one function that creates a message + +function percentageOfStudentsAndMentors(numberOfStudents, numberOfMentors){ + let total = numberOfStudents + numberOfMentors; + let percentageStudents = Math.round((numberOfStudents / total) * 100); + let percentageMentors = Math.round((numberOfMentors / total) * 100); + return `Percentage mentors: ${percentageStudents}%\nPercentage students: ${percentageMentors}%`; +} + +console.log(percentageOfStudentsAndMentors(15, 8)); \ No newline at end of file diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..029fd87ea --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,13 @@ +//- In `exercise2.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one. +//- Your program should include a function that spells their name in uppercase, and a function that creates a shouty greeting. +//- Log each greeting to the console. + +function shoutyGreeting(mentorName){ + return `hello ${mentorName}`.toUpperCase(); +} + +let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; + +for (i = 0; i < 5; i++){ + console.log(shoutyGreeting(mentors[i])); +} From fe69129bbc0b08fe22b73a5a40fada5b5a38c6af Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:01:39 +0000 Subject: [PATCH 12/23] Completed Mandatory Ex 1 Syntax Completed Mandatory Exercises 1 Syntax errors --- mandatory/1-syntax-errors.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..6f498d235 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; } /* From 96de5034f3bf5ee074c9871bede683606fc27f22 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:02:14 +0000 Subject: [PATCH 13/23] Completed Mandatory Ex 2 Logic Completed Mandatory Ex 2 Logic error --- 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 3c578ad87..1f1245b39 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 getWordLength(word) { - return "word".length(); + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + return a * b * c; } /* From 8c4d5c39a80c5270cdefb7f6115cceb6984052d0 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:02:38 +0000 Subject: [PATCH 14/23] Completed Mandatory Ex 3 Function Completed Mandatory Ex 3 Function Output --- mandatory/3-function-output.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..dca103231 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,21 @@ // Add comments to explain what this function does. You're meant to use Google! function getNumber() { - return Math.random() * 10; + return Math.random() * 10; // returns the number entered multiplied by a random integer from 0 to 9 } // Add comments to explain what this function does. You're meant to use Google! function s(w1, w2) { - return w1.concat(w2); + return w1.concat(w2); // concatenates w1 and w2 without white space } function concatenate(firstWord, secondWord, thirdWord) { // Write the body of this function to concatenate three words together. + return `${firstWord} ${secondWord} ${thirdWord}`; // Look at the test case below to understand what this function is expected to return. } +console.log(concatenate("Code","Your","Future")); + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== From 31f3c9a5d8c19530a2402eb9b55d920772ab6a1b Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:02:52 +0000 Subject: [PATCH 15/23] Completed Mandatory Ex 4 Tax Completed Mandatory Ex 4 Tax --- mandatory/4-tax.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..6186aaaf7 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,10 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(productPrice) { + return productPrice * 1.2; +} +console.log(calculateSalesTax(15)); /* CURRENCY FORMATTING @@ -17,8 +20,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} - +function addTaxAndFormatCurrency(productPrice) { + let totalCost = productPrice * 1.2; + return `£${totalCost.toFixed(2)}`; // fixes to 2 decimal places +} +console.log(addTaxAndFormatCurrency(15)); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== From a9156eea1deb544de2009ab2ca701a4030554e71 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:22:31 +0000 Subject: [PATCH 16/23] Completed Extra 1 Currency Completed Extra 1 Currency Conversions --- extra/1-currency-conversion.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..d7a93dca3 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,8 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} - +function convertToUSD(priceInPounds) { + return priceInPounds * 1.4; +} +console.log(convertToUSD(32)); /* CURRENCY FORMATTING =================== @@ -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(priceInPounds) { + return (priceInPounds * 0.99) * 5.7; +} +console.log(convertToBRL(30)); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 64583d6e12344d6eade1dcb864a5696a72129324 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 20:55:35 +0000 Subject: [PATCH 17/23] Completed Extra 2 piping Completed Extra 2 piping --- extra/2-piping.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index 067dd0f5b..d14121f44 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,31 @@ the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return num1 + num2; } +console.log(add(1, 3)); -function multiply() { - +function multiply(num1, num2) { + return num1 * num2; } +console.log(multiply(2, 3)); -function format() { - +function format(number) { + return `£${number}`; } +console.log(format(16)); -const startingValue = 2 +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = add(startingValue, 10); badCode = multiply(badCode, 2); console.log(badCode = format(badCode)); // code all on one line is difficult to read /* BETTER PRACTICE */ - -let goodCode = +// Comments can be added to explain each line and it is easier to read +let goodCode = add(startingValue, 10); // function call to add 10 to startingValue and store result in goodCode +goodCode = multiply(goodCode, 2); // function call to multiply value of goodCode by 2 +console.log(goodCode = format(goodCode)); // function call to format value of goodCode /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 58ef5635208e42db41599ff920a26d1b1aee57dc Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sun, 17 Jan 2021 22:28:29 +0000 Subject: [PATCH 18/23] Completed Extra Magic 8 Ball Completed Extra Magic 8 Ball --- extra/3-magic-8-ball.js | 46 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index e32182cfe..c3dfa0384 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,10 +45,35 @@ // This should log "The ball has shaken!" // and return the answer. + +function getRandomNumber(min, max) { + return Math.round(Math.random() * (max - min) + min); +} + function shakeBall() { - //Write your code in here + console.log("The ball has shaken!"); + let answer = ""; + randomNumber = getRandomNumber(1, 4) + if (randomNumber == 1) { + answer = `It is decidedly so.`; + return(answer); + } + else if (randomNumber == 2) { + answer = `As I see it, yes.`; + return(answer); + } + else if (randomNumber == 3) { + answer = `Reply hazy, try again.`; + return(answer); + } + else if (randomNumber == 4) { + answer = `My reply is no.`; + return(answer); + } } +console.log(shakeBall()); + /* This function should say whether the answer it is given is - very positive @@ -60,8 +85,27 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + let rating = ""; + if (answer == `It is decidedly so.`) { + console.log(rating = "very positive"); + return rating; + } + else if (answer == `As I see it, yes.`) { + console.log(rating = "positive"); + return rating; + } + else if (answer == `Reply hazy, try again.`) { + console.log(rating = "negative"); + return rating; + } + else if (answer == `My reply is no.`) { + console.log(rating = "very negative"); + return rating; + } } +checkAnswer(); +console.log(); /* ================================== ======= TESTS - DO NOT MODIFY ===== From 4a1825a62a1e3e2cc3174a6b84643a92fcefd2c8 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Fri, 22 Jan 2021 13:18:14 +0000 Subject: [PATCH 19/23] Updated Magic 8 Ball Removed console.log and put rating assignment with return --- extra/3-magic-8-ball.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index c3dfa0384..4e3ef5931 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -87,20 +87,16 @@ function checkAnswer(answer) { //Write your code in here let rating = ""; if (answer == `It is decidedly so.`) { - console.log(rating = "very positive"); - return rating; + return rating = "very positive"; } else if (answer == `As I see it, yes.`) { - console.log(rating = "positive"); - return rating; + return rating = "positive"; } else if (answer == `Reply hazy, try again.`) { - console.log(rating = "negative"); - return rating; + return rating = "negative"; } else if (answer == `My reply is no.`) { - console.log(rating = "very negative"); - return rating; + return rating = "very negative"; } } From 63646c537d262801dd3739e77ba24350001c1a40 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Fri, 22 Jan 2021 14:35:20 +0000 Subject: [PATCH 20/23] Improved Magic 8 Ball Solution Used object to set up key : value pairs for possible answers then a random selector to pick from them. Used these values to check answer rating with .includes() --- extra/3-magic-8-ball.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 4e3ef5931..01bd831f8 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,28 +46,37 @@ // This should log "The ball has shaken!" // and return the answer. +let possibleAnswers = { + veryPositive: ["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."], + veryNegative: ["Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."] +} + function getRandomNumber(min, max) { return Math.round(Math.random() * (max - min) + min); } +let answerSelection = getRandomNumber(1 , 5); + function shakeBall() { console.log("The ball has shaken!"); let answer = ""; - randomNumber = getRandomNumber(1, 4) + randomNumber = getRandomNumber(1, 4); if (randomNumber == 1) { - answer = `It is decidedly so.`; + answer = possibleAnswers.veryPositive[answerSelection]; return(answer); } else if (randomNumber == 2) { - answer = `As I see it, yes.`; + answer = possibleAnswers.positive[answerSelection]; return(answer); } else if (randomNumber == 3) { - answer = `Reply hazy, try again.`; + answer = possibleAnswers.negative[answerSelection]; return(answer); } else if (randomNumber == 4) { - answer = `My reply is no.`; + answer = possibleAnswers.veryNegative[answerSelection]; return(answer); } } @@ -83,19 +92,20 @@ console.log(shakeBall()); This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { + +function checkAnswer(answer, answerSelection) { //Write your code in here let rating = ""; - if (answer == `It is decidedly so.`) { + if (possibleAnswers.veryPositive.includes(answer, [answerSelection])) { return rating = "very positive"; } - else if (answer == `As I see it, yes.`) { + else if (possibleAnswers.positive.includes(answer, [answerSelection])) { return rating = "positive"; } - else if (answer == `Reply hazy, try again.`) { + else if (possibleAnswers.negative.includes(answer, [answerSelection])) { return rating = "negative"; } - else if (answer == `My reply is no.`) { + else if (possibleAnswers.veryNegative.includes(answer, [answerSelection])) { return rating = "very negative"; } } From dc6d6224262cd76b59e3eff400bb6ded9b0a5713 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Fri, 22 Jan 2021 16:21:39 +0000 Subject: [PATCH 21/23] Added comments to Magic 8 Ball Added comments to code for Magic 8 Ball --- extra/3-magic-8-ball.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 01bd831f8..80cd59483 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,6 +46,7 @@ // This should log "The ball has shaken!" // and return the answer. +// Object "possibleAnswers" declared for the 4 categories to set up key:value pairs let possibleAnswers = { veryPositive: ["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."], @@ -53,10 +54,13 @@ let possibleAnswers = { veryNegative: ["Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."] } +// Random number function function getRandomNumber(min, max) { + // Produces a random decimal number between 0-1 then multiplies it by the range then adds the min value. Rounds to nearest number. return Math.round(Math.random() * (max - min) + min); } +// Variable used in shakeBall() function to select an answer from each category and the checkAnswer() function as a parameter to pass in a random number between 1 and 5 let answerSelection = getRandomNumber(1 , 5); function shakeBall() { @@ -64,18 +68,22 @@ function shakeBall() { let answer = ""; randomNumber = getRandomNumber(1, 4); if (randomNumber == 1) { + // if the random number selected is 1, then variable "answer" is assigned the value of a random selection (1-5) from the "veryPositive" key values in the possibleAnswers object answer = possibleAnswers.veryPositive[answerSelection]; return(answer); } else if (randomNumber == 2) { + // if the random number selected is 2, then variable "answer" is assigned the value of a random selection (1-5) from the "positive" key values in the possibleAnswers object answer = possibleAnswers.positive[answerSelection]; return(answer); } else if (randomNumber == 3) { + // if the random number selected is 3, then variable "answer" is assigned the value of a random selection (1-5) from the "negative" key values in the possibleAnswers object answer = possibleAnswers.negative[answerSelection]; return(answer); } else if (randomNumber == 4) { + // if the random number selected is 4, then variable "answer" is assigned the value of a random selection (1-5) from the "veryNegative" key values in the possibleAnswers object answer = possibleAnswers.veryNegative[answerSelection]; return(answer); } @@ -96,15 +104,19 @@ console.log(shakeBall()); function checkAnswer(answer, answerSelection) { //Write your code in here let rating = ""; + // if the "veryPositive" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value very positive if (possibleAnswers.veryPositive.includes(answer, [answerSelection])) { return rating = "very positive"; } + // if the "positive" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value positive else if (possibleAnswers.positive.includes(answer, [answerSelection])) { return rating = "positive"; } + // if the "negative" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value negative else if (possibleAnswers.negative.includes(answer, [answerSelection])) { return rating = "negative"; } + // if the "VeryNegative" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value very negative else if (possibleAnswers.veryNegative.includes(answer, [answerSelection])) { return rating = "very negative"; } From 545d6be44c80f2ae2a3b660c0213843f2747e5a6 Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sat, 23 Jan 2021 19:44:40 +0000 Subject: [PATCH 22/23] Corrected an error in answerSelection Changed arguments passed to getRandomNumber function from (1 , 5) to (0 , 4) as these are the index numbers for the arrays. --- extra/3-magic-8-ball.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 80cd59483..75d995216 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -60,8 +60,8 @@ function getRandomNumber(min, max) { return Math.round(Math.random() * (max - min) + min); } -// Variable used in shakeBall() function to select an answer from each category and the checkAnswer() function as a parameter to pass in a random number between 1 and 5 -let answerSelection = getRandomNumber(1 , 5); +// Variable used in shakeBall() function to select an answer from each category and the checkAnswer() function as a parameter to pass in a random number between 0 and 4 +let answerSelection = getRandomNumber(0 , 4); function shakeBall() { console.log("The ball has shaken!"); From 094e4ae4f6396e69f14ece145d775ab298907e9e Mon Sep 17 00:00:00 2001 From: Kara Howard Date: Sat, 23 Jan 2021 21:05:08 +0000 Subject: [PATCH 23/23] Updated comments for Magic 8 Ball Updated comments to reflect changes made previously --- extra/3-magic-8-ball.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 75d995216..e046b7756 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -60,30 +60,31 @@ function getRandomNumber(min, max) { return Math.round(Math.random() * (max - min) + min); } -// Variable used in shakeBall() function to select an answer from each category and the checkAnswer() function as a parameter to pass in a random number between 0 and 4 +// Variable used in shakeBall() function and checkAnswer function to select an answer from the array of the chosen category (passes in a random number between 0 and 4) let answerSelection = getRandomNumber(0 , 4); function shakeBall() { console.log("The ball has shaken!"); + let randomNumber = getRandomNumber(1, 4); let answer = ""; - randomNumber = getRandomNumber(1, 4); + if (randomNumber == 1) { - // if the random number selected is 1, then variable "answer" is assigned the value of a random selection (1-5) from the "veryPositive" key values in the possibleAnswers object + // if the random number selected is 1, then variable "answer" is assigned the value of a random selection (0-4) from the "veryPositive" values array in the possibleAnswers object answer = possibleAnswers.veryPositive[answerSelection]; return(answer); } else if (randomNumber == 2) { - // if the random number selected is 2, then variable "answer" is assigned the value of a random selection (1-5) from the "positive" key values in the possibleAnswers object + // if the random number selected is 2, then variable "answer" is assigned the value of a random selection (0-4) from the "positive" values array in the possibleAnswers object answer = possibleAnswers.positive[answerSelection]; return(answer); } else if (randomNumber == 3) { - // if the random number selected is 3, then variable "answer" is assigned the value of a random selection (1-5) from the "negative" key values in the possibleAnswers object + // if the random number selected is 3, then variable "answer" is assigned the value of a random selection (0-4) from the "negative" values array in the possibleAnswers object answer = possibleAnswers.negative[answerSelection]; return(answer); } else if (randomNumber == 4) { - // if the random number selected is 4, then variable "answer" is assigned the value of a random selection (1-5) from the "veryNegative" key values in the possibleAnswers object + // if the random number selected is 4, then variable "answer" is assigned the value of a random selection (0-4) from the "veryNegative" values array in the possibleAnswers object answer = possibleAnswers.veryNegative[answerSelection]; return(answer); } @@ -103,22 +104,21 @@ console.log(shakeBall()); function checkAnswer(answer, answerSelection) { //Write your code in here - let rating = ""; - // if the "veryPositive" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value very positive + // if the "veryPositive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very positive" if (possibleAnswers.veryPositive.includes(answer, [answerSelection])) { - return rating = "very positive"; + return "very positive"; } - // if the "positive" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value positive + // if the "positive" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "positive" else if (possibleAnswers.positive.includes(answer, [answerSelection])) { - return rating = "positive"; + return "positive"; } - // if the "negative" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value negative + // if the "negative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "negative" else if (possibleAnswers.negative.includes(answer, [answerSelection])) { - return rating = "negative"; + return "negative"; } - // if the "VeryNegative" key values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "rating" value very negative + // if the "veryNegative" values in the possibleAnswers object include the string assigned to "answer" at the index specified by "answerSelection", then return "very negative" else if (possibleAnswers.veryNegative.includes(answer, [answerSelection])) { - return rating = "very negative"; + return "very negative"; } }