diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..77ab3e457 Binary files /dev/null and b/.DS_Store differ diff --git a/Untitled/.DS_Store b/Untitled/.DS_Store new file mode 100644 index 000000000..5008ddfcf Binary files /dev/null and b/Untitled/.DS_Store differ diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..d25c8ac2d 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(price) { + let total=price * 1.4; + return total; +} /* CURRENCY CONVERSION @@ -15,7 +18,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(price) { + let total=parseFloat((((price/100)*99)*5.7).toFixed(2)); + return total; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..eb1f7bfb8 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,40 @@ the final result to the variable goodCode */ -function add() { - +function add(a,b) { +let sum=a+b; +return sum; } -function multiply() { +function multiply(a,b) { +let mult=a*b; +return mult; } -function format() { - +function format(num) { +return "£"+num; } -const startingValue = 2; + // Why can this code be seen as bad practice? Comment your answer. -let badCode = +//const is local variabe and we can't change its value +const startingValue = 2; + +let badCode =format(multiply(add(startingValue,10),2)); +console.log(badCode); + /* BETTER PRACTICE */ +//it is not easy to read and understand/ + +//creating variable with let;// -let goodCode = +let summ= add(startingValue,10); +let multiplyy=multiply(summ,2); +let goodCode=format(multiplyy); +/* we can understand this code better / /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..d85419e68 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,40 +1,32 @@ /** - 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 + ## 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. @@ -43,11 +35,70 @@ 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.", +]; + +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.", +]; + // This should log "The ball has shaken!" // and return the answer. function shakeBall() { - //Write your code in here -} + + console.log("The ball has shaken!"); + let finalAnswer = Math.floor(Math.random() * answers.length); + return answers[finalAnswer]; +}; /* This function should say whether the answer it is given is @@ -55,33 +106,39 @@ function shakeBall() { - 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"; + } + else if(positive.includes(answer)) { + return "positive"; + } + else if (negative.includes(answer)) { + 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!) ================================== */ +const { toBeOneOf } = require("jest-extended"); 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", @@ -89,7 +146,6 @@ test("whole magic 8 ball sequence", () => { "very negative", ]); }); - test("magic 8 ball returns different values each time", () => { const seenAnswers = new Set(); for (let i = 0; i < 10; ++i) { @@ -100,7 +156,6 @@ test("magic 8 ball returns different values each time", () => { "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( @@ -108,11 +163,9 @@ test("magic 8 ball returns different values each time", () => { ); } }); - 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"); -}); +}); \ No newline at end of file diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..6df0ed926 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 "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..568320e01 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,16 @@ // 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; + multyple = a * b * c; + return multyple; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..8b26ce017 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,4 +1,5 @@ // Add comments to explain what this function does. You're meant to use Google! +//The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0 function getRandomNumber() { return Math.random() * 10; } @@ -7,10 +8,12 @@ function getRandomNumber() { function combine2Words(word1, word2) { return word1.concat(word2); } +// The concat() method joins two or more strings function concatenate(firstWord, secondWord, thirdWord) { // Write the body of this function to concatenate three words together. - // Look at the test case below to understand what this function is expected to return. + // Look at the test case below to understand what this function is expected to return. + return firstWord.concat(" ",secondWord," ",thirdWord); } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..7d00a9db3 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,11 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(price) { +return price + (price/100)*20 ; + + +} /* CURRENCY FORMATTING @@ -17,7 +21,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) { + total=calculateSalesTax(price).toFixed(2) + return "£"+total; +} /* ===================================================