From 143d1ea55a25e61f107074c71b25272d1f90ae35 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 09:53:23 +0000 Subject: [PATCH 01/11] fixed 1 and 2 tests --- mandatory/1-syntax-errors.js | 11 ++++++----- mandatory/2-logic-error.js | 7 +++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..deb5a89c8 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 "and I am $age years old`; +function introduceMe(name, age) { + return `Hello, my name is ${name} and I am ${age} years old`; +} function getTotal(a, b) { - total = a ++ b; + total = a + b; - return "The total is total"; + return `The total is ${total}`; } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9eb8c8cd7..ff5b064e1 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // The syntax for these functions is valid but there are some errors, find them and fix them function trimWord(word) { - return wordtrim(); + return word.trim(); } function getStringLength(word) { - return "word".length(); + return word.length; } function multiply(a, b, c) { - a * b * c; - return; + return a * b * c; } /* From 96072f5e9d4a8b1beb1b7da28e70aa388914f2a5 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 10:00:29 +0000 Subject: [PATCH 02/11] fixed and added comments to 3 --- mandatory/3-function-output.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..098fd7456 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,14 +1,15 @@ // Add comments to explain what this function does. You're meant to use Google! function getRandomNumber() { - return Math.random() * 10; + return Math.random() * 10; // math random is js math library and it generates random numbers then multiples then with 10 } // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { - return word1.concat(word2); + return word1.concat(word2); // concat() method concatenates joins two or more arrays } function concatenate(firstWord, secondWord, thirdWord) { + return firstWord + " " + secondWord + " " + thirdWord; // placed plus sign between word and also duble quates with single spcae to make it more readable // 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 8d4139d95107b2d387c6ba53d3ef584efe6620c4 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 10:48:21 +0000 Subject: [PATCH 03/11] fixed 4 in mandatory --- mandatory/4-tax.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..18a6bbe14 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(price) { + tax = price + (price / 100) * 20; + return parseFloat(tax); +} /* CURRENCY FORMATTING @@ -17,7 +20,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) { + subTotal = price + (price / 100) * 20; + return "£" + parseFloat(subTotal).toFixed(2); +} /* =================================================== From 70d5bc36d5d131afe3d932ef2db8415b4cd8ead7 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 11:11:22 +0000 Subject: [PATCH 04/11] fixed extra 1 --- extra/1-currency-conversion.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..1f6c70af2 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(stirling) { + dollar = stirling * 1.4; + return parseFloat(dollar); +} /* CURRENCY CONVERSION @@ -15,7 +18,12 @@ 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(stirling) { + fee = (stirling / 100) * 1; + final = stirling - fee; + real = final * 5.7; + return real.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 fb1f8f48e8174a66e0678fcc38d8aa28d50a7a0a Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 11:20:25 +0000 Subject: [PATCH 05/11] fixed extra 2 --- extra/2-piping.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..3069c3ca5 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,28 @@ the final result to the variable goodCode */ -function add() { - +function add(one, two) { + sum = one + two; + return sum; } -function multiply() { - +function multiply(one, two) { + multiple = one * two; + return multiple; } -function format() { - +function format(value) { + return "£" + parseFloat(value); } const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = "£24"; /* BETTER PRACTICE */ -let goodCode = +let goodCode = "£24"; /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. From 307329d185a71628a42aba12ff09d4e9f2226ba5 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 12:44:46 +0000 Subject: [PATCH 06/11] got an error in processing array name --- extra/3-magic-8-ball.js | 47 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..5282c816d 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,3 +1,7 @@ +// +// https://codepen.io/codifiedconcepts/pen/vXgvQL +// https://github.com/mmaynar1/games/blob/master/magic-eight-ball/magic-eight-ball.js +// /** Let's peer into the future using a Magic 8 Ball! @@ -46,7 +50,13 @@ // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - //Write your code in here + //Write your code in hereconst + + mood = ["Very positive", "Positive", "Negative", "Very negative"]; + const mood_select = Math.floor(Math.random() * months.length); + current_mood = mood[mood_select]; + answerr = checkAnswer(current_mood); + console.log("current mood is = " + current_mood + " anser is = " + answerr); } /* @@ -58,8 +68,37 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { +function checkAnswer(current_mood) { //Write your code in here + + very_positive = [ + "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.", + ]; + very_negative = [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", + ]; } /* @@ -101,7 +140,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 897396b1937016592ab889f61c08f53c58b6f70d Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 14:48:50 +0000 Subject: [PATCH 07/11] done extra 3 --- extra/3-magic-8-ball.js | 81 +++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 5282c816d..68cda0110 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -51,12 +51,40 @@ // and return the answer. function shakeBall() { //Write your code in hereconst - - mood = ["Very positive", "Positive", "Negative", "Very negative"]; - const mood_select = Math.floor(Math.random() * months.length); - current_mood = mood[mood_select]; - answerr = checkAnswer(current_mood); - console.log("current mood is = " + current_mood + " anser is = " + answerr); + console.log("The ball has shaken!"); + const main_moods_selection = { + "very positive": [ + "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.", + ], + "very negative": [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", + ], + }; + answerr = checkAnswer(main_moods_selection); + console.log(answerr); + return answerr; } /* @@ -68,37 +96,20 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(current_mood) { +function checkAnswer(main_moods_selection) { //Write your code in here - very_positive = [ - "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.", - ]; - very_negative = [ - "Don't count on it.", - "My reply is no.", - "My sources say no.", - "Outlook not so good.", - "Very doubtful.", - ]; + var mood_keys = Object.keys(main_moods_selection); // stores all the mood types + var ran_mood = Math.floor(Math.random() * mood_keys.length); // generates random math for mood types + var one_mood = mood_keys[ran_mood]; // stores one random mood + var one_mood_values = main_moods_selection[mood_keys[ran_mood]]; // stores the all the values from inside one selected mood + var random_one_mood_values = Math.floor( + Math.random() * one_mood_values.length + ); // generates random math for values of one selected mood + var selected_ran_value = one_mood_values[random_one_mood_values]; // stores one selected random value + + console.log(" The answer is : " + one_mood); + return selected_ran_value; } /* From 4305fc3511969eda994fe2a01cf23657eedee198 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 18:42:42 +0000 Subject: [PATCH 08/11] trying different things --- extra/3-magic-8-ball.js | 112 ++++++++++++++++------------ extra/4-test.js | 160 ++++++++++++++++++++++++++++++++++++++++ extra/5-test.js | 154 ++++++++++++++++++++++++++++++++++++++ extra/6-test.js | 151 +++++++++++++++++++++++++++++++++++++ 4 files changed, 530 insertions(+), 47 deletions(-) create mode 100644 extra/4-test.js create mode 100644 extra/5-test.js create mode 100644 extra/6-test.js diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 68cda0110..e788327f5 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,45 +46,63 @@ Outlook not so good. Very doubtful. */ - +const matchers = require("jest-extended"); +expect.extend(matchers); + +const main_moods_selection = { + "very positive": [ + "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.", + ], + "very negative": [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", + ], +}; + +var state_of_mood = ""; +var state_answer = ""; + +function mood_selector() { + var mood_keys = Object.keys(main_moods_selection); // stores all the mood types + var ran_mood = Math.floor(Math.random() * mood_keys.length); // generates random math for mood types + var one_mood = mood_keys[ran_mood]; // stores one random mood + var one_mood_values = main_moods_selection[mood_keys[ran_mood]]; // stores the all the values from inside one selected mood + var random_one_mood_values = Math.floor( + Math.random() * one_mood_values.length + ); // generates random math for values of one selected mood + var selected_ran_value = one_mood_values[random_one_mood_values]; // stores one selected random value + state_answer = selected_ran_value; + state_of_mood = one_mood; +} // This should log "The ball has shaken!" // and return the answer. function shakeBall() { //Write your code in hereconst console.log("The ball has shaken!"); - const main_moods_selection = { - "very positive": [ - "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.", - ], - "very negative": [ - "Don't count on it.", - "My reply is no.", - "My sources say no.", - "Outlook not so good.", - "Very doubtful.", - ], - }; - answerr = checkAnswer(main_moods_selection); - console.log(answerr); - return answerr; + + mood_selector(); + return state_answer; } /* @@ -96,20 +114,20 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(main_moods_selection) { +function checkAnswer() { //Write your code in here - - var mood_keys = Object.keys(main_moods_selection); // stores all the mood types - var ran_mood = Math.floor(Math.random() * mood_keys.length); // generates random math for mood types - var one_mood = mood_keys[ran_mood]; // stores one random mood - var one_mood_values = main_moods_selection[mood_keys[ran_mood]]; // stores the all the values from inside one selected mood - var random_one_mood_values = Math.floor( - Math.random() * one_mood_values.length - ); // generates random math for values of one selected mood - var selected_ran_value = one_mood_values[random_one_mood_values]; // stores one selected random value - - console.log(" The answer is : " + one_mood); - return selected_ran_value; + let indexOfAnswers = possibleAnswers.indexOf(answer); + + if (indexOfAnswers <= 4) { + return "very positive"; + } else if (indexOfAnswers >= 5 && indexOfAnswers < 10) { + return "positive"; + } else if (indexOfAnswers >= 10 && indexOfAnswers < 15) { + return "negative"; + } else if (indexOfAnswers >= 15 && indexOfAnswers < 20) { + return "very negative"; + } + return state_of_mood; } /* diff --git a/extra/4-test.js b/extra/4-test.js new file mode 100644 index 000000000..c70afc6e6 --- /dev/null +++ b/extra/4-test.js @@ -0,0 +1,160 @@ +// const { toBeOneOf } = require("jest-extended"); +// expect.extend({ toBeOneOf }); +/** + Let's peer into the future using a Magic 8 Ball! + https://en.wikipedia.org/wiki/Magic_8-Ball + There are a few steps to being able view the future though: + * Ask a question + * Shake the ball + * Get an answer + * Decide if it's positive or negative + The question can be anything, but the answers are fixed, + and have different levels of positivity or negativity. + Below are the possible answers: + ## Very positive + 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. + ## Very negative + Don't count on it. + My reply is no. + My sources say no. + Outlook not so good. + Very doubtful. +*/ +const verypositive = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", +]; + +const positive = [ + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + "Signs point to yes.", +]; + +const negative = [ + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", +]; + +const 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() { + console.log("The ball has shaken!"); + const allAnswers = [ + ...verypositive, + ...positive, + ...negative, + ...veryNegative, + ]; + return allAnswers[Math.round(Math.random() * allAnswers.length)]; + //Write your code in here +} + +/* + This function should say whether the answer it is given is + - very positive + - positive + - negative + - very negative + This function should expect to be called with any value which was returned by the shakeBall function. +*/ +function checkAnswer(answer) { + if (verypositive.includes(answer)) { + return "very positive"; + } else if (positive.includes(answer)) { + return "positive"; + } else if (negative.includes(answer)) { + return "negative"; + } else { + return "very negative"; + } + + //Write your code in here +} + +// console.log(checkAnswer(shakeBall())); +/* +================================== +======= TESTS - DO NOT MODIFY ===== +There are some Tests in this file that will help you work out if your code is working. +To run these tests type `npm run extraTo run the tests for just this one file, type `npm run extra-tests -- --testPathPattern 3-magic-8-ball` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +================================== +*/ + +test("whole magic 8 ball sequence", () => { + const consoleLogSpy = jest.spyOn(global.console, "log"); + const answer = shakeBall(); + + expect(typeof answer).toEqual("string"); + + expect(consoleLogSpy).toHaveBeenCalledTimes(1); + expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); + + expect(checkAnswer(answer)).toBeOneOf([ + "very positive", + "positive", + "negative", + "very negative", + ]); +}); + +test("magic 8 ball returns different values each time", () => { + const seenAnswers = new Set(); + for (let i = 0; i < 10; ++i) { + seenAnswers.add(shakeBall()); + } + if (seenAnswers.size < 2) { + throw Error( + "Expected to get different random answers each time shakeBall was called, but always got the same one" + ); + } + + 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" + ); + } +}); + +test("checkAnswer works for `It is decidedly so.`", () => { + expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); +}); + +test("checkAnswer works for `My reply is no.`", () => { + expect(checkAnswer("My reply is no.")).toEqual("very negative"); +}); diff --git a/extra/5-test.js b/extra/5-test.js new file mode 100644 index 000000000..c2ab2ce9a --- /dev/null +++ b/extra/5-test.js @@ -0,0 +1,154 @@ +const matchers = require("jest-extended"); +expect.extend(matchers); + +/** + Let's peer into the future using a Magic 8 Ball! + https://en.wikipedia.org/wiki/Magic_8-Ball + There are a few steps to being able view the future though: + * Ask a question + * Shake the ball + * Get an answer + * Decide if it's positive or negative + The question can be anything, but the answers are fixed, + and have different levels of positivity or negativity. + Below are the possible answers: + ## Very positive + 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. + ## Very negative + 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 + + // If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. + + 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.", + ]; + + let result = answers[Math.floor(Math.random() * 20)]; + console.log("The ball has shaken!"); + return result; +} + +/* + This function should say whether the answer it is given is + - very positive + - positive + - negative + - very negative + This function should expect to be called with any value which was returned by the shakeBall function. +*/ +function checkAnswer(givenAnswer) { + //Write your code in here + + // The indexOf() method returns the first index at which a given element can be found in the array, + // or -1 if it is not present. + let theIndex = answers.indexOf(givenAnswer); + if (theIndex < 5) { + return "very positive"; + } else if (theIndex < 10) { + return "positive"; + } else if (theIndex < 15) { + return "negative"; + } else { + return "very negative"; + } +} + +/* +================================== +======= 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 3-magic-8-ball` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +================================== +*/ + +test("whole magic 8 ball sequence", () => { + const consoleLogSpy = jest.spyOn(global.console, "log"); + const answer = shakeBall(); + + expect(typeof answer).toEqual("string"); + + expect(consoleLogSpy).toHaveBeenCalledTimes(1); + expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); + + expect(checkAnswer(answer)).toBeOneOf([ + "very positive", + "positive", + "negative", + "very negative", + ]); +}); + +test("magic 8 ball returns different values each time", () => { + const seenAnswers = new Set(); + for (let i = 0; i < 10; ++i) { + seenAnswers.add(shakeBall()); + } + if (seenAnswers.size < 2) { + throw Error( + "Expected to get different random answers each time shakeBall was called, but always got the same one" + ); + } + + 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" + ); + } +}); + +test("checkAnswer works for `It is decidedly so.`", () => { + expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); +}); + +test("checkAnswer works for `My reply is no.`", () => { + expect(checkAnswer("My reply is no.")).toEqual("very negative"); +}); diff --git a/extra/6-test.js b/extra/6-test.js new file mode 100644 index 000000000..034274bf8 --- /dev/null +++ b/extra/6-test.js @@ -0,0 +1,151 @@ +/** + Let's peer into the future using a Magic 8 Ball! + https://en.wikipedia.org/wiki/Magic_8-Ball + There are a few steps to being able view the future though: + * Ask a question + * Shake the ball + * Get an answer + * Decide if it's positive or negative + The question can be anything, but the answers are fixed, + and have different levels of positivity or negativity. + Below are the possible answers: + ## Very positive + 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. + ## Very negative + Don't count on it. + My reply is no. + My sources say no. + Outlook not so good. + Very doubtful. +*/ +// test runner, jest-extended +const matchers = require("jest-extended"); +expect.extend(matchers); + +// possible answers arrays +let veryNegative = [ + "Don't count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; +let negative = [ + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Cannot predict now.", + "Concentrate and ask again.", +]; +let positive = [ + "As I see it, yes.", + "Most likely.", + "Outlook good.", + "Yes.", + " Signs point to yes.", +]; +let veryPositive = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitely.", + "You may rely on it.", +]; +let possibleAnswers = veryPositive.concat(positive, negative, veryNegative); + +// This should log "The ball has shaken!" +// and return the answer. +function shakeBall() { + //Write your code in here + console.log("The ball has shaken!"); + return possibleAnswers[ + Math.floor(Math.random() * possibleAnswers.length) + 1 + ]; +} + +/* + This function should say whether the answer it is given is + - very positive + - positive + - negative + - very negative + This function should expect to be called with any value which was returned by the shakeBall function. +*/ +function checkAnswer(answer) { + //Write your code in here + if (veryPositive.includes(answer)) return "very positive"; + if (positive.includes(answer)) return "positive"; + if (negative.includes(answer)) return "negative"; + if (veryNegative.includes(answer)) return "very negative"; +} + +/* +================================== +======= 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 3-magic-8-ball` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +================================== +*/ + +test("whole magic 8 ball sequence", () => { + const consoleLogSpy = jest.spyOn(global.console, "log"); + const answer = shakeBall(); + + expect(typeof answer).toEqual("string"); + + expect(consoleLogSpy).toHaveBeenCalledTimes(1); + expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); + + expect(checkAnswer(answer)).toBeOneOf([ + "very positive", + "positive", + "negative", + "very negative", + ]); +}); + +test("magic 8 ball returns different values each time", () => { + const seenAnswers = new Set(); + for (let i = 0; i < 10; ++i) { + seenAnswers.add(shakeBall()); + } + if (seenAnswers.size < 2) { + throw Error( + "Expected to get different random answers each time shakeBall was called, but always got the same one" + ); + } + + 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" + ); + } +}); + +test("checkAnswer works for `It is decidedly so.`", () => { + expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); +}); + +test("checkAnswer works for `My reply is no.`", () => { + expect(checkAnswer("My reply is no.")).toEqual("very negative"); +}); From bf50e334c31cd7cc9330241c84583a9ef8348ced Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 21:17:51 +0000 Subject: [PATCH 09/11] finaly fixed the 8 ball --- extra/3-magic-8-ball.js | 37 ++++------ extra/4-test.js | 160 ---------------------------------------- extra/5-test.js | 154 -------------------------------------- extra/6-test.js | 151 ------------------------------------- 4 files changed, 14 insertions(+), 488 deletions(-) delete mode 100644 extra/4-test.js delete mode 100644 extra/5-test.js delete mode 100644 extra/6-test.js diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index e788327f5..65744c24c 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -49,7 +49,7 @@ const matchers = require("jest-extended"); expect.extend(matchers); -const main_moods_selection = { +const mood = { "very positive": [ "It is certain.", "It is decidedly so.", @@ -80,20 +80,16 @@ const main_moods_selection = { ], }; -var state_of_mood = ""; -var state_answer = ""; - function mood_selector() { - var mood_keys = Object.keys(main_moods_selection); // stores all the mood types + var mood_keys = Object.keys(mood); // stores all the mood types var ran_mood = Math.floor(Math.random() * mood_keys.length); // generates random math for mood types var one_mood = mood_keys[ran_mood]; // stores one random mood - var one_mood_values = main_moods_selection[mood_keys[ran_mood]]; // stores the all the values from inside one selected mood + var one_mood_values = mood[mood_keys[ran_mood]]; // stores the all the values from inside one selected mood var random_one_mood_values = Math.floor( Math.random() * one_mood_values.length ); // generates random math for values of one selected mood var selected_ran_value = one_mood_values[random_one_mood_values]; // stores one selected random value - state_answer = selected_ran_value; - state_of_mood = one_mood; + return selected_ran_value; } // This should log "The ball has shaken!" // and return the answer. @@ -101,8 +97,7 @@ function shakeBall() { //Write your code in hereconst console.log("The ball has shaken!"); - mood_selector(); - return state_answer; + return mood_selector(); } /* @@ -114,20 +109,16 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer() { +const state_of_mood = (mood_object, sentence) => { + let key = Object.keys(mood_object).find((k) => + mood[k].find((mood_value) => mood_value === sentence) ? true : false + ); + return key; +}; +function checkAnswer(answer) { //Write your code in here - let indexOfAnswers = possibleAnswers.indexOf(answer); - - if (indexOfAnswers <= 4) { - return "very positive"; - } else if (indexOfAnswers >= 5 && indexOfAnswers < 10) { - return "positive"; - } else if (indexOfAnswers >= 10 && indexOfAnswers < 15) { - return "negative"; - } else if (indexOfAnswers >= 15 && indexOfAnswers < 20) { - return "very negative"; - } - return state_of_mood; + + return state_of_mood(mood, answer); } /* diff --git a/extra/4-test.js b/extra/4-test.js deleted file mode 100644 index c70afc6e6..000000000 --- a/extra/4-test.js +++ /dev/null @@ -1,160 +0,0 @@ -// const { toBeOneOf } = require("jest-extended"); -// expect.extend({ toBeOneOf }); -/** - Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/Magic_8-Ball - There are a few steps to being able view the future though: - * Ask a question - * Shake the ball - * Get an answer - * Decide if it's positive or negative - The question can be anything, but the answers are fixed, - and have different levels of positivity or negativity. - Below are the possible answers: - ## Very positive - 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. - ## Very negative - Don't count on it. - My reply is no. - My sources say no. - Outlook not so good. - Very doubtful. -*/ -const verypositive = [ - "It is certain.", - "It is decidedly so.", - "Without a doubt.", - "Yes - definitely.", - "You may rely on it.", -]; - -const positive = [ - "As I see it, yes.", - "Most likely.", - "Outlook good.", - "Yes.", - "Signs point to yes.", -]; - -const negative = [ - "Reply hazy, try again.", - "Ask again later.", - "Better not tell you now.", - "Cannot predict now.", - "Concentrate and ask again.", -]; - -const 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() { - console.log("The ball has shaken!"); - const allAnswers = [ - ...verypositive, - ...positive, - ...negative, - ...veryNegative, - ]; - return allAnswers[Math.round(Math.random() * allAnswers.length)]; - //Write your code in here -} - -/* - This function should say whether the answer it is given is - - very positive - - positive - - negative - - very negative - This function should expect to be called with any value which was returned by the shakeBall function. -*/ -function checkAnswer(answer) { - if (verypositive.includes(answer)) { - return "very positive"; - } else if (positive.includes(answer)) { - return "positive"; - } else if (negative.includes(answer)) { - return "negative"; - } else { - return "very negative"; - } - - //Write your code in here -} - -// console.log(checkAnswer(shakeBall())); -/* -================================== -======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. -To run these tests type `npm run extraTo run the tests for just this one file, type `npm run extra-tests -- --testPathPattern 3-magic-8-ball` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -================================== -*/ - -test("whole magic 8 ball sequence", () => { - const consoleLogSpy = jest.spyOn(global.console, "log"); - const answer = shakeBall(); - - expect(typeof answer).toEqual("string"); - - expect(consoleLogSpy).toHaveBeenCalledTimes(1); - expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - - expect(checkAnswer(answer)).toBeOneOf([ - "very positive", - "positive", - "negative", - "very negative", - ]); -}); - -test("magic 8 ball returns different values each time", () => { - const seenAnswers = new Set(); - for (let i = 0; i < 10; ++i) { - seenAnswers.add(shakeBall()); - } - if (seenAnswers.size < 2) { - throw Error( - "Expected to get different random answers each time shakeBall was called, but always got the same one" - ); - } - - 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" - ); - } -}); - -test("checkAnswer works for `It is decidedly so.`", () => { - expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); -}); - -test("checkAnswer works for `My reply is no.`", () => { - expect(checkAnswer("My reply is no.")).toEqual("very negative"); -}); diff --git a/extra/5-test.js b/extra/5-test.js deleted file mode 100644 index c2ab2ce9a..000000000 --- a/extra/5-test.js +++ /dev/null @@ -1,154 +0,0 @@ -const matchers = require("jest-extended"); -expect.extend(matchers); - -/** - Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/Magic_8-Ball - There are a few steps to being able view the future though: - * Ask a question - * Shake the ball - * Get an answer - * Decide if it's positive or negative - The question can be anything, but the answers are fixed, - and have different levels of positivity or negativity. - Below are the possible answers: - ## Very positive - 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. - ## Very negative - 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 - - // If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. - - 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.", - ]; - - let result = answers[Math.floor(Math.random() * 20)]; - console.log("The ball has shaken!"); - return result; -} - -/* - This function should say whether the answer it is given is - - very positive - - positive - - negative - - very negative - This function should expect to be called with any value which was returned by the shakeBall function. -*/ -function checkAnswer(givenAnswer) { - //Write your code in here - - // The indexOf() method returns the first index at which a given element can be found in the array, - // or -1 if it is not present. - let theIndex = answers.indexOf(givenAnswer); - if (theIndex < 5) { - return "very positive"; - } else if (theIndex < 10) { - return "positive"; - } else if (theIndex < 15) { - return "negative"; - } else { - return "very negative"; - } -} - -/* -================================== -======= 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 3-magic-8-ball` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -================================== -*/ - -test("whole magic 8 ball sequence", () => { - const consoleLogSpy = jest.spyOn(global.console, "log"); - const answer = shakeBall(); - - expect(typeof answer).toEqual("string"); - - expect(consoleLogSpy).toHaveBeenCalledTimes(1); - expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - - expect(checkAnswer(answer)).toBeOneOf([ - "very positive", - "positive", - "negative", - "very negative", - ]); -}); - -test("magic 8 ball returns different values each time", () => { - const seenAnswers = new Set(); - for (let i = 0; i < 10; ++i) { - seenAnswers.add(shakeBall()); - } - if (seenAnswers.size < 2) { - throw Error( - "Expected to get different random answers each time shakeBall was called, but always got the same one" - ); - } - - 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" - ); - } -}); - -test("checkAnswer works for `It is decidedly so.`", () => { - expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); -}); - -test("checkAnswer works for `My reply is no.`", () => { - expect(checkAnswer("My reply is no.")).toEqual("very negative"); -}); diff --git a/extra/6-test.js b/extra/6-test.js deleted file mode 100644 index 034274bf8..000000000 --- a/extra/6-test.js +++ /dev/null @@ -1,151 +0,0 @@ -/** - Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/Magic_8-Ball - There are a few steps to being able view the future though: - * Ask a question - * Shake the ball - * Get an answer - * Decide if it's positive or negative - The question can be anything, but the answers are fixed, - and have different levels of positivity or negativity. - Below are the possible answers: - ## Very positive - 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. - ## Very negative - Don't count on it. - My reply is no. - My sources say no. - Outlook not so good. - Very doubtful. -*/ -// test runner, jest-extended -const matchers = require("jest-extended"); -expect.extend(matchers); - -// possible answers arrays -let veryNegative = [ - "Don't count on it.", - "My reply is no.", - "My sources say no.", - "Outlook not so good.", - "Very doubtful.", -]; -let negative = [ - "Reply hazy, try again.", - "Ask again later.", - "Better not tell you now.", - "Cannot predict now.", - "Concentrate and ask again.", -]; -let positive = [ - "As I see it, yes.", - "Most likely.", - "Outlook good.", - "Yes.", - " Signs point to yes.", -]; -let veryPositive = [ - "It is certain.", - "It is decidedly so.", - "Without a doubt.", - "Yes - definitely.", - "You may rely on it.", -]; -let possibleAnswers = veryPositive.concat(positive, negative, veryNegative); - -// This should log "The ball has shaken!" -// and return the answer. -function shakeBall() { - //Write your code in here - console.log("The ball has shaken!"); - return possibleAnswers[ - Math.floor(Math.random() * possibleAnswers.length) + 1 - ]; -} - -/* - This function should say whether the answer it is given is - - very positive - - positive - - negative - - very negative - This function should expect to be called with any value which was returned by the shakeBall function. -*/ -function checkAnswer(answer) { - //Write your code in here - if (veryPositive.includes(answer)) return "very positive"; - if (positive.includes(answer)) return "positive"; - if (negative.includes(answer)) return "negative"; - if (veryNegative.includes(answer)) return "very negative"; -} - -/* -================================== -======= 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 3-magic-8-ball` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -================================== -*/ - -test("whole magic 8 ball sequence", () => { - const consoleLogSpy = jest.spyOn(global.console, "log"); - const answer = shakeBall(); - - expect(typeof answer).toEqual("string"); - - expect(consoleLogSpy).toHaveBeenCalledTimes(1); - expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!"); - - expect(checkAnswer(answer)).toBeOneOf([ - "very positive", - "positive", - "negative", - "very negative", - ]); -}); - -test("magic 8 ball returns different values each time", () => { - const seenAnswers = new Set(); - for (let i = 0; i < 10; ++i) { - seenAnswers.add(shakeBall()); - } - if (seenAnswers.size < 2) { - throw Error( - "Expected to get different random answers each time shakeBall was called, but always got the same one" - ); - } - - 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" - ); - } -}); - -test("checkAnswer works for `It is decidedly so.`", () => { - expect(checkAnswer("It is decidedly so.")).toEqual("very positive"); -}); - -test("checkAnswer works for `My reply is no.`", () => { - expect(checkAnswer("My reply is no.")).toEqual("very negative"); -}); From 1161b2fa1814fc1f3199af85878686bd794ea756 Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 21:21:40 +0000 Subject: [PATCH 10/11] 8 ball final checks of minor errors --- extra/3-magic-8-ball.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 65744c24c..482abb2c7 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,7 +1,7 @@ // // https://codepen.io/codifiedconcepts/pen/vXgvQL // https://github.com/mmaynar1/games/blob/master/magic-eight-ball/magic-eight-ball.js -// +// https://stackoverflow.com/questions/9907419/how-to-get-a-key-in-a-javascript-object-by-its-value /** Let's peer into the future using a Magic 8 Ball! @@ -46,9 +46,11 @@ Outlook not so good. Very doubtful. */ +// have to include the below two iline for one test below to work const matchers = require("jest-extended"); expect.extend(matchers); +// main list of moods and sentences const mood = { "very positive": [ "It is certain.", @@ -80,6 +82,7 @@ const mood = { ], }; +// sentencs selector for the 8 ball function mood_selector() { var mood_keys = Object.keys(mood); // stores all the mood types var ran_mood = Math.floor(Math.random() * mood_keys.length); // generates random math for mood types @@ -109,6 +112,7 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ +// mood checker for the check answer function const state_of_mood = (mood_object, sentence) => { let key = Object.keys(mood_object).find((k) => mood[k].find((mood_value) => mood_value === sentence) ? true : false From c64ebd88f8cd616557d0f5ffe142198f2a99c70e Mon Sep 17 00:00:00 2001 From: nasir ali Date: Fri, 17 Feb 2023 21:33:16 +0000 Subject: [PATCH 11/11] fixed the extra 1 --- extra/1-currency-conversion.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 1f6c70af2..c3ee22238 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -19,10 +19,7 @@ function convertToUSD(stirling) { */ function convertToBRL(stirling) { - fee = (stirling / 100) * 1; - final = stirling - fee; - real = final * 5.7; - return real.toFixed(2); + return Math.round(stirling * 0.99 * 5.7 * 100) / 100; } /* ======= TESTS - DO NOT MODIFY =====