From 6f0cafedb7c62133b448c8213c73860a62423291 Mon Sep 17 00:00:00 2001 From: mikinet Date: Sun, 6 Dec 2020 23:27:34 +0000 Subject: [PATCH 1/4] Exercises performed (added or modified codes). --- exercises/B-hello-world/exercise.js | 3 + exercises/C-variables/exercise.js | 4 +- exercises/D-strings/exercise.js | 4 +- exercises/E-strings-concatenation/exercise.js | 6 +- exercises/F-strings-methods/exercise.js | 4 +- exercises/F-strings-methods/exercise2.js | 4 +- exercises/G-numbers/exercise.js | 7 ++ exercises/I-floats/exercise.js | 8 +++ exercises/J-functions/exercise.js | 8 +++ exercises/J-functions/exercise2.js | 6 ++ exercises/K-functions-parameters/exercise.js | 3 +- exercises/K-functions-parameters/exercise2.js | 4 +- exercises/K-functions-parameters/exercise3.js | 4 +- exercises/K-functions-parameters/exercise4.js | 6 +- exercises/K-functions-parameters/exercise5.js | 4 +- extra/1-currency-conversion.js | 12 +++- extra/2-piping.js | 18 ++--- extra/3-magic-8-ball.js | 66 +++++++++++++++++++ mandatory/1-syntax-errors.js | 11 ++-- mandatory/2-logic-error.js | 10 +-- mandatory/3-function-output.js | 6 +- mandatory/4-tax.js | 10 ++- 22 files changed, 172 insertions(+), 36 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..be87f0d6f 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,4 @@ console.log("Hello world"); +console.log("Hello world. I just started learning JS!") +console.log("This is the first exercise in the module.") +console.log("The module takes 3 weeks.") \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..7beea3261 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var greeting = "Hello world"; +console.log(greeting); +console.log(greeting); console.log(greeting); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..9a78510f5 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +var message = "This is a string"; +var messageType = typeof message; console.log(message); +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..3e06e9327 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - -console.log(message); +var greetingStart = "Hi, my name is "; +var name = "Michael"; +var greeting = greetingStart + name; +console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..6c778d099 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +var name = "Michael"; +var nameLength = name.length; +var message = "My name is " + name + " and my name is " + nameLength +" characters long."; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..38dd783bb 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,5 @@ const name = " Daniel "; - +var trimmedName = name.trim(); +var nameLength = name.length; +var message="My name is " + trimmedName + " and my name is " + nameLength + " characters long." console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..6c098af33 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,8 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +var numberOfStudents = 15; +var numberOfMentors = 6; +var numberOfParticipants = numberOfStudents + numberOfMentors; +// Output +console.log("Number of students: " + numberOfStudents); +console.log("Number of mentors: " + numberOfMentors); +console.log("Total number of students and mentors: " + numberOfParticipants); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..26dc4a7ff 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,10 @@ +// Declare and initialise variables var numberOfStudents = 15; var numberOfMentors = 8; +var numberOfParticipants = numberOfStudents + numberOfMentors; +var percentageOfStudents = Math.round(numberOfStudents / numberOfParticipants * 100); +var percentageOfMentors = Math.round(numberOfMentors / numberOfParticipants * 100); +// Output +console.log("Percentage of students: " + percentageOfStudents + "%"); +console.log("Percentage of mentors: " + percentageOfMentors + "%"); + diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..ea8cacb47 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,15 @@ function halve(number) { // complete the function here + return number / 2; } var result = halve(12); +console.log(result); + +result = halve(23); +console.log(result); +result = halve(56); console.log(result); + + diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..606ed7533 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,13 @@ function triple(number) { // complete function here + return number * 3; } var result = triple(12); +console.log(result); + +result = triple(23); +console.log(result); +result = triple(56); console.log(result); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..8a48c7959 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { // Calculate the result of the function and return it + return a * b; } // Assign the result of calling the function the variable `result` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..6839e8684 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,7 @@ // Declare your function first - +function divide(a, b) { + return a / b; +} var result = divide(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..dabc803a2 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,7 @@ // Write your function here - +function createGreeting(name) { + return "Hello, my name is " + name; +} var greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..a224196b4 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function add(a, b) { + return a + b; +} // Call the function and assign to a variable `sum` - +var sum = add(13, 124); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..fa64315fb 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,7 @@ // Declare your function here - +function createLongGreeting(name, age) { + return "Hello, my name is " + name + " and I'm " + age + " years old"; +} const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 70a2fe863..f04fd3050 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,9 @@ 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 FORMATTING @@ -15,7 +17,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(price) { + const exchangeRate = 5.7; + const serviceCharge = 0.01 * price; + let actualPrice = price - serviceCharge; + let priceInBrl = actualPrice * exchangeRate; + return priceInBrl; +} /* ======= 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..063f5cc7d 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,28 @@ 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(num) { + return "£" + num; } const startingValue = 2 // Why can this code be seen as bad practice? Comment your answer. -let badCode = +let badCode = format(multiply(2, add(10, startingValue))); /* BETTER PRACTICE */ -let goodCode = +let goodCode = add(10, startingValue); +goodCode = multiply(goodCode, 2); +goodCode = format(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..2e0c9face 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -47,6 +47,34 @@ // and return the answer. function shakeBall() { //Write your code in here + console.log("The ball has shaken!"); + + 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." + ]; + // Select a random answer from list + let index = Math.floor(Math.random() * answers.length); + let randomAnswer = answers[index]; + return randomAnswer; } /* @@ -60,6 +88,44 @@ function shakeBall() { */ function checkAnswer(answer) { //Write your code in here + let answers = [ + "very positive", + "positive", + "negative", + "very negative" + ]; + + switch (answer) { + case "It is certain.": + case "It is decidedly so.": + case "Without a doubt.": + case "Yes - definitely.": + case "You may rely on it.": + answer = answers[0]; + break; + case "As I see it, yes.": + case "Most likely.": + case "Outlook good.": + case "Yes.": + case "Signs point to yes.": + answer = answers[1]; + break; + case "Reply hazy, try again.": + case "Ask again later.": + case "Better not tell you now.": + case "Cannot predict now.": + case "Concentrate and ask again.": + answer = answers[2]; + break; + case "Don't count on it.": + case "My reply is no.": + case "My sources say no.": + case "Outlook not so good.": + case "Very doubtful.": + answer = answers[3]; + break; + } + return answer; } /* diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index 0a21afd1b..6f498d235 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,17 @@ // 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 " + name "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 3c578ad87..d17aed27e 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,18 +1,18 @@ // The syntax for this function is valid but it has an error, find it and fix it. function trimWord(word) { - return wordtrim(); + return word.trim(); } function getWordLength(word) { - return "word".length(); + return word.length; } -function multiply(a, b, c) { - a * b * c; - return; +function multiply(a, b, c) { + return a * b * c; } +const { stringify } = require("querystring"); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index c9221a200..2939f188a 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,16 +1,18 @@ -// Add comments to explain what this function does. You're meant to use Google! +// Return a random number multiplied by 10 function getNumber() { return Math.random() * 10; } -// Add comments to explain what this function does. You're meant to use Google! +// Affix one string value to the end of a second string value and return the merge function s(w1, w2) { + return w1.concat(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. + return firstWord.concat(" ", secondWord, " ", thirdWord); } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index c9e41c691..38076d703 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,7 +5,9 @@ Sales tax is 20% of the price of the product */ -function calculateSalesTax() {} +function calculateSalesTax(price) { + return price / 5; +} /* CURRENCY FORMATTING @@ -17,8 +19,12 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price,tax) { + let actualPrice = price + tax; + return "£" + actualPrice.toFixed(2); +} +const { format } = require("path"); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== From 22fc713b3627def3b8e0343978c0e8d74c5e78ae Mon Sep 17 00:00:00 2001 From: mikinet Date: Tue, 8 Dec 2020 11:16:47 +0000 Subject: [PATCH 2/4] Updated folder- created file; dded codes. Renamed 'exercise.js' as 'exercise2.js'; created new file 'exericse.js'. Added several lines of code that complete the exercises. --- exercises/L-functions-nested/exercise.js | 30 +++++++++++++++++++---- exercises/L-functions-nested/exercise2.js | 23 +++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 exercises/L-functions-nested/exercise2.js diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..7dc8249c2 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,25 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +// Declare and initialise global variables +var numberOfStudents = 15; +var numberOfMentors = 8; +var numberOfParticipants = numberOfStudents + numberOfMentors; +var result; // percentage calculation result +var message; // message to be displayed + +// This function calculates percentages +function calculatePercentage(a, b) { + return Math.round(a / b * 100); // approximate value +} + +// Function to display the result of percentage calculation +function displayResult(val, desc) { // val = percentage value, desc = what the percentage is for + var message = "Percentage of ".concat(desc+": ", val + "%"); + console.log(message); +} + +// About students +result = calculatePercentage(numberOfStudents, numberOfParticipants); +message = displayResult(result, "students"); + +// About mentors +result = calculatePercentage(numberOfMentors, numberOfParticipants); +message = displayResult(result, "mentors"); diff --git a/exercises/L-functions-nested/exercise2.js b/exercises/L-functions-nested/exercise2.js new file mode 100644 index 000000000..6c0139b8d --- /dev/null +++ b/exercises/L-functions-nested/exercise2.js @@ -0,0 +1,23 @@ +var mentor1 = "Daniel"; +var mentor2 = "Irina"; +var mentor3 = "Mimi"; +var mentor4 = "Rob"; +var mentor5 = "Yohannes"; + +// Function to change style of text to uppercase +function changeToUpperCase(text) { + return text.toUpperCase(); +} + +// Function to display greeting message, given name of a person to greet +function greetSomeone(name) { + var greeting = "Hello " + name; + var message = changeToUpperCase(greeting); + console.log(message); +} + +greetSomeone(mentor1); +greetSomeone(mentor2); +greetSomeone(mentor3); +greetSomeone(mentor4); +greetSomeone(mentor5); \ No newline at end of file From aef2066c4158f14ae35f89a60c4710bc960a0999 Mon Sep 17 00:00:00 2001 From: mikinet Date: Wed, 9 Dec 2020 15:14:22 +0000 Subject: [PATCH 3/4] Added comments. Modified function (lines 22, 23). --- mandatory/4-tax.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index 38076d703..a452bcbee 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,8 +5,13 @@ Sales tax is 20% of the price of the product */ +/***** Quick Note ******* + * The function below fails the tests. However, it meets the requirements (lines 4 and 5) + * and has no logic errors (I hope). + * I hope the suggestion I made (starts at line 32) can explain why. */ + function calculateSalesTax(price) { - return price / 5; + return price / 5; // tax = 20% of price } /* @@ -19,11 +24,22 @@ function calculateSalesTax(price) { Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency(price,tax) { +function addTaxAndFormatCurrency(price) { + let tax = calculateSalesTax(price); let actualPrice = price + tax; return "£" + actualPrice.toFixed(2); } +/* + ========================= THIS IS A SUGGESTION ============================ + * I suggest lines 57 through 59 be corrected for values: calculateSalesTax() should + * return the tax (i.e. 20% of product price, as stipulated in lines 4 and 5 above), + * not the value of price + tax. + * + * It's possible to make calculateSalesTax() calculate the total price to pass the tests, + * but that does seem counterintuitive to what we expect the function does. + ============================================================================ */ + const { format } = require("path"); /* =================================================== From 805ab25000e8499c7cc3b82a412fdf15d2c4d600 Mon Sep 17 00:00:00 2001 From: mikinet Date: Wed, 9 Dec 2020 16:22:42 +0000 Subject: [PATCH 4/4] Corrected figures and edited text in comment lines --- mandatory/4-tax.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index a452bcbee..ca0d4c3a4 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -8,7 +8,7 @@ /***** Quick Note ******* * The function below fails the tests. However, it meets the requirements (lines 4 and 5) * and has no logic errors (I hope). - * I hope the suggestion I made (starts at line 32) can explain why. */ + * I hope the suggestion I made (starts at line 35) can explain why. */ function calculateSalesTax(price) { return price / 5; // tax = 20% of price @@ -32,9 +32,9 @@ function addTaxAndFormatCurrency(price) { /* ========================= THIS IS A SUGGESTION ============================ - * I suggest lines 57 through 59 be corrected for values: calculateSalesTax() should - * return the tax (i.e. 20% of product price, as stipulated in lines 4 and 5 above), - * not the value of price + tax. + * I suggest calls made to the function calculateSalesTax (lines 70 through 72) be + * edited for values: calculateSalesTax() should return the tax (i.e. 20% of product + * price, as stipulated in lines 4 and 5 above), not the value of price + tax. * * It's possible to make calculateSalesTax() calculate the total price to pass the tests, * but that does seem counterintuitive to what we expect the function does.