From aab1435ec23b5516036be7982ad46ee0fb4535f4 Mon Sep 17 00:00:00 2001 From: TemwaNyirenda Date: Fri, 1 Jul 2022 13:45:20 +0200 Subject: [PATCH 1/3] Completed Exercises --- exercises/B-hello-world/exercise.js | 3 +++ exercises/C-variables/exercise.js | 4 +++- exercises/D-strings/exercise.js | 5 ++++- exercises/E-strings-concatenation/exercise.js | 4 +++- exercises/F-strings-methods/exercise.js | 3 ++- exercises/F-strings-methods/exercise2.js | 2 +- exercises/G-numbers/exercise.js | 5 +++++ exercises/I-floats/exercise.js | 7 +++++++ exercises/J-functions/exercise.js | 15 +++++++++++++- exercises/J-functions/exercise2.js | 2 +- exercises/K-functions-parameters/exercise.js | 4 ++-- exercises/K-functions-parameters/exercise2.js | 4 +++- exercises/K-functions-parameters/exercise3.js | 4 +++- exercises/K-functions-parameters/exercise4.js | 5 ++++- exercises/K-functions-parameters/exercise5.js | 4 +++- exercises/L-functions-nested/exercise.js | 20 +++++++++++++++++++ 16 files changed, 78 insertions(+), 13 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..b8bd1ce46 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,4 @@ console.log("Hello world"); +console.log("What's your name?"); +console.log("Mine is Temwa."); +console.log(4); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..482a9bab3 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +let greeting = "Good morning to ya!"; +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..edc245d49 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` - +let message = "Whazzup?"; console.log(message); + +let messageType = typeof(message); +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..eaee624fc 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` - +let greeting = "Top of the morning to ya"; +let yourName = "CYFer"; +let message = greeting + ", " + yourName; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..347ba344f 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - +let myName = "Temwa"; +let message = `Hello, my name is ${myName} and fun fact, my name is ${myName.length} characters long.`; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..6916212b2 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,3 @@ const name = " Daniel "; - +let 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 49e7bc00b..aa31d5934 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,6 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 40; +let numberOfMentors = 7; +let totalNumber = numberOfStudents + numberOfMentors; + +console.log(totalNumber); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..ac0e780d1 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,9 @@ var numberOfStudents = 15; var numberOfMentors = 8; +let total = numberOfStudents + numberOfMentors; + +let percentageStudents = (numberOfStudents / total) * 100; +let percentageMentors = (numberOfMentors / total) * 100; + +console.log(`Percentage of students: ${Math.round(percentageStudents)}%`); +console.log(`Percentage of mentors: ${Math.round(percentageMentors)}%`); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..e0555f8a2 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,20 @@ function halve(number) { - // complete the function here + return (number / 2);// complete the function here } var result = halve(12); console.log(result); + +var result = halve(101); + +console.log(result); + +var result = halve(25.5); + +console.log(result); + +var result = halve(1); + +console.log(result); + \ No newline at end of file diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..1b7361545 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,5 @@ function triple(number) { - // complete function here + return (number * 3);// complete function here } var result = triple(12); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..498cfbaf1 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,6 @@ // Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it +function multiply(num1, num2) { + return (num1 * num2);// Calculate the result of the function and return it } // 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..c4bfa0a90 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(num1, num2){ + return (num1 / num2); +} 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..3e1e73632 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(insertName) { + return (`Welcome to CYF, ${insertName}.`); +} 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..d4f3109f9 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,8 @@ // Declare your function first - +function add(num1, num2) { + return (num1 + num2); +} // Call the function and assign to a variable `sum` +let 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..fe25443a6 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(insertName, insertAge) { + return (`Woah, ${insertName}, I can't believe you're only ${insertAge} years old!`); +} const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..d299f1e47 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,23 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +function createAShoutyMessage(message) { + return (message.toUpperCase()); +} + +function shoutName(name) { + return (name.toUpperCase()); +} + +greeting = "Finally you're here! Come on in, "; + +function createAShoutyMessageWithName(message, name) { + return (createAShoutyMessage(message) + shoutName(name)); +} + +console.log(createAShoutyMessageWithName(greeting, mentor1)); +console.log(createAShoutyMessageWithName(greeting, mentor2)); +console.log(createAShoutyMessageWithName(greeting, mentor3)); +console.log(createAShoutyMessageWithName(greeting, mentor4)); +console.log(createAShoutyMessageWithName(greeting, mentor5)); \ No newline at end of file From 8cd09abf06c71bab422437a60137a3eb6d5bce3e Mon Sep 17 00:00:00 2001 From: TemwaNyirenda Date: Fri, 1 Jul 2022 14:01:00 +0200 Subject: [PATCH 2/3] Completed mandatory --- mandatory/1-syntax-errors.js | 11 ++++++----- mandatory/2-logic-error.js | 7 +++---- mandatory/3-function-output.js | 10 ++++++++-- mandatory/4-tax.js | 10 ++++++++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..98360ac6b 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 9cca7603b..37c4ebc15 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,15 @@ // 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 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..2d38806cd 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,16 +1,22 @@ // Add comments to explain what this function does. You're meant to use Google! -function getRandomNumber() { +function getRandomNumber() { // this funtion gets a random number from 0 to 9 return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! -function combine2Words(word1, word2) { +function combine2Words(word1, word2) { // this function concatenates two words (it joins word2 to the end of word1 - same as using the + sign) return word1.concat(word2); } 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 sentence = firstWord.concat(" "); + sentence = sentence.concat(secondWord); + sentence = sentence.concat(" "); + sentence = sentence.concat(thirdWord); + + return sentence; } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..be22fd3de 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(priceOfProduct) { + return priceOfProduct * 1.2 +} /* CURRENCY FORMATTING @@ -17,7 +19,11 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(priceOfProduct) { + let totalPrice = calculateSalesTax(priceOfProduct); + + return `£${totalPrice.toFixed(2)}`; +} /* =================================================== From 7379431058486f54dd0b59c3f36b4774dc186060 Mon Sep 17 00:00:00 2001 From: TemwaNyirenda Date: Fri, 1 Jul 2022 15:59:22 +0200 Subject: [PATCH 3/3] Completed extra except 3 (missing 1 out of 4) --- extra/1-currency-conversion.js | 17 ++++- extra/2-piping.js | 17 +++-- extra/3-magic-8-ball.js | 116 +++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 9 deletions(-) diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..7b625d2e6 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(priceInPounds) { + return priceInPounds * 1.4; +} /* CURRENCY CONVERSION @@ -15,7 +17,18 @@ 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(priceInPounds) { + let priceMinusFee = 0.99 * priceInPounds; + let priceInBRL = priceMinusFee * 5.7; + if ((priceInBRL - Math.floor(priceInBRL)) > 0) + { + priceInBRL *= 100; + priceInBRL = Math.round(priceInBRL); + priceInBRL /= 100; + } + + 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 b4f8c4c1b..fd6fddcda 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(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 number = add(startingValue, 10); +number = multiply(number, 2); -let goodCode = +let goodCode = format(number); /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..aba669ead 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -46,6 +46,95 @@ // This should log "The ball has shaken!" // and return the answer. function shakeBall() { + let randomNumber = Math.floor(Math.random() * 20); + // console.log (randomNumber); + + if (randomNumber == 0) + { + answer = "It is certain."; + } + else if (randomNumber == 1) + { + answer = "It is decidedly so."; + } + else if (randomNumber == 2) + { + answer = "Without a doubt."; + } + else if (randomNumber == 3) + { + answer = "Yes - definitely."; + } + else if (randomNumber == 4) + { + answer = "You may rely on it."; + } + else if (randomNumber == 5) + { + answer = "As I see it, yes."; + } + else if (randomNumber == 6) + { + answer = "Most likely."; + } + else if (randomNumber == 7) + { + answer = "Outlook good."; + } + else if (randomNumber == 8) + { + answer = "Yes."; + } + else if (randomNumber == 9) + { + answer = "Signs point to yes."; + } + else if (randomNumber == 10) + { + answer = "Reply hazy, try again."; + } + else if (randomNumber == 11) + { + answer = "Ask again later."; + } + else if (randomNumber == 12) + { + answer = "Better not tell you now."; + } + else if (randomNumber == 13) + { + answer = "Cannot predict now."; + } + else if (randomNumber == 14) + { + answer = "Concentrate and ask again."; + } + else if (randomNumber == 15) + { + answer = "Don't count on it."; + } + else if (randomNumber == 16) + { + answer = "My reply is no."; + } + else if (randomNumber == 17) + { + answer = "My sources say no."; + } + else if (randomNumber == 18) + { + answer = "Outlook not so good."; + } + else + { + answer = "Very doubtful."; + } + + console.log("The ball has shaken!"); + // console.log(answer); + // console.log(checkAnswer(answer)); + // checkAnswer(answer); + return answer; //Write your code in here } @@ -59,8 +148,34 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ function checkAnswer(answer) { + + if (answer == 'It is certain.' || answer == 'It is decidedly so.' || answer == 'Without a doubt.' || answer == 'Yes - definitely.' || answer == 'You may rely on it.') + { + // console.log("very positive"); + level = "very positive"; + } + else if (answer == 'As I see it, yes.' || answer == 'Most likely.' || answer == 'Outlook good.' || answer == 'Yes.' || answer == 'Signs point to yes.') + { + // console.log("positive"); + level = "positive"; + } + else if (answer == 'Reply hazy, try again.' || answer == 'Ask again later.' || answer == 'Better not tell you now.' || answer == 'Cannot predict now.' || answer == 'Concentrate and ask again.') + { + // console.log("negative"); + level = "negative"; + } + else + { + // console.log("very negative"); + level = "very negative"; + } + + return level; //Write your code in here } +// let answer = shakeBall(); +// console.log(shakeBall()); +// console.log(checkAnswer(answer)); /* ================================== @@ -116,3 +231,4 @@ test("checkAnswer works for `It is decidedly so.`", () => { test("checkAnswer works for `My reply is no.`", () => { expect(checkAnswer("My reply is no.")).toEqual("very negative"); }); +