From c6d2d2838dd09714ed6b7a926df397244910cb2a Mon Sep 17 00:00:00 2001 From: Laura TM <67758358+Laura-TM@users.noreply.github.com> Date: Wed, 20 Jan 2021 22:00:40 +0000 Subject: [PATCH 1/4] Solutions to JavaScript problems --- exercises/B-hello-world/exercise.js | 8 +++- exercises/C-variables/exercise.js | 10 ++++- exercises/D-strings/exercise.js | 6 ++- exercises/E-strings-concatenation/exercise.js | 11 ++++- exercises/F-strings-methods/exercise.js | 12 +++++- exercises/F-strings-methods/exercise2.js | 5 +++ exercises/G-numbers/README.md | 4 +- exercises/G-numbers/exercise.js | 9 ++++ exercises/I-floats/README.md | 2 +- exercises/I-floats/exercise.js | 14 ++++++ exercises/J-functions/exercise.js | 9 +++- exercises/J-functions/exercise2.js | 7 ++- exercises/K-functions-parameters/README.md | 2 +- exercises/K-functions-parameters/exercise.js | 7 ++- exercises/K-functions-parameters/exercise2.js | 7 +++ exercises/K-functions-parameters/exercise3.js | 9 +++- exercises/K-functions-parameters/exercise4.js | 10 +++-- exercises/K-functions-parameters/exercise5.js | 13 +++++- exercises/L-functions-nested/README.md | 2 +- exercises/L-functions-nested/exercise.js | 5 --- exercises/L-functions-nested/exercise1A.js | 18 ++++++++ exercises/L-functions-nested/exercise1B.js | 19 ++++++++ exercises/L-functions-nested/exercise1C.js | 18 ++++++++ exercises/L-functions-nested/exercise2.js | 23 ++++++++++ extra/1-currency-conversion.js | 19 +++++++- extra/2-piping.js | 32 ++++++++++---- extra/3-magic-8-ball.js | 43 ++++++++++++++++--- mandatory/1-syntax-errors.js | 16 ++++--- mandatory/2-logic-error.js | 10 +++-- mandatory/3-function-output.js | 12 ++++++ mandatory/4-tax.js | 19 ++++++-- 31 files changed, 325 insertions(+), 56 deletions(-) delete mode 100644 exercises/L-functions-nested/exercise.js create mode 100644 exercises/L-functions-nested/exercise1A.js create mode 100644 exercises/L-functions-nested/exercise1B.js create mode 100644 exercises/L-functions-nested/exercise1C.js create mode 100644 exercises/L-functions-nested/exercise2.js diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..d9a0a7c09 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,7 @@ -console.log("Hello world"); +// Various console.log messages at the same time +console.log('Hello world'); +console.log(5); // Numbers on their own won't return errors as they are a different type of data +console.log('Hello World. I just started learning JavaScript!'); +console.log('Returned to this exercise and changed this message!'); +console.log('Sometimes I spent too long getting bored'); +// Noticed that error messages appear on the terminal when words are not passed as strings (with quotation marks) \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..527249f15 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,9 @@ // Start by creating a variable `greeting` - -console.log(greeting); +var greeting = 'Aqui estoy!'; +// console.log(greeting); +// console.log(greeting); +// console.log(greeting); +for(i = 0; i < 3; i++) { + console.log(greeting); +} +// First tried the basic way and then made a for-loop to try an alternative \ No newline at end of file diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..e8d91e4cd 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - -console.log(message); +// Then another variable to store the data type and finally log it to the console +var message = 'This is a string data type'; +var messageType = typeof message; +console.log(messageType); \ No newline at end of file diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..efc7f2582 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,12 @@ // Start by creating a variable `message` -console.log(message); +// I tried this task first with variables in an individual form, but then decided to put them all in a function + +function getGreeting(name) { + var greeting = 'Hola'; + var courtesy = ', ¿cómo estás hoy?'; + var message = greeting + ' ' + name + courtesy; + return message; +} +var name = 'Laurita'; +console.log(getGreeting(name)); \ No newline at end of file diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..1f469279f 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,13 @@ // Start by creating a variable `message` -console.log(message); +// I first tried it with variables (line 5, 6, 13) and then -after the functions exercises, made a function here too (lines 8 to 13) + +// var myName = 'Laurita'; +// var myGreeting = 'My name is ' + myName + ' and my name is ' + myName.length + ' characters long'; + +function getGreeting(name) { + var myGreeting = 'My name is ' + name + ' and my name is ' + name.length + ' characters long'; + return myGreeting; +} +var name = 'Laurita' +console.log(getGreeting(name)); \ No newline at end of file diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..80028aa63 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,8 @@ +// There are two ariables, one to store a name plus the other one to store a message +// The .trim() method was applied to the name to reduce the unnecessary whitespace + const name = " Daniel "; +const message = ' My name is ' + name.trim() + ' and my name is ' + name.trim().length + ' characters long'; + console.log(message); diff --git a/exercises/G-numbers/README.md b/exercises/G-numbers/README.md index 4c6f45a41..1e8b18a82 100644 --- a/exercises/G-numbers/README.md +++ b/exercises/G-numbers/README.md @@ -6,7 +6,7 @@ Unlike strings, numbers do not need to be wrapped in quotes. var age = 30; ``` -You can use mathematical operators to caclulate numbers: +You can use mathematical operators to calculate numbers: ```js var sum = 10 + 2; // 12 @@ -25,5 +25,5 @@ var difference = 10 - 2; // 8 ``` Number of students: 15 Number of mentors: 8 -Total numnber of students and mentors: 23 +Total number of students and mentors: 23 ``` diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..d8927765a 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,10 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` + +// Some variables were created to store numbers and a sentence, which then was logged to the console + +let numberOfStudents = 30; +let numberOfMentors = 10; + +let numberOfAdults = 'The total amount of adults is ' + (numberOfStudents + numberOfMentors); + +console.log(numberOfAdults); \ No newline at end of file diff --git a/exercises/I-floats/README.md b/exercises/I-floats/README.md index e7c399979..d19233057 100644 --- a/exercises/I-floats/README.md +++ b/exercises/I-floats/README.md @@ -8,7 +8,7 @@ Floats can be rounded to the nearest whole number using the `Math.round` functio ```js var preciseAge = 30.612437; -var roughAge = Math.round(preciseAge); // 30 +var roughAge = Math.round(preciseAge); // 31 ``` ## Exercise diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..809f954f0 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,16 @@ + var numberOfStudents = 15; var numberOfMentors = 8; + +// Variables created to generate the percentages and then the messages expected, which were then logged to the console + +var numberOfAdults = numberOfMentors + numberOfStudents; + +var percentageStudents = (numberOfStudents * 100) / numberOfAdults; +var percentageMentors = (numberOfMentors * 100) / numberOfAdults; + +var totalStudents = 'Percentage of students: ' + Math.round(percentageStudents) + '%'; +var totalMentors = 'Percentage of mentors: ' + Math.round(percentageMentors) + '%'; + +console.log(totalStudents); +console.log(totalMentors); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..9258e644b 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,14 @@ + function halve(number) { - // complete the function here + // Complete the function here + let half = number / 2; + return half; } var result = halve(12); +// Called the function various times + console.log(result); +console.log(halve(4)); +console.log(halve(1458690980)); \ No newline at end of file diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..0c885a094 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,12 @@ + function triple(number) { - // complete function here + // Completed function here to trebble a number and called it several times + let trebbledNum = number * 3; + return trebbledNum; } var result = triple(12); console.log(result); +console.log(triple(39)); +console.log(triple(1)); \ No newline at end of file diff --git a/exercises/K-functions-parameters/README.md b/exercises/K-functions-parameters/README.md index 02ab2a844..f860d8809 100644 --- a/exercises/K-functions-parameters/README.md +++ b/exercises/K-functions-parameters/README.md @@ -10,7 +10,7 @@ function add(a, b) { When you write a function (sometimes called _declaring a function_) you assign names to the parameters inside of the parentheses (`()`). Parameters can be called anything. -This function is exactly the same as the on above: +This function is exactly the same as the one above: ```js function add(num1, num2) { diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..f2a57da44 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,12 @@ + // Complete the function so that it takes input parameters -function multiply() { +function multiply(num1, num2) { // Calculate the result of the function and return it + return num1 * num2; } // Assign the result of calling the function the variable `result` var result = multiply(3, 4); -console.log(result); +// Call the variable to show the value returned with the above function +console.log(result); \ No newline at end of file diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..ae9d3ec9e 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,12 @@ + // Declare your function first +function divide(num1, num2) { + // Write the block of code inside the curly braces + return num1 / num2; +} +// Store the value of this function on a variable var result = divide(3, 4); +// Call the variable console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..ed93dbde1 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,12 @@ -// Write your function here +// Write your function here with the block of code inside the curly braces +function createGreeting(name) { + let myGreeting = 'Hello, my name is ' + name; + return myGreeting; +} + +// Store the value in a variable var greeting = createGreeting("Daniel"); +// Call the variable console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..b127c8fec 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,9 @@ -// Declare your function first +// Declare your function first +function mySum(num1, num2) { + let sum = num1 + num2; + return sum; +} // Call the function and assign to a variable `sum` - -console.log(sum); +let totalSum = mySum(13, 124); +console.log(totalSum); \ No newline at end of file diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..7460eb2ae 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,14 @@ + // Declare your function here +function myGreeting(name, age) { + let myName = 'Hello, my name is ' + name; + let myAge = ' and I am ' + age + ' years old.'; + let fullGreeting = myName + myAge; + return fullGreeting; +} -const greeting = createLongGreeting("Daniel", 30); +// Store the value in a variable +const greeting = myGreeting("Daniel", 30); -console.log(greeting); +// Call the variable +console.log(greeting); \ No newline at end of file diff --git a/exercises/L-functions-nested/README.md b/exercises/L-functions-nested/README.md index f5e571303..a1c0b34f3 100644 --- a/exercises/L-functions-nested/README.md +++ b/exercises/L-functions-nested/README.md @@ -20,7 +20,7 @@ function createCreeting(name, age) { ## Exercise 1 - In `exercise.js` write a program that displays the percentage of students and mentors in the group -- The percentage should be rounded to the nearest whole number (use a search engine to find out how to this with JavaScript) +- The percentage should be rounded to the nearest whole number (use a search engine to find out how to do this with JavaScript) - You should have one function that calculates the percentage, and one function that creates a message > Consider: should your percentage function do the rounding, or should it be done when the greeting is created? diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js deleted file mode 100644 index a5d377442..000000000 --- a/exercises/L-functions-nested/exercise.js +++ /dev/null @@ -1,5 +0,0 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; diff --git a/exercises/L-functions-nested/exercise1A.js b/exercises/L-functions-nested/exercise1A.js new file mode 100644 index 000000000..bfca57f6c --- /dev/null +++ b/exercises/L-functions-nested/exercise1A.js @@ -0,0 +1,18 @@ +// Option A [I had help to do this task, because I couldn't work it out by myself] + +// First, created a function that calculated the percentage and round it +function getPercentage(subset, total) { + let percentage = Math.round((subset * 100) / total); + return percentage; +} +// After that, created another function that generated sentences using the percentage's function above +function getMessage(numberOfStudents, numberOfMentors) { + let total = numberOfStudents + numberOfMentors; + let studentPercentage = getPercentage(numberOfStudents, total); + let mentorPercentage = getPercentage(numberOfMentors, total); + let studentMessage = "Percentage of students: " + studentPercentage + "%"; + let mentorMessage = "Percentage of mentors: " + mentorPercentage + "%"; + return studentMessage + "\n" + mentorMessage; +} +// Called the last function to generate the sentences expected +console.log(getMessage(16, 30)); diff --git a/exercises/L-functions-nested/exercise1B.js b/exercises/L-functions-nested/exercise1B.js new file mode 100644 index 000000000..881ab5922 --- /dev/null +++ b/exercises/L-functions-nested/exercise1B.js @@ -0,0 +1,19 @@ +// Option B [I had help to do this task, because I couldn't work it out by myself] + +// Created two separate functions +function getPercentage(subset, total) { + let percentage = Math.round((subset * 100) / total); + return percentage; +} +function getMessage(group, percentage) { + let message = "Percentage of " + group + ": " + percentage + "%"; + return message; +} +// Created variables to store values from the functions and call them for the students +let studentsPercentage = getPercentage(16, 46); +let studentsMessage = getMessage("students", studentsPercentage); +console.log(studentsMessage); +// Created variables to store values from the functions and call them for the mentors +let mentorsPercentage = getPercentage(30, 46); +let mentorsMessage = getMessage("mentors", mentorsPercentage); +console.log(mentorsMessage); diff --git a/exercises/L-functions-nested/exercise1C.js b/exercises/L-functions-nested/exercise1C.js new file mode 100644 index 000000000..f2760e07a --- /dev/null +++ b/exercises/L-functions-nested/exercise1C.js @@ -0,0 +1,18 @@ +// Option C + +// Created a function with several variables that when called would return the expected output +function doPercentage(mentors, students) { + let allStudents = students; + let allMentors = mentors; + let allAdults = allStudents + allMentors; + let percentageStudents = (allStudents * 100) / allAdults; + let percentageMentors = (allMentors * 100) / allAdults; + let allPercentagesS = + "Percentage of students: " + Math.round(percentageStudents) + "%."; + let allPercentagesM = + "Percentage of mentors: " + Math.round(percentageMentors) + "%."; + let allPercentages = allPercentagesS + allPercentagesM; + return allPercentages; + } + + console.log(doPercentage(16, 30)); \ No newline at end of file diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..ac1be200a --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,23 @@ +// I got rid of this list of variables and made it into an array below + +// var mentor1 = "Daniel"; +// var mentor2 = "Irina"; +// var mentor3 = "Mimi"; +// var mentor4 = "Rob"; +// var mentor5 = "Yohannes"; + +// Created a nested function, the first one turns letters upper case and the second one displays the greeting to the mentors +function capitalisedString(message) { + let capitalisedShoutOut = message.toUpperCase(); + return capitalisedShoutOut; +} +function shoutOut(theMentors){ + for (i = 0; i < theMentors.length; i++){ + let shoutOut = 'Hello ' + theMentors[i]; + let capitalisedShoutOut = capitalisedString(shoutOut); + console.log(capitalisedShoutOut) + } +} +let theMentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; +shoutOut(theMentors); +// Once you specify the array of names, you can use the function (line 22) \ No newline at end of file diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..f1390f82f 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,15 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +// This function multiplies the value of a GBP to convert it into USD. You could also put it all in one line but I see it clearer this way +function convertToUSD(priceGBP) { + let priceUSD = priceGBP * 1.4; + return priceUSD; + // return "$" + priceUSD.toFixed(2); + // This could be added if you wanted to put the currency symbol and some decimals +} + +console.log(convertToUSD(32)); /* CURRENCY FORMATTING @@ -15,7 +23,14 @@ 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() {} +// Following the same principle as for the previous function, but taking into consideration the transaction fee, which needs to be worked out too +function convertToBRL(priceGBP) { + let foreignFee = priceGBP * 1 / 100; + let priceBRL = (priceGBP - foreignFee) * 5.7; + return priceBRL; +} + +console.log(convertToBRL(30)); /* ======= 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 067dd0f5b..def28bf41 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,42 @@ the final result to the variable goodCode */ -function add() { +// Created the three functions requested on exercise one +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 +const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +// I had helped doing this task, as I wasn't sure what it was asking or how to even write it! +// From looking at this piece of code, I can see that it is really hard to read and it can lead to mistakes + +/* BAD PRACTICE */ + +let badCode = format(multiply(add(startingValue, 10), 2)); +console.log(badCode); /* BETTER PRACTICE */ -let goodCode = +function someMaths(startingValue) { + let added = add(startingValue, 10); + let multiplied = multiply(added, 2); + let formatted = format(multiplied); + return formatted; +} + +let goodCode = someMaths(startingValue); +console.log(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 e32182cfe..3c6f13e68 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,9 +1,8 @@ /** - 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: + There are a few steps to being able to view the future though: * Ask a question * Shake the ball * Get an answer @@ -43,10 +42,22 @@ Very doubtful. */ -// This should log "The ball has shaken!" -// and return the answer. +// First I placed all possibilities within an array + +let possibleAnswers = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitey.", +"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.", +"Dont count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."]; + +// This function logs "The ball has shaken!" and returns the answer. +// I had guidance to make this function, as I was not able to make it by myself + function shakeBall() { - //Write your code in here + console.log('The ball has shaken!') + let max = possibleAnswers.length - 1; + let min = 0; + let randomIndex = Math.floor(Math.random() * (max - min) + min); + return possibleAnswers[randomIndex]; } /* @@ -58,10 +69,30 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ + +// This function checks the index of the array and determines the type of response previously given +// I had some guidance to make this function, as I was not able to make it by myself + function checkAnswer(answer) { - //Write your code in here + let arrayIndex = possibleAnswers.indexOf(answer); + console.log(arrayIndex); + if (arrayIndex >= 0 && arrayIndex <= 4) { + return 'very positive'; + } else if (arrayIndex >= 5 && arrayIndex <= 9) { + return 'positive'; + } else if (arrayIndex >= 10 && arrayIndex <= 14) { + return 'negative'; + } else { + return 'very negative'; + } } +let answerSelection = shakeBall(); +console.log(answerSelection); + +let answerType = checkAnswer(answerSelection); +console.log(answerType); + /* ================================== ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..a170b0440 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,20 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +// Parametres need to be separated by commas +function addNumbers(a, b, c) { return a + b + c; } -function introduceMe(name, age) -return "Hello, my name is " + name "and I am " age + "years old"; +// Block of code needs to be displayed between curly braces +// Missing spaces next to variables within the return element +function introduceMe(name, age){ + return "Hello, my name is " + name + " and I am " + age + " years old"; +} +// Incorrect use of operators (+) both in the variable and the return element 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 3c578ad87..be3b9477a 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,18 @@ // The syntax for this function is valid but it has an error, find it and fix it. +// Missing . before the method to correctly use it function trimWord(word) { - return wordtrim(); + return word.trim(); } +// Variable doesn't need quotation marks and the length method doesn't require empty parenthesis function getWordLength(word) { - return "word".length(); + return word.length; } +// Return element was separated in two different lines 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 c9221a200..cff76a184 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,13 @@ // Add comments to explain what this function does. You're meant to use Google! + +// This 'getNumber' function will return a random number between 0 (inclusive) and 10 (exclusive), which cannot be chosen or reset by the user. function getNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! + +// This 's' function with the .concat() method is used to merge two or more arrays, by returning a new array and not changing the existing arrays. function s(w1, w2) { return w1.concat(w2); } @@ -11,8 +15,16 @@ function s(w1, w2) { 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. + let myFirstWord = firstWord + ' '; + let mySecondWord = secondWord + ' '; + let myThirdWord = thirdWord; + return myFirstWord.concat(mySecondWord, myThirdWord); } +console.log(concatenate('code', 'your', 'future')); +console.log(concatenate('I', 'like', 'pizza')); +console.log(concatenate('I', 'am', 13)); + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..6db9873b5 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -1,11 +1,18 @@ -/* +/* SALES TAX ========= A business requires a program that calculates how much sales tax to charge Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +// This function aims to calculate the tax amount to charge on any given product depending on its price and the 20% given sales tax +function calculateSalesTax(priceOfProduct) { + let salesTax = 20; + let taxAmount = priceOfProduct * salesTax / 100; + return taxAmount; +} + +console.log(calculateSalesTax(80)); /* CURRENCY FORMATTING @@ -17,7 +24,13 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +// This nested function follows up from the previous one; using the taxAmount already worked out to return +function getFormattedTotalPrice(productPrice) { + let totalPrice = productPrice + calculateSalesTax(productPrice); + return "£" + totalPrice.toFixed(2); +} + +console.log(getFormattedTotalPrice(80)); /* =================================================== From df4bda0b07b3e85b097f1e419e044e11254d771a Mon Sep 17 00:00:00 2001 From: Laura TM <67758358+Laura-TM@users.noreply.github.com> Date: Wed, 20 Jan 2021 22:27:18 +0000 Subject: [PATCH 2/4] Amending two typos on my comments --- exercises/F-strings-methods/exercise2.js | 2 +- extra/2-piping.js | 46 ++++++++++++------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index 80028aa63..d882e999c 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,4 +1,4 @@ -// There are two ariables, one to store a name plus the other one to store a message +// There are two variables, one to store a name plus the other one to store a message // The .trim() method was applied to the name to reduce the unnecessary whitespace const name = " Daniel "; diff --git a/extra/2-piping.js b/extra/2-piping.js index def28bf41..ec46dcc7d 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -19,15 +19,15 @@ // Created the three functions requested on exercise one function add(num1, num2) { - return num1 + num2; + return num1 + num2; } function multiply(num1, num2) { - return num1 * num2; + return num1 * num2; } function format(num) { - return '£' + num; + return "£" + num; } const startingValue = 2; @@ -44,10 +44,10 @@ console.log(badCode); /* BETTER PRACTICE */ function someMaths(startingValue) { - let added = add(startingValue, 10); - let multiplied = multiply(added, 2); - let formatted = format(multiplied); - return formatted; + let added = add(startingValue, 10); + let multiplied = multiply(added, 2); + let formatted = format(multiplied); + return formatted; } let goodCode = someMaths(startingValue); @@ -59,22 +59,24 @@ There are some Tests in this file that will help you work out if your code is wo To run these tests type `node 2-piping.js` into your terminal */ -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your code returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your code returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } -test('add function - case 1 works', add(1,3), 4) -test('add function - case 2 works', add(2.4,5), 7.4) -test('multiply function works', multiply(2,3), 6) -test('format function works', format(16), "£16") -test('badCode variable correctly assigned', badCode, "£24") -test('goodCode variable correctly assigned', goodCode, "£24") +test("add function - case 1 works", add(1, 3), 4); +test("add function - case 2 works", add(2.4, 5), 7.4); +test("multiply function works", multiply(2, 3), 6); +test("format function works", format(16), "£16"); +test("badCode variable correctly assigned", badCode, "£24"); +test("goodCode variable correctly assigned", goodCode, "£24"); From ab02039951ba017342b58967f33df392c3899c6c Mon Sep 17 00:00:00 2001 From: Laura TM <67758358+Laura-TM@users.noreply.github.com> Date: Wed, 20 Jan 2021 23:09:37 +0000 Subject: [PATCH 3/4] Actioned format command --- exercises/B-hello-world/exercise.js | 12 +++---- exercises/C-variables/exercise.js | 13 ++++--- exercises/D-strings/exercise.js | 4 +-- exercises/E-strings-concatenation/exercise.js | 12 +++---- exercises/F-strings-methods/exercise.js | 13 ++++--- exercises/F-strings-methods/exercise2.js | 7 +++- exercises/G-numbers/exercise.js | 5 +-- exercises/I-floats/exercise.js | 9 ++--- exercises/J-functions/exercise.js | 3 +- exercises/J-functions/exercise2.js | 3 +- exercises/K-functions-parameters/exercise.js | 3 +- exercises/K-functions-parameters/exercise2.js | 5 ++- exercises/K-functions-parameters/exercise3.js | 5 ++- exercises/K-functions-parameters/exercise4.js | 7 ++-- exercises/K-functions-parameters/exercise5.js | 11 +++--- exercises/L-functions-nested/exercise1C.js | 30 ++++++++-------- exercises/L-functions-nested/exercise2.js | 18 ++++------ extra/1-currency-conversion.js | 4 +-- extra/3-magic-8-ball.js | 36 ++++++++++++++----- mandatory/1-syntax-errors.js | 34 ++++++++++-------- mandatory/3-function-output.js | 10 +++--- mandatory/4-tax.js | 6 ++-- 22 files changed, 136 insertions(+), 114 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index d9a0a7c09..19ff8d2e2 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1,7 +1,7 @@ // Various console.log messages at the same time -console.log('Hello world'); -console.log(5); // Numbers on their own won't return errors as they are a different type of data -console.log('Hello World. I just started learning JavaScript!'); -console.log('Returned to this exercise and changed this message!'); -console.log('Sometimes I spent too long getting bored'); -// Noticed that error messages appear on the terminal when words are not passed as strings (with quotation marks) \ No newline at end of file +console.log("Hello world"); +console.log(5); // Numbers on their own won't return errors as they are a different type of data +console.log("Hello World. I just started learning JavaScript!"); +console.log("Returned to this exercise and changed this message!"); +console.log("Sometimes I spent too long getting bored"); +// Noticed that error messages appear on the terminal when words are not passed as strings (with quotation marks) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index 527249f15..1c4fec058 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,9 +1,8 @@ // Start by creating a variable `greeting` -var greeting = 'Aqui estoy!'; -// console.log(greeting); -// console.log(greeting); -// console.log(greeting); -for(i = 0; i < 3; i++) { - console.log(greeting); +// First tried the basic way: three separate times console.log(greeting); and then made a for-loop to try an alternative + +var greeting = "Aqui estoy!"; + +for (i = 0; i < 3; i++) { + console.log(greeting); } -// First tried the basic way and then made a for-loop to try an alternative \ No newline at end of file diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index e8d91e4cd..de8909a0d 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,5 +1,5 @@ // Start by creating a variable `message` // Then another variable to store the data type and finally log it to the console -var message = 'This is a string data type'; +var message = "This is a string data type"; var messageType = typeof message; -console.log(messageType); \ No newline at end of file +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index efc7f2582..4a8fdfa0b 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -3,10 +3,10 @@ // I tried this task first with variables in an individual form, but then decided to put them all in a function function getGreeting(name) { - var greeting = 'Hola'; - var courtesy = ', ¿cómo estás hoy?'; - var message = greeting + ' ' + name + courtesy; - return message; + var greeting = "Hola"; + var courtesy = ", ¿cómo estás hoy?"; + var message = greeting + " " + name + courtesy; + return message; } -var name = 'Laurita'; -console.log(getGreeting(name)); \ No newline at end of file +var name = "Laurita"; +console.log(getGreeting(name)); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 1f469279f..5d6a5bf41 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -6,8 +6,13 @@ // var myGreeting = 'My name is ' + myName + ' and my name is ' + myName.length + ' characters long'; function getGreeting(name) { - var myGreeting = 'My name is ' + name + ' and my name is ' + name.length + ' characters long'; - return myGreeting; + var myGreeting = + "My name is " + + name + + " and my name is " + + name.length + + " characters long"; + return myGreeting; } -var name = 'Laurita' -console.log(getGreeting(name)); \ No newline at end of file +var name = "Laurita"; +console.log(getGreeting(name)); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index d882e999c..82537f9b6 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -3,6 +3,11 @@ const name = " Daniel "; -const message = ' My name is ' + name.trim() + ' and my name is ' + name.trim().length + ' characters long'; +const message = + " My name is " + + name.trim() + + " and my name is " + + name.trim().length + + " characters long"; console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index d8927765a..01359cfc6 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -5,6 +5,7 @@ let numberOfStudents = 30; let numberOfMentors = 10; -let numberOfAdults = 'The total amount of adults is ' + (numberOfStudents + numberOfMentors); +let numberOfAdults = + "The total amount of adults is " + (numberOfStudents + numberOfMentors); -console.log(numberOfAdults); \ No newline at end of file +console.log(numberOfAdults); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index 809f954f0..5c6391ed4 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,4 +1,3 @@ - var numberOfStudents = 15; var numberOfMentors = 8; @@ -9,8 +8,10 @@ var numberOfAdults = numberOfMentors + numberOfStudents; var percentageStudents = (numberOfStudents * 100) / numberOfAdults; var percentageMentors = (numberOfMentors * 100) / numberOfAdults; -var totalStudents = 'Percentage of students: ' + Math.round(percentageStudents) + '%'; -var totalMentors = 'Percentage of mentors: ' + Math.round(percentageMentors) + '%'; +var totalStudents = + "Percentage of students: " + Math.round(percentageStudents) + "%"; +var totalMentors = + "Percentage of mentors: " + Math.round(percentageMentors) + "%"; console.log(totalStudents); -console.log(totalMentors); \ No newline at end of file +console.log(totalMentors); diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 9258e644b..6ecad6115 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,4 +1,3 @@ - function halve(number) { // Complete the function here let half = number / 2; @@ -11,4 +10,4 @@ var result = halve(12); console.log(result); console.log(halve(4)); -console.log(halve(1458690980)); \ No newline at end of file +console.log(halve(1458690980)); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 0c885a094..fada0888f 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,4 +1,3 @@ - function triple(number) { // Completed function here to trebble a number and called it several times let trebbledNum = number * 3; @@ -9,4 +8,4 @@ var result = triple(12); console.log(result); console.log(triple(39)); -console.log(triple(1)); \ No newline at end of file +console.log(triple(1)); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index f2a57da44..a73506d8f 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,4 +1,3 @@ - // Complete the function so that it takes input parameters function multiply(num1, num2) { // Calculate the result of the function and return it @@ -9,4 +8,4 @@ function multiply(num1, num2) { var result = multiply(3, 4); // Call the variable to show the value returned with the above function -console.log(result); \ No newline at end of file +console.log(result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index ae9d3ec9e..1e1b2fb98 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,8 +1,7 @@ - // Declare your function first function divide(num1, num2) { - // Write the block of code inside the curly braces - return num1 / num2; + // Write the block of code inside the curly braces + return num1 / num2; } // Store the value of this function on a variable diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index ed93dbde1..ccbc338d4 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,8 +1,7 @@ - // Write your function here with the block of code inside the curly braces function createGreeting(name) { - let myGreeting = 'Hello, my name is ' + name; - return myGreeting; + let myGreeting = "Hello, my name is " + name; + return myGreeting; } // Store the value in a variable diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index b127c8fec..6245a24aa 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,9 +1,8 @@ - // Declare your function first function mySum(num1, num2) { - let sum = num1 + num2; - return sum; + let sum = num1 + num2; + return sum; } // Call the function and assign to a variable `sum` let totalSum = mySum(13, 124); -console.log(totalSum); \ No newline at end of file +console.log(totalSum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7460eb2ae..e28553af8 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,14 +1,13 @@ - // Declare your function here function myGreeting(name, age) { - let myName = 'Hello, my name is ' + name; - let myAge = ' and I am ' + age + ' years old.'; - let fullGreeting = myName + myAge; - return fullGreeting; + let myName = "Hello, my name is " + name; + let myAge = " and I am " + age + " years old."; + let fullGreeting = myName + myAge; + return fullGreeting; } // Store the value in a variable const greeting = myGreeting("Daniel", 30); // Call the variable -console.log(greeting); \ No newline at end of file +console.log(greeting); diff --git a/exercises/L-functions-nested/exercise1C.js b/exercises/L-functions-nested/exercise1C.js index f2760e07a..e0f7325b7 100644 --- a/exercises/L-functions-nested/exercise1C.js +++ b/exercises/L-functions-nested/exercise1C.js @@ -1,18 +1,18 @@ // Option C -// Created a function with several variables that when called would return the expected output +// Created a function with several variables that when called would return the expected output function doPercentage(mentors, students) { - let allStudents = students; - let allMentors = mentors; - let allAdults = allStudents + allMentors; - let percentageStudents = (allStudents * 100) / allAdults; - let percentageMentors = (allMentors * 100) / allAdults; - let allPercentagesS = - "Percentage of students: " + Math.round(percentageStudents) + "%."; - let allPercentagesM = - "Percentage of mentors: " + Math.round(percentageMentors) + "%."; - let allPercentages = allPercentagesS + allPercentagesM; - return allPercentages; - } - - console.log(doPercentage(16, 30)); \ No newline at end of file + let allStudents = students; + let allMentors = mentors; + let allAdults = allStudents + allMentors; + let percentageStudents = (allStudents * 100) / allAdults; + let percentageMentors = (allMentors * 100) / allAdults; + let allPercentagesS = + "Percentage of students: " + Math.round(percentageStudents) + "%."; + let allPercentagesM = + "Percentage of mentors: " + Math.round(percentageMentors) + "%."; + let allPercentages = allPercentagesS + allPercentagesM; + return allPercentages; +} + +console.log(doPercentage(16, 30)); diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js index ac1be200a..00bcf8493 100644 --- a/exercises/L-functions-nested/exercise2.js +++ b/exercises/L-functions-nested/exercise2.js @@ -1,23 +1,17 @@ -// I got rid of this list of variables and made it into an array below - -// var mentor1 = "Daniel"; -// var mentor2 = "Irina"; -// var mentor3 = "Mimi"; -// var mentor4 = "Rob"; -// var mentor5 = "Yohannes"; +// I got rid of the original list of variables and made it into an array below // Created a nested function, the first one turns letters upper case and the second one displays the greeting to the mentors function capitalisedString(message) { let capitalisedShoutOut = message.toUpperCase(); return capitalisedShoutOut; } -function shoutOut(theMentors){ - for (i = 0; i < theMentors.length; i++){ - let shoutOut = 'Hello ' + theMentors[i]; +function shoutOut(theMentors) { + for (i = 0; i < theMentors.length; i++) { + let shoutOut = "Hello " + theMentors[i]; let capitalisedShoutOut = capitalisedString(shoutOut); - console.log(capitalisedShoutOut) + console.log(capitalisedShoutOut); } } let theMentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; shoutOut(theMentors); -// Once you specify the array of names, you can use the function (line 22) \ No newline at end of file +// Once you specify the array of names, you can use the function (line 22) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index f1390f82f..0e718c4c0 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -9,7 +9,7 @@ function convertToUSD(priceGBP) { let priceUSD = priceGBP * 1.4; return priceUSD; - // return "$" + priceUSD.toFixed(2); + // return "$" + priceUSD.toFixed(2); // This could be added if you wanted to put the currency symbol and some decimals } @@ -25,7 +25,7 @@ console.log(convertToUSD(32)); // Following the same principle as for the previous function, but taking into consideration the transaction fee, which needs to be worked out too function convertToBRL(priceGBP) { - let foreignFee = priceGBP * 1 / 100; + let foreignFee = (priceGBP * 1) / 100; let priceBRL = (priceGBP - foreignFee) * 5.7; return priceBRL; } diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 3c6f13e68..84edaebc4 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -44,16 +44,34 @@ // First I placed all possibilities within an array -let possibleAnswers = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitey.", -"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.", -"Dont count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."]; +let possibleAnswers = [ + "It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes - definitey.", + "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.", + "Dont count on it.", + "My reply is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.", +]; // This function logs "The ball has shaken!" and returns the answer. // I had guidance to make this function, as I was not able to make it by myself function shakeBall() { - console.log('The ball has shaken!') + console.log("The ball has shaken!"); let max = possibleAnswers.length - 1; let min = 0; let randomIndex = Math.floor(Math.random() * (max - min) + min); @@ -77,13 +95,13 @@ function checkAnswer(answer) { let arrayIndex = possibleAnswers.indexOf(answer); console.log(arrayIndex); if (arrayIndex >= 0 && arrayIndex <= 4) { - return 'very positive'; + return "very positive"; } else if (arrayIndex >= 5 && arrayIndex <= 9) { - return 'positive'; + return "positive"; } else if (arrayIndex >= 10 && arrayIndex <= 14) { - return 'negative'; + return "negative"; } else { - return 'very negative'; + return "very negative"; } } diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a170b0440..719e62219 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -2,19 +2,19 @@ // Parametres need to be separated by commas function addNumbers(a, b, c) { - return a + b + c; + return a + b + c; } // Block of code needs to be displayed between curly braces // Missing spaces next to variables within the return element -function introduceMe(name, age){ +function introduceMe(name, age) { return "Hello, my name is " + name + " and I am " + age + " years old"; } // Incorrect use of operators (+) both in the variable and the return element function getTotal(a, b) { - total = a + b; - return "The total is " + total; + total = a + b; + return "The total is " + total; } /* @@ -28,19 +28,25 @@ To run these tests type `node 1-syntax-errors.js` into your terminal =================================================== */ -const util = require('util'); +const util = require("util"); function test(test_name, actual, expected) { - let status; - if (actual === expected) { - status = "PASSED"; - } else { - status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; - } - - console.log(`${test_name}: ${status}`); + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect( + expected + )} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); } test("fixed addNumbers function - case 1", addNumbers(3, 4, 6), 13); -test("fixed introduceMe function", introduceMe("Sonjide", 27), "Hello, my name is Sonjide and I am 27 years old"); +test( + "fixed introduceMe function", + introduceMe("Sonjide", 27), + "Hello, my name is Sonjide and I am 27 years old" +); test("fixed getTotal function", getTotal(23, 5), "The total is 28"); diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index cff76a184..5867be344 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -15,15 +15,15 @@ function s(w1, w2) { 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. - let myFirstWord = firstWord + ' '; - let mySecondWord = secondWord + ' '; + let myFirstWord = firstWord + " "; + let mySecondWord = secondWord + " "; let myThirdWord = thirdWord; return myFirstWord.concat(mySecondWord, myThirdWord); } -console.log(concatenate('code', 'your', 'future')); -console.log(concatenate('I', 'like', 'pizza')); -console.log(concatenate('I', 'am', 13)); +console.log(concatenate("code", "your", "future")); +console.log(concatenate("I", "like", "pizza")); +console.log(concatenate("I", "am", 13)); /* =================================================== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 6db9873b5..69347533f 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -8,10 +8,10 @@ // This function aims to calculate the tax amount to charge on any given product depending on its price and the 20% given sales tax function calculateSalesTax(priceOfProduct) { let salesTax = 20; - let taxAmount = priceOfProduct * salesTax / 100; + let taxAmount = (priceOfProduct * salesTax) / 100; return taxAmount; } - + console.log(calculateSalesTax(80)); /* @@ -24,7 +24,7 @@ console.log(calculateSalesTax(80)); Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -// This nested function follows up from the previous one; using the taxAmount already worked out to return +// This nested function follows up from the previous one; using the taxAmount already worked out to return function getFormattedTotalPrice(productPrice) { let totalPrice = productPrice + calculateSalesTax(productPrice); return "£" + totalPrice.toFixed(2); From 147dc59497752519a862f15e982697bcab87a043 Mon Sep 17 00:00:00 2001 From: Laura TM <67758358+Laura-TM@users.noreply.github.com> Date: Wed, 3 Feb 2021 21:27:37 +0000 Subject: [PATCH 4/4] changes reviewed --- mandatory/3-function-output.js | 17 +++++++++++------ mandatory/4-tax.js | 16 ++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5867be344..c009e933e 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -7,20 +7,25 @@ function getNumber() { // Add comments to explain what this function does. You're meant to use Google! -// This 's' function with the .concat() method is used to merge two or more arrays, by returning a new array and not changing the existing arrays. +// This 's' function with the .concat() method is used here to join two arrays, returning a new array and not changing the existing arrays. function s(w1, w2) { return w1.concat(w2); } -function concatenate(firstWord, secondWord, thirdWord) { +function concatenate(theFirstWord, theSecondWord, theThirdWord) { // 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. - let myFirstWord = firstWord + " "; - let mySecondWord = secondWord + " "; - let myThirdWord = thirdWord; - return myFirstWord.concat(mySecondWord, myThirdWord); + return theFirstWord + " " + theSecondWord + " " + theThirdWord; } +// Below is the longer option I originally had before feedback was given by tutor + +// function concatenate(firstWord, secondWord, thirdWord) { +// let myFirstWord = firstWord + " "; +// let mySecondWord = secondWord + " "; +// return myFirstWord.concat(mySecondWord, thirdWord); +// } + console.log(concatenate("code", "your", "future")); console.log(concatenate("I", "like", "pizza")); console.log(concatenate("I", "am", 13)); diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 69347533f..60efc1581 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,11 +5,11 @@ Sales tax is 20% of the price of the product */ -// This function aims to calculate the tax amount to charge on any given product depending on its price and the 20% given sales tax function calculateSalesTax(priceOfProduct) { let salesTax = 20; - let taxAmount = (priceOfProduct * salesTax) / 100; - return taxAmount; + let taxAmount = priceOfProduct * (salesTax / 100); + // math round used to fix rounding errors + return Math.round(taxAmount * 100) / 100; } console.log(calculateSalesTax(80)); @@ -25,12 +25,12 @@ console.log(calculateSalesTax(80)); */ // This nested function follows up from the previous one; using the taxAmount already worked out to return -function getFormattedTotalPrice(productPrice) { +function addTaxAndFormatCurrency(productPrice) { let totalPrice = productPrice + calculateSalesTax(productPrice); return "£" + totalPrice.toFixed(2); } -console.log(getFormattedTotalPrice(80)); +console.log(addTaxAndFormatCurrency(80)); /* =================================================== @@ -58,9 +58,9 @@ function test(test_name, actual, expected) { console.log(`${test_name}: ${status}`); } -test("calculateSalesTax function - case 1 works", calculateSalesTax(15), 18); -test("calculateSalesTax function - case 2 works", calculateSalesTax(17.5), 21); -test("calculateSalesTax function - case 3 works", calculateSalesTax(34), 40.8); +test("calculateSalesTax function - case 1 works", calculateSalesTax(15), 3); +test("calculateSalesTax function - case 2 works", calculateSalesTax(17.5), 3.5); +test("calculateSalesTax function - case 3 works", calculateSalesTax(34), 6.8); test( "addTaxAndFormatCurrency function - case 1 works",