From 61bc11bad56a0a8ce065c262604905ed67577e03 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Tue, 22 Nov 2022 21:07:57 +0000 Subject: [PATCH 01/12] first commit by terminal --- exercises/B-hello-world/README.md | 4 ++++ exercises/B-hello-world/exercise.js | 3 ++- exercises/I-floats/exercise.js | 6 ++++++ mandatory/1-syntax-errors.js | 15 +++++++-------- mandatory/2-logic-error.js | 10 ++++------ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 27282c4af..6ae0b6fa5 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -14,5 +14,9 @@ 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? + I received a syntax error. + - What happens when you console.log() just a number without quotes? + It works without error. diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..54f6a0cfe 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ -console.log("Hello world"); +console.log("Hello world, I just started learning JavaScript!"); +console.log(54); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..a2d996f8f 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,8 @@ var numberOfStudents = 15; var numberOfMentors = 8; +var total = numberOfMentors + numberOfStudents; +var percentageOfStudents = (numberOfStudents * 100) / total; +var percentageOfMentors = (numberOfMentors * 100) / total; + +console.log(`Percentage students: ${Math.round(percentageOfStudents)}%`); +console.log(`Percentage mentors: ${Math.round(percentageOfMentors)}%`); \ No newline at end of file diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..838863af6 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,16 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +function addNumbers(a, b, c) { return a + b + c; } -function introduceMe(name, age) - return "Hello, my name is " + 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; - - return "The total is total"; + total = a + b; + return `The total is ${total}`; } /* @@ -31,8 +31,7 @@ test("addNumbers adds numbers correctly", () => { test("introduceMe function returns the correct string", () => { expect(introduceMe("Sonjide", 27)).toEqual( - "Hello, my name is Sonjide and I am 27 years old" - ); + "Hello, my name is Sonjide and I am 27 years old"); }); test("getTotal returns a string describing the total", () => { diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..305672698 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; } /* @@ -30,8 +29,7 @@ test("trimWord trims leading and trailing whitespace", () => { test("trimWord doesn't remove whitespace in the middle of the string", () => { expect(trimWord(" CodeYourFuture teaches coding ")).toEqual( - "CodeYourFuture teaches coding" - ); + "CodeYourFuture teaches coding"); }); test("getStringLength returns the length of a word", () => { From 8bb08bcdce78c313285ea12ab221d74e15bd8397 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:46:19 +0000 Subject: [PATCH 02/12] C to L for exercise & 2 mandatory done --- exercises/B-hello-world/README.md | 2 +- exercises/C-variables/exercise.js | 4 +++- exercises/D-strings/exercise.js | 3 ++- exercises/E-strings-concatenation/exercise.js | 4 +++- exercises/F-strings-methods/exercise.js | 5 +++-- exercises/F-strings-methods/exercise2.js | 4 +++- exercises/G-numbers/exercise.js | 6 ++++++ mandatory/2-logic-error.js | 2 +- package.json | 12 +++++++++--- 9 files changed, 31 insertions(+), 11 deletions(-) diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 6ae0b6fa5..9d3d146e8 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -19,4 +19,4 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". I received a syntax error. - What happens when you console.log() just a number without quotes? - It works without error. + It prints the number without any error. diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..7beea3261 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var greeting = "Hello world"; +console.log(greeting); +console.log(greeting); console.log(greeting); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..caafd0197 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - +var message = "This is a string"; console.log(message); +console.log(typeof message); \ No newline at end of file diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..91483d6f2 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +var greeting = "Hello, my name is "; +var myName = "Daniel"; +message = greeting + myName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..f513fbd24 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - -console.log(message); +var myName = "Daniel"; +var message = myName.length; +console.log(`My name is ${myName} and my name is ${message} characters long`); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..74bc32296 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,5 @@ const name = " Daniel "; - +message = name.trim(" "); console.log(message); +console.log(`My name is ${message} and my name is ${message.length} characters long`); + diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..29bd5a76f 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,7 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudents = 15; +var numberOfMentors = 8; +var total = numberOfMentors + numberOfStudents; +console.log(`Number of students: ${numberOfStudents}`); +console.log(`Number of mentors: ${numberOfMentors}`); +console.log(`Total number of students and mentors: ${total}`); \ No newline at end of file diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 305672698..2baf991ff 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -5,7 +5,7 @@ function trimWord(word) { } function getStringLength(word) { - return word.length(); + return word.length; } function multiply(a, b, c) { diff --git a/package.json b/package.json index 93e0861e3..9e7f2ffeb 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,21 @@ "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/issues" }, "jest": { - "setupFilesAfterEnv": ["jest-extended"], + "setupFilesAfterEnv": [ + "jest-extended" + ], "projects": [ { "displayName": "mandatory", - "testMatch": ["/mandatory/*.js"] + "testMatch": [ + "/mandatory/*.js" + ] }, { "displayName": "extra", - "testMatch": ["/extra/*.js"] + "testMatch": [ + "/extra/*.js" + ] } ] }, From 33841b4261d1f90a291c6d44d57a4deddccaa97f Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Wed, 23 Nov 2022 22:21:00 +0000 Subject: [PATCH 03/12] J & K & L exercise done --- exercises/L-functions-nested/exercise.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..989ea0036 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,18 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function shutyGreeting(mentor) { + var name = upperCase(mentor); + return `Hello ${name}`; +} + +function upperCase(mentor) { + return mentor.toUpperCase(); +} + +console.log(shutyGreeting(mentor1)); +console.log(shutyGreeting(mentor2)); +console.log(shutyGreeting(mentor3)); +console.log(shutyGreeting(mentor4)); +console.log(shutyGreeting(mentor5)); \ No newline at end of file From 588d1fe6b296a436de70ad0d2c5e78c5f2a30deb Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 05:12:55 +0000 Subject: [PATCH 04/12] currency from extra done --- 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 75b3c6aab..306ad9224 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,11 @@ function convertToUSD() {} They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. */ -function convertToBRL() {} +function convertToBRL(price) { + let num = price * 5.643; //5.7 * 0.99 = 5.643 + num = num.toFixed(2); + return parseFloat(num); +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 6b01c2d346cb0b4b73ee708a6101195e89da1a0b Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 05:26:27 +0000 Subject: [PATCH 05/12] K & L for exercise finished before but it didn't committed --- README.md | 4 ++-- exercises/J-functions/exercise.js | 7 +++++++ exercises/J-functions/exercise2.js | 4 +++- 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 | 4 +++- 8 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dff05d7cd..aafe8c35a 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ Like learning a musical instrument, programming requires daily practise. -The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson. +The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson. The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson. ## Running the code/tests -The files for the mandatory/extra exercises are intended to be run as jest tests. +The files for the mandatory/extra exercises are intended to be run as jest tests. - Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies). - To run the tests for all mandatory/extra exercises, run `npm test` diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..8448a0eac 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,14 @@ function halve(number) { // complete the function here + return number / 2; } var result = halve(12); +console.log(result); + +var result = halve(20); +console.log(result); +var result = halve(36); console.log(result); + diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..86e647f18 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,9 @@ function triple(number) { // complete function here + return number * 3; } var result = triple(12); - console.log(result); + + diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..f842a131f 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(x, y) { // Calculate the result of the function and return it + return x * y; } // 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..ea3284118 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(x, y) { + return x / y; +} var result = divide(3, 4); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..86a978b20 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(str) { + return `Hello, my name is ${str}`; +} var greeting = createGreeting("Daniel"); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..29d05c31c 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function add(x, y) { + return x + y; +} // Call the function and assign to a variable `sum` - +var sum = add(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..096ef9b7c 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +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); console.log(greeting); From 69c27b013a8137e26402e2986b734843d36e5cb4 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 05:45:50 +0000 Subject: [PATCH 06/12] piping for extera finished --- extra/2-piping.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..a6baa2573 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,30 +16,31 @@ the final result to the variable goodCode */ -function add() { - +function add(firstNum, secondNum) { + return firstNum + secondNum; } -function multiply() { - +function multiply(firstNum, secondNum) { + return firstNum * secondNum; } -function format() { - +function format(num) { + return `£${num}`; } 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 result = add(startingValue, 10); +result = multiply(result, 2); +let goodCode = format(result); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. - + To run the tests for just this one file, type `npm test -- --testPathPattern 2-piping` into your terminal (Reminder: You must have run `npm install` one time before this will work!) */ From 0863da5a4cc353d40a3b772d0c563933f1fd082c Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 06:44:29 +0000 Subject: [PATCH 07/12] function from madatory done --- mandatory/3-function-output.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..396d67e58 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -2,13 +2,21 @@ function getRandomNumber() { return Math.random() * 10; } +// choose a random number which is between 0 to 1, so by changing to times 10, we receive a random number between 1 to 10 + // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { return word1.concat(word2); } +//it concat two words, it means we receive 2 parameter beside each other. + function concatenate(firstWord, secondWord, thirdWord) { + let result = combine2Words(firstWord, " "); + result = combine2Words(result, secondWord); + result = combine2Words(result, " "); + return combine2Words(result, 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. } From 6a54bcdae223958ac8efbc68fa5ae0e3bdf2c945 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 07:45:21 +0000 Subject: [PATCH 08/12] tax for mandatory done --- mandatory/4-tax.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..5d826574d 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,8 +5,9 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} - +function calculateSalesTax(price) { + return price * 1.2; // price + (price * 20 / 100 )=> price + price / 5 => price * 6 /5 +} /* CURRENCY FORMATTING =================== @@ -17,7 +18,9 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(tax) { + return `£${calculateSalesTax(tax).toFixed(2)}`; +} /* =================================================== From 4f62691be9f554b2350f59cab4832442e10b42a6 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Thu, 24 Nov 2022 23:15:16 +0000 Subject: [PATCH 09/12] 8 ball for extra done. It works for me without test(just calling function) --- extra/3-magic-8-ball.js | 50 +++++++++++++++++++++++++++++++++- mandatory/3-function-output.js | 10 +++---- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..e5a552662 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,8 +45,41 @@ // This should log "The ball has shaken!" // and return the answer. + + +const answers = [["It is certain", + "It is decidedly so", + "Without a doubt", + "Yes - definitely", + "You may rely on it"], + +["As I see it, yes", + "Most likely", + "Outlook good", + "Yes", + "Signs point to yes"], + +["Reply hazy, try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again"], + +["Don't count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful"]]; + + function shakeBall() { //Write your code in here + + let firstItem = Math.floor(Math.random() * 4); + let secondItem = Math.floor(Math.random() * 5); + + return checkAnswer(answers[firstItem][secondItem]); + } /* @@ -58,11 +91,26 @@ 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 + + for (let i = 0; i < 4; i++) + for (let j = 0; j < 5; j++) + if (answers[i][j] === answer) { + if (i === 0) + return "very positive"; + else if (i === 1) + return "positive"; + else if (i === 2) + return "negative"; + else + return "very negative"; + } } -/* +/* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 396d67e58..5e541f9d7 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -9,14 +9,14 @@ function getRandomNumber() { function combine2Words(word1, word2) { return word1.concat(word2); } -//it concat two words, it means we receive 2 parameter beside each other. +//it concat two words, it means we will these receive 2 parameters beside each other. function concatenate(firstWord, secondWord, thirdWord) { - let result = combine2Words(firstWord, " "); - result = combine2Words(result, secondWord); - result = combine2Words(result, " "); - return combine2Words(result, thirdWord); + let result = firstWord.concat(" "); + result = result.concat(secondWord); + result = result.concat(" "); + return result.concat(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. } From 3ab14ac15c57578843885ce013d87f5e920b3acd Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Tue, 29 Nov 2022 19:10:06 +0000 Subject: [PATCH 10/12] exercise K updated --- exercises/K-functions-parameters/exercise2.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index ea3284118..9825a4e1e 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,6 +1,8 @@ // Declare your function first function divide(x, y) { - return x / y; + if (y !== 0) + return x / y; + return "It is not possible!" } var result = divide(3, 4); From 6643f6f2b7c569a1ce46bb8b8b902a2a06345314 Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Tue, 29 Nov 2022 19:39:42 +0000 Subject: [PATCH 11/12] extra 3-magic-8-ball updated --- 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 e5a552662..226dff38d 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -75,8 +75,8 @@ const answers = [["It is certain", function shakeBall() { //Write your code in here - let firstItem = Math.floor(Math.random() * 4); - let secondItem = Math.floor(Math.random() * 5); + let firstItem = Math.floor(Math.random() * answers.length); + let secondItem = Math.floor(Math.random() * answers[0].length); return checkAnswer(answers[firstItem][secondItem]); From c453afbbe5e2a5a5d6684c20dfc6ed0e8118900b Mon Sep 17 00:00:00 2001 From: Farzaneh Haghani <68525882+farzaneh-haghani@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:27:55 +0000 Subject: [PATCH 12/12] Error solved --- extra/3-magic-8-ball.js | 52 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 226dff38d..3a5e1e679 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,30 +46,31 @@ // This should log "The ball has shaken!" // and return the answer. +const { toBeOneOf } = require("jest-extended"); -const answers = [["It is certain", - "It is decidedly so", - "Without a doubt", - "Yes - definitely", - "You may rely on it"], +const answers = [["It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it."], -["As I see it, yes", - "Most likely", - "Outlook good", - "Yes", - "Signs point to yes"], +["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"], +["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"]]; +["Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful."]]; function shakeBall() { @@ -77,9 +78,8 @@ function shakeBall() { let firstItem = Math.floor(Math.random() * answers.length); let secondItem = Math.floor(Math.random() * answers[0].length); - - return checkAnswer(answers[firstItem][secondItem]); - + console.log("The ball has shaken!"); + return answers[firstItem][secondItem]; } /* @@ -96,8 +96,8 @@ function shakeBall() { function checkAnswer(answer) { //Write your code in here - for (let i = 0; i < 4; i++) - for (let j = 0; j < 5; j++) + for (let i = 0; i < answers.length; i++) + for (let j = 0; j < answers[0].length; j++) if (answers[i][j] === answer) { if (i === 0) return "very positive"; @@ -116,7 +116,7 @@ function checkAnswer(answer) { There are some Tests in this file that will help you work out if your code is working. -To run the tests for just this one file, type `npm test -- --testPathPattern 3-magic-8-ball` into your terminal +To run the tests for just this one file, type `npm test-- --testPathPattern 3 - magic - 8 - ball` into your terminal (Reminder: You must have run `npm install` one time before this will work!) ================================== */