diff --git a/README.md b/README.md index 6611d00c3..5c862b7a6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Coursework + # 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..8d3f576f2 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) { + return price*1.4; + +} /* CURRENCY CONVERSION @@ -15,7 +18,13 @@ 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(PoundExchange) { + let exchangeRate = 5.7; + let fee = 0.01; + PoundExchange = (PoundExchange * exchangeRate) * (1 - fee); + return parseFloat(PoundExchange.toFixed(2)); +} /* ======= 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..19b2c5610 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,29 @@ the final result to the variable goodCode */ -function add() { - +function add(a,b) { +return a + b; } -function multiply() { - +function multiply(a,b) { +return a*b; } -function format() { +function format(number) { + return '£'+ number; } 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 addedValue = add(startingValue, 10); +let multipliedValue = multiply(addedValue, 2); +let goodCode = format(multipliedValue); /* ======= 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/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index d9e004465..dda84c3e5 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; - - return "The total is total"; + total = a + b; + 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; } /* diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..7c18fbd5f 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,14 +1,20 @@ // Add comments to explain what this function does. You're meant to use Google! +//this function generates a random value between 0(inclusive) and 10(exlusive) everytime is called +//Math.random generates a number between 0(inclusive ) and 1(exlusive) and then the function multiplies it with 10 function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +//this function with combine/joins two strings together and will return a + function combine2Words(word1, word2) { return word1.concat(word2); } function concatenate(firstWord, secondWord, thirdWord) { + return `${firstWord.toString()} ${secondWord.toString()} ${thirdWord.toString()}`; + // 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. } diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..bd01e6fd2 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,10 +5,15 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(ProductPrice) { + let Tax = ProductPrice * 0.2; + let total = ProductPrice + Tax; + return total; +} /* CURRENCY FORMATTING + =================== The business has informed you that prices must have 2 decimal places They must also start with the currency symbol @@ -17,8 +22,12 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(ProductPrice) { + let Tax = ProductPrice * 0.2; + let total = ProductPrice + Tax; + return `£${total.toFixed(2)}`; +} /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE =====