diff --git a/README.md b/README.md index 6611d00c3..653c21722 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Coursework +n# Coursework Like learning a musical instrument, programming requires daily practice. @@ -10,7 +10,6 @@ If you think you need to do more practice with the basics, then you can find som ## Setting up your code editor - There are some tools that will help you to write code. One of these, [Prettier](https://prettier.io/), formats your code, making it easier for you and others to read. ### 1. Install prettier @@ -25,10 +24,9 @@ There are some tools that will help you to write code. One of these, [Prettier]( - Search for `editor format` - Set `editor.formatOnSave` and `editor.formatOnPaste` to true - ## 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/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..24d480e94 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(UKPrice) { + const USPrice = UKPrice * 1.4; + return USPrice; +} /* CURRENCY CONVERSION @@ -15,27 +18,30 @@ 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(UKPrice) { + const BRLPrice = 0.99 * UKPrice * 5.7; + return Number(BRLPrice.toFixed(2)); +} -/* ======= TESTS - DO NOT MODIFY ===== +/* ======= 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 1-currency-conversion` into your terminal (Reminder: You must have run `npm install` one time before this will work!) */ -test("convertToUSD function works for £32", () => { +test('convertToUSD function works for £32', () => { expect(convertToUSD(32)).toEqual(44.8); }); -test("convertToUSD function works for £50", () => { +test('convertToUSD function works for £50', () => { expect(convertToUSD(50)).toEqual(70); }); -test("convertToBRL function works for £30", () => { +test('convertToBRL function works for £30', () => { expect(convertToBRL(30)).toEqual(169.29); }); -test("convertToBRL function works for £1.50", () => { +test('convertToBRL function works for £1.50', () => { expect(convertToBRL(1.5)).toEqual(8.46); }); diff --git a/extra/2-piping.js b/extra/2-piping.js index b4f8c4c1b..2e423358f 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -11,63 +11,69 @@ - add 10 to startingValue - multiply the result by 2 - format it - + 3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign the final result to the variable goodCode */ -function add() { - +function add(num1, num2) { + return num1 + num2; } -function multiply() { - +function multiply(num1, num2) { + return num1 * num2; } -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 goodCodeFunc = (startingValue) => { + const added = add(startingValue, 10); + const multiplied = multiply(added, 2); + const result = format(multiplied); + return result; +}; -/* ======= TESTS - DO NOT MODIFY ===== +let goodCode = goodCodeFunc(startingValue); +/* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. - +ipe(add, multiply, format)(numb); 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!) */ -test("add function - case 1 works", () => { +test('add function - case 1 works', () => { expect(add(1, 3)).toEqual(4); }); -test("add function - case 2 works", () => { +test('add function - case 2 works', () => { expect(add(2.4, 5)).toEqual(7.4); }); -test("multiply function works", () => { +test('multiply function works', () => { expect(multiply(2, 3)).toEqual(6); }); -test("format function works for whole number", () => { - expect(format(16)).toEqual("£16"); +test('format function works for whole number', () => { + expect(format(16)).toEqual('£16'); }); -test("format function works for decimal number", () => { - expect(format(10.1)).toEqual("£10.1"); +test('format function works for decimal number', () => { + expect(format(10.1)).toEqual('£10.1'); }); -test("badCode variable correctly assigned", () => { - expect(badCode).toEqual("£24"); +test('badCode variable correctly assigned', () => { + expect(badCode).toEqual('£24'); }); -test("goodCode variable correctly assigned", () => { - expect(goodCode).toEqual("£24"); +test('goodCode variable correctly assigned', () => { + expect(goodCode).toEqual('£24'); }); diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..8a625ea20 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,7 +1,7 @@ /** Let's peer into the future using a Magic 8 Ball! - https://en.wikipedia.org/wiki/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 @@ -43,26 +43,75 @@ Very doubtful. */ +// This should log "The ball has shaken!" +// and return the answer. +let veryPositiveAnswers = [ + 'It is certain.', + 'It is decidedly so.', + 'Without a doubt.', + 'Yes - definitely.', + 'You may rely on it.', +]; + +let positiveAnswers = [ + 'As I see it, yes.', + 'Most likely.', + 'Outlook good.', + 'Yes.', + 'Signs point to yes.', +]; + +let negativeAnswers = [ + 'Reply hazy, try again.', + 'Ask again later.', + 'Better not tell you now.', + 'Cannot predict now.', + 'Concentrate and ask again.', +]; + +let veryNegativeAnswers = [ + "Don't count on it.", + 'My reply is no.', + 'My sources say no.', + 'Outlook not so good.', + 'Very doubtful.', +]; + +let allAnswers = [ + ...veryPositiveAnswers, + ...positiveAnswers, + ...negativeAnswers, + ...veryNegativeAnswers, +]; + // 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 randomIndex = Math.floor(Math.random() * allAnswers.length); + return allAnswers[randomIndex]; } -/* +/* 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 (veryPositiveAnswers.includes(answer)) { + return 'very positive'; + } else if (positiveAnswers.includes(answer)) { + return 'positive'; + } else if (negativeAnswers.includes(answer)) { + return 'negative'; + } else if (veryNegativeAnswers.includes(answer)) { + return 'very negative'; + } } - -/* +/* ================================== ======= TESTS - DO NOT MODIFY ===== @@ -73,46 +122,41 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-m ================================== */ -test("whole magic 8 ball sequence", () => { - const consoleLogSpy = jest.spyOn(global.console, "log"); +test('whole magic 8 ball sequence', () => { + const consoleLogSpy = jest.spyOn(global.console, 'log'); const answer = shakeBall(); - expect(typeof answer).toEqual("string"); + 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", - ]); + 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", () => { +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" + '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" + '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 `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"); +test('checkAnswer works for `My reply is no.`', () => { + expect(checkAnswer('My reply is no.')).toEqual('very negative'); }); diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..bd6df53a9 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,19 +1,20 @@ // 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}`; } -/* +/* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== @@ -25,16 +26,14 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 1-s =================================================== */ -test("addNumbers adds numbers correctly", () => { +test('addNumbers adds numbers correctly', () => { expect(addNumbers(3, 4, 6)).toEqual(13); }); -test("introduceMe function returns the correct string", () => { - expect(introduceMe("Sonjide", 27)).toEqual( - "Hello, my name is Sonjide and I am 27 years old" - ); +test('introduceMe function returns the correct string', () => { + expect(introduceMe('Sonjide', 27)).toEqual('Hello, my name is Sonjide and I am 27 years old'); }); -test("getTotal returns a string describing the total", () => { - expect(getTotal(23, 5)).toEqual("The total is 28"); +test('getTotal returns a string describing the total', () => { + expect(getTotal(23, 5)).toEqual('The total is 28'); }); diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9eb8c8cd7..a8ed8fc09 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,19 +1,18 @@ // 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; } -/* +/* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== @@ -24,28 +23,26 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 2-l =================================================== */ -test("trimWord trims leading and trailing whitespace", () => { - expect(trimWord(" CodeYourFuture ")).toEqual("CodeYourFuture"); +test('trimWord trims leading and trailing whitespace', () => { + expect(trimWord(' CodeYourFuture ')).toEqual('CodeYourFuture'); }); test("trimWord doesn't remove whitespace in the middle of the string", () => { - expect(trimWord(" CodeYourFuture teaches coding ")).toEqual( - "CodeYourFuture teaches coding" - ); + expect(trimWord(' CodeYourFuture teaches coding ')).toEqual('CodeYourFuture teaches coding'); }); -test("getStringLength returns the length of a word", () => { - expect(getStringLength("Turtles")).toEqual(7); +test('getStringLength returns the length of a word', () => { + expect(getStringLength('Turtles')).toEqual(7); }); -test("getStringLength returns the length of a sentence", () => { - expect(getStringLength("A wild sentence appeared!")).toEqual(25); +test('getStringLength returns the length of a sentence', () => { + expect(getStringLength('A wild sentence appeared!')).toEqual(25); }); -test("multiply multiplies numbers", () => { +test('multiply multiplies numbers', () => { expect(multiply(2, 3, 6)).toEqual(36); }); -test("multiply multiplies different numbers", () => { +test('multiply multiplies different numbers', () => { expect(multiply(2, 3, 4)).toEqual(24); }); diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..90cdca882 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -2,18 +2,22 @@ function getRandomNumber() { return Math.random() * 10; } +// Function getRandomNumber generates random number between 0 and 1, and multiplies the result by 10. So output of this function is random number between 0 and 10. // Add comments to explain what this function does. You're meant to use Google! function combine2Words(word1, word2) { return word1.concat(word2); } +// Function combine2Words concatinates two strings, that were given to this function as the parameters. If word1 is string "Hello" and word2 is "World", the output of function combine2Words will be "HelloWorld". + 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. + return firstWord.concat(' ', secondWord, ' ', thirdWord); } -/* +/* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== @@ -24,14 +28,14 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-f ================================== */ -test("concatenate example #1", () => { - expect(concatenate("code", "your", "future")).toEqual("code your future"); +test('concatenate example #1', () => { + expect(concatenate('code', 'your', 'future')).toEqual('code your future'); }); -test("concatenate example #2", () => { - expect(concatenate("I", "like", "pizza")).toEqual("I like pizza"); +test('concatenate example #2', () => { + expect(concatenate('I', 'like', 'pizza')).toEqual('I like pizza'); }); test("concatenate doesn't only accept strings", () => { - expect(concatenate("I", "am", 13)).toEqual("I am 13"); + expect(concatenate('I', 'am', 13)).toEqual('I am 13'); }); diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..017257d37 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(sales) { + const tax = 0.2 * sales; + return tax + sales; +} /* CURRENCY FORMATTING @@ -17,9 +20,12 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(sales) { + const tax = 0.2 * sales; + return `£${(sales + tax).toFixed(2)}`; +} -/* +/* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== @@ -30,26 +36,26 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 4-t =================================================== */ -test("calculateSalesTax for £15", () => { +test('calculateSalesTax for £15', () => { expect(calculateSalesTax(15)).toEqual(18); }); -test("calculateSalesTax for £17.50", () => { +test('calculateSalesTax for £17.50', () => { expect(calculateSalesTax(17.5)).toEqual(21); }); -test("calculateSalesTax for £34", () => { +test('calculateSalesTax for £34', () => { expect(calculateSalesTax(34)).toEqual(40.8); }); -test("addTaxAndFormatCurrency for £15", () => { - expect(addTaxAndFormatCurrency(15)).toEqual("£18.00"); +test('addTaxAndFormatCurrency for £15', () => { + expect(addTaxAndFormatCurrency(15)).toEqual('£18.00'); }); -test("addTaxAndFormatCurrency for £17.50", () => { - expect(addTaxAndFormatCurrency(17.5)).toEqual("£21.00"); +test('addTaxAndFormatCurrency for £17.50', () => { + expect(addTaxAndFormatCurrency(17.5)).toEqual('£21.00'); }); -test("addTaxAndFormatCurrency for £34", () => { - expect(addTaxAndFormatCurrency(34)).toEqual("£40.80"); +test('addTaxAndFormatCurrency for £34', () => { + expect(addTaxAndFormatCurrency(34)).toEqual('£40.80'); });