diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..d5670d82b 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -4,8 +4,18 @@ The business is breaking out into a new market and need to convert prices to USD Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ +function convertToUSD(pound) { + let usd = pound * 1.4; + return usd; + // return "$" + pound * 1.4.toFixed(2); +} -function convertToUSD() {} +// function convertToUSD(amountInPounds) { +// const exchangeRate = 1.4; +// return amountInPounds * exchangeRate; +// } // suggested in 'solutions' + +// console.log(convertToUSD(200)); /* CURRENCY CONVERSION @@ -15,8 +25,19 @@ function convertToUSD() {} They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. */ -function convertToBRL() {} +function convertToBRL(pound) { + let brl = parseFloat(((pound / 100) * 99 * 5.7).toFixed(2)); + return brl; +} + +// function convertToBRL(amountInPounds) { +// const transactionFee = 0.01; +// const exchangeRate = 5.7; +// const amountAfterFee = amountInPounds * (1 - transactionFee); +// return amountAfterFee * exchangeRate; +// } //suggested in 'solutions' +// console.log(convertToBRL(10)); /* ======= 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..167b569fd 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,52 @@ the final result to the variable goodCode */ -function add() { - +function add(a, b) { + return a + b; } -function multiply() { +console.log(add(20, 9)); +function multiply(a, b) { + return a * b; } -function format() { +console.log(multiply(2, 10)); +function format(num) { + return "£" + eval(num); } +console.log(format(45)); + const startingValue = 2; +function badPractice() { + return badCode; +} + +let badCode = "£" + (startingValue + 10) * 2; + +console.log(badPractice()); + +let incrementedValue = startingValue + 10; +let doubledValue = incrementedValue * 2; +let formattedValue = "£" + doubledValue; + +function goodPractice() { + return goodCode; +} + +let goodCode = formattedValue; + +// let goodCode = format(multiply(add(startingValue, 10), 2)); //suggested in 'solutions' // Why can this code be seen as bad practice? Comment your answer. -let badCode = + +// Const should be used for the values we don't re-assign. The name startingValue suggests otherwise /* BETTER PRACTICE */ -let goodCode = +// let goodCode = /* ======= 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..9716db3a2 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -43,11 +43,46 @@ Very doubtful. */ +let 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 displayAnswer() { + let index = Math.floor(Math.random() * answers.length); + let answer = answers[index]; + + return answer; +} + +console.log(displayAnswer()); function shakeBall() { - //Write your code in here + console.log("The ball has shaken!"); + return displayAnswer(); } +let answer = shakeBall(); +console.log("my answer is " + answer); /* This function should say whether the answer it is given is @@ -58,9 +93,81 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ -function checkAnswer(answer) { +function checkAnswer(answerToCheck) { //Write your code in here + + if (answers.indexOf(answerToCheck) < 5) { + return "very positive"; + } else if (answers.indexOf(answerToCheck) < 10) { + return "positive"; + } else if (answers.indexOf(answerToCheck) < 15) { + return "negative"; + } else { + return "very negative"; + } } +console.log(checkAnswer(answer)); + +// console.log(displayAnswer()); + +// const veryNegativeAnswers = [ +// "Don't count on it.", +// 'My reply is no.', +// 'My sources say no.', +// 'Outlook not so good.', +// 'Very doubtful.', +// ]; +// const negativeAnswers = [ +// 'Reply hazy, try again.', +// 'Ask again later.', +// 'Better not tell you now.', +// 'Cannot predict now.', +// 'Concentrate and ask again.', +// ]; +// const positiveAnswers = [ +// 'As I see it, yes.', +// 'Most likely.', +// 'Outlook good.', +// 'Yes.', +// 'Signs point to yes.', +// ]; +// const veryPositiveAnswers = [ +// 'You may rely on it', +// 'You may rely on it', +// 'It is decidedly so.', +// 'It is certain.', +// ]; +// const possibleAnswers = veryNegativeAnswers.concat( +// negativeAnswers, +// positiveAnswers, +// veryPositiveAnswers +// ); +// // This should log "The ball has shaken!" +// // and return the answer. +// function shakeBall() { +// //Write your code in here +// const randomIndex = Math.floor(Math.random() * possibleAnswers.length); +// const randomAnswer = possibleAnswers[randomIndex]; +// const message = 'The ball has shaken!'; +// console.log(message); +// return randomAnswer; +// } + +// /* +// 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) { +// const answerIndex = possibleAnswers.indexOf(answer); +// if (answerIndex >= 15) return 'very positive'; +// if (answerIndex >= 10) return 'positive'; +// if (answerIndex >= 5) return 'negative'; +// return 'very negative'; +// } //suggested in 'solutions' /* ================================== @@ -101,7 +208,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" diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..9104a8327 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,18 +1,19 @@ // 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; + let total = a + b; - return "The total is total"; + return "The total is " + total; } - +// // /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9eb8c8cd7..2adb4e95a 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; + return a * b * c; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..dc68855bb 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,28 @@ // Add comments to explain what this function does. You're meant to use Google! + +// the output is what the function returns, which is Math.random() * 10; _ random method class Math = The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, then * 10 and it could be a number < 10 && >0 + function getRandomNumber() { - return Math.random() * 10; + return Math.random() * 10; } - + // Add comments to explain what this function does. You're meant to use Google! +// The concat() method concatenates the string arguments to the calling string and returns a new string. in this case the two argument will be joined without any space in between function combine2Words(word1, word2) { return word1.concat(word2); } + + function concatenate(firstWord, secondWord, thirdWord) { + return firstWord.concat(` ${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. } - +console.log(concatenate("code" , "your", "future")); + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..1fb81fd3a 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,15 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} + +function calculateSalesTax(productPrice) { + let salesTax = productPrice * 0.2; + return productPrice + salesTax +} + +// let totalPrice1 = calculateSalesTax(200); +// console.log(totalPrice1); + /* CURRENCY FORMATTING @@ -17,7 +25,13 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + let productPriceRes = calculateSalesTax(price); + + return `£${productPriceRes.toFixed(2)}`; + +} + /* ===================================================