From afe5929a72f6f3dc92fc3c37182fd814105c49a6 Mon Sep 17 00:00:00 2001 From: Rahwa Date: Thu, 16 Dec 2021 14:44:34 +0000 Subject: [PATCH 1/4] exercises and Mandatory sections are done. --- exercises/B-hello-world/exercise.js | 21 ++++++++++++ exercises/C-variables/exercise.js | 5 +++ exercises/D-strings/exercise.js | 14 +++++++- exercises/E-strings-concatenation/exercise.js | 4 +++ exercises/F-strings-methods/exercise.js | 5 +++ exercises/F-strings-methods/exercise2.js | 5 ++- exercises/G-numbers/exercise.js | 10 ++++++ exercises/I-floats/exercise.js | 10 ++++++ exercises/J-functions/exercise.js | 13 +++++-- exercises/J-functions/exercise2.js | 10 ++++-- exercises/K-functions-parameters/exercise.js | 5 +-- exercises/K-functions-parameters/exercise2.js | 6 ++-- exercises/K-functions-parameters/exercise3.js | 6 +++- exercises/K-functions-parameters/exercise4.js | 8 ++++- exercises/K-functions-parameters/exercise5.js | 8 +++-- exercises/L-functions-nested/exercise.js | 27 ++++++++++++--- mandatory/1-syntax-errors.js | 21 ++++++++---- mandatory/2-logic-error.js | 34 +++++++++++++++---- mandatory/3-function-output.js | 9 +++-- mandatory/4-tax.js | 20 +++++++++-- 20 files changed, 204 insertions(+), 37 deletions(-) diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..0166d2522 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,22 @@ + + +//console.log(Hello world) console.log("Hello world"); + +//Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!' +console.log("Hello World. I just started learning JavaScript!"); + + +//What happens when you console.log() just a number without quotes? +//Terminal prints out a number. +console.log(80); + + +//Try to console.log() several things at once. +//Terminal prints out everything in one line. +console.log("I bought a shoes for £80", "Hello world", "I am curious to learn JS"); + + +//What happens when you get rid of the quote marks? +//Terminal shows a syntax error. +console.log(I am curious to learn JS); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..954e0e415 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `greeting` +//Add a variable `greeting` to exercise.js (make sure it comes _before_ the console.log +let greeting="Hello world!"; +//Print your `greeting` to the console 3 times +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..5e86f8a63 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,15 @@ // Start by creating a variable `message` -console.log(message); +//Write a program that logs a message and its type +const number = 4; +let message = "Rahwa"; +let text; +//Terminal shows a number +console.log(typeof number); + +//Terminal shows a string +console.log(typeof message); + +//Terminal shows undefined +console.log(typeof text); + diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..e8457a2ed 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `message` +//Write a program that logs a message with a greeting and your name +let startGreeting = "Hi, my name is "; +let nameOfPerson="Rahwa"; +let message = startGreeting + nameOfPerson; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..b5361ba45 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,8 @@ // Start by creating a variable `message` +//Log a message that includes the length of your name +let nameOfPerson = "Rahwa"; +let nameOfPersonLength = nameOfPerson.length; +let message = `My name is ${nameOfPerson} and my name is ${nameOfPersonLength} character long.`; + console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..8eacf8de0 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,6 @@ -const name = " Daniel "; +//Log the same message using the variable, `name` provided +//Use the `.trim` method to remove the extra whitespace +const nameOfPerson = " Daniel "; +let message = nameOfPerson.trim(); console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..b318eac24 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,11 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` + +//Create two variables `numberOfStudents` and `numberOfMentors` + +let numberOfStudents = 15; +let numberOfMentors = 8; +let sum = numberOfStudents + numberOfMentors; + +//Log a message that displays the total number of students and mentors + +console.log(`The total number of students and mentors is ${sum}`); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..3db4f5018 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,12 @@ + +//Using the variables provided in the exercise calculate the percentage of mentors and students in the group + var numberOfStudents = 15; var numberOfMentors = 8; +var total = numberOfStudents + numberOfMentors; +var percentageOfStudents = (Math.round(numberOfStudents/total * 100)); +var percentageOfMentors = (Math.round(numberOfMentors / total * 100)); + + +console.log (`percentage students:${percentageOfStudents}%`); +console.log(`percentage mentors:${percentageOfMentors}%`); diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..cd8c6232c 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,16 @@ + +//Complete the function in exercise.js so that it halves the input function halve(number) { // complete the function here + return number/2; } -var result = halve(12); +//Try calling the function more than once with some different numbers +let resultOne = halve(12); +let resultTwo = halve(50); +let resultThree = halve(125); + +console.log(resultOne); +console.log(resultTwo); +console.log(resultThree); -console.log(result); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..6cd970856 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,11 @@ + +//Complete the function in exercise2.js so that it triples the input + function triple(number) { // complete function here + + return number * 3; } +let resultOne = triple(12); -var result = triple(12); - -console.log(result); +console.log(resultOne); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..d96efd1a8 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,10 @@ // 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` -var result = multiply(3, 4); +let result = multiply(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..d4919eac6 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,7 @@ // Declare your function first - -var result = divide(3, 4); +function divide(a, b){ + return a/b; +} +let 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..5a5f94d91 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,9 @@ // Write your function here -var greeting = createGreeting("Daniel"); + function creatGreeting(nameOfPerson){ +return `Hello, my name is ${nameOfPerson}`; +} +//calling the function +let greeting = creatGreeting("Daniel"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..4b32ba66e 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,11 @@ // Declare your function first +//Write a function that adds two numbers together +//Call the function, passing `13` and `124` as parameters, and assigning the returned value to a variable `sum` +function sum(a, b){ + return a + b; +} // Call the function and assign to a variable `sum` +let total = sum(13, 124); -console.log(sum); +console.log(total); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..762cbae3e 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,9 @@ // Declare your function here -const greeting = createLongGreeting("Daniel", 30); +//Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) +function nameAndAge(name, age){ +return `My name is ${name} and I am ${age} years old.`; +} +const longGreeting = nameAndAge("Daniel", 30); -console.log(greeting); +console.log(longGreeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..0f9c1f73a 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,22 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; + +//Your program should include a function that spells their name in uppercase, and a function that creates a shouty greeting. +//Log each greeting to the console. + +// let mentor1 = "Daniel"; +// let mentor2 = "Irina"; +// let mentor3 = "Mimi"; +// let mentor4 = "Rob"; +// let mentor5 = "Yohannes"; + function upperCase(greeting, nameOfPerson){ + + let text = `${greeting.toUpperCase()} ${nameOfPerson.toUpperCase()}`; + return text; + } + + console.log(upperCase("Hi", "Daniel")); + console.log(upperCase("Hi", "Irina")); + console.log(upperCase("Hi", "Mimi")); + console.log(upperCase("Hi", "Rob")); + console.log(upperCase("Hi", "Yohannes")); + + diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..48899aa6a 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,18 +1,25 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { +//addNumbers adds numbers correctly +function addNumbers(a, b, c) { return a + b + c; } +console.log(addNumbers(3, 4, 6)); +//introduceMe function returns the correct string function introduceMe(name, age) - return "Hello, my name is " + name "and I am " age + "years old"; - +{ + let message = `Hello, my name is ${name} and I am ${age} years old`; + return message; + } +console.log(introduceMe("Sonjide", 27)); + +//getTotal returns a string describing the total function getTotal(a, b) { - total = a ++ b; - - return "The total is total"; + let total = a + b; + return "The total is " + total ; } - +console.log(getTotal(23, 5)) /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..dc482aa86 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,18 +1,40 @@ // The syntax for this function is valid but it has an error, find it and fix it. +//trimWord trims leading and trailing whitespace function trimWord(word) { - return wordtrim(); + return word.trim(); } +console.log(trimWord("CodeYourFuture ")); -function getStringLength(word) { - return "word".length(); +//trimWord doesn't remove whitespace in the middle of the string +function trimSentence(sentence) { + return sentence.trim(); } +console.log(trimSentence(" CodeYourFuture teaches coding ")); -function multiply(a, b, c) { - a * b * c; - return; +//getStringLength returns the length of a word + function getStringLength(word) { + return word.length; +} +console.log(getStringLength("Turtles")); + +//getStringLength returns the length of a sentence + function getStringLength(sentence) { + return sentence.length; } +console.log(getStringLength("A wild sentence appeared!")); +//multiply multiplies numbers + function multiply(a, b, c) { + return a * b * c; +} +console.log(multiply(2, 3, 6)); + +//multiply multiplies different numbers +function multiply(a, b, c) { + return a * b * c; +} +console.log(multiply(2, 3, 4)); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..5555f123f 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,18 +1,23 @@ // Add comments to explain what this function does. You're meant to use Google! +//return Math.random() * 10 - It returns a random number between 0 and 9 where 0 and 9 are included. function getRandomNumber() { return Math.random() * 10; } -// Add comments to explain what this function does. You're meant to use Google! +// Add comments to explain what this function does. You're meant to use Google! +//return word1.concat(word2) - Joins word1 and word2. function combine2Words(word1, word2) { + return word1.concat(word2); } +//Join firstWord, secondWord and thirdWord. 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); } - +console.log(concatenate("code", "your", "future")); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..4dd4bb0c6 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,8 +5,14 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(priceOfProduct) { + let priceProductTax = priceOfProduct + (priceOfProduct * 20) / 100; + return priceProductTax; +} +console.log(calculateSalesTax(15)); +console.log(calculateSalesTax(17.50)); +console.log(calculateSalesTax(34)); /* CURRENCY FORMATTING =================== @@ -17,7 +23,17 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} + +function addTaxAndFormatCurrency(calculateSalesTax) { + let priceProductTax = calculateSalesTax + (calculateSalesTax * 20) / 100; + let taxFormat = `£${priceProductTax.toFixed(2)}`; + return taxFormat; +} +console.log(addTaxAndFormatCurrency(15)); +console.log(addTaxAndFormatCurrency(17.5)); +console.log(addTaxAndFormatCurrency(34)); + + /* =================================================== From c979d4cc884b8fd42a505c07fe3c0bfaa5d09701 Mon Sep 17 00:00:00 2001 From: Rahwa Date: Thu, 16 Dec 2021 15:27:20 +0000 Subject: [PATCH 2/4] changed script test --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 93e0861e3..dc77551f0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Exercises for JS1 Week 1", "license": "CC-BY-SA-4.0", "scripts": { - "test": "jest" + "test": "jest","extra-tests":"jest extra" }, "repository": { "type": "git", From ba7c9c4f9cbe216c5fa46c7eafebdc8edfcd6070 Mon Sep 17 00:00:00 2001 From: Rahwa Date: Thu, 16 Dec 2021 15:35:41 +0000 Subject: [PATCH 3/4] Update 3-magic-8-ball.js --- extra/3-magic-8-ball.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index f3adbefa5..32d8dfc96 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,5 +1,6 @@ /** - +const matchers = require(“jest-extended”); +expect.extend(matchers); Let's peer into the future using a Magic 8 Ball! https://en.wikipedia.org/wiki/Magic_8-Ball From f843f10f0f97a01d81676b51f56bbb9a4214bd65 Mon Sep 17 00:00:00 2001 From: Rahwa Date: Thu, 16 Dec 2021 17:12:09 +0000 Subject: [PATCH 4/4] Update 2-logic-error.js --- mandatory/2-logic-error.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index dc482aa86..2e53c98ed 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -4,37 +4,37 @@ function trimWord(word) { return word.trim(); } -console.log(trimWord("CodeYourFuture ")); +// console.log(trimWord("CodeYourFuture ")); -//trimWord doesn't remove whitespace in the middle of the string -function trimSentence(sentence) { - return sentence.trim(); -} -console.log(trimSentence(" CodeYourFuture teaches coding ")); +// //trimWord doesn't remove whitespace in the middle of the string +// function trimSentence(sentence) { +// return sentence.trim(); +// } +// console.log(trimSentence(" CodeYourFuture teaches coding ")); //getStringLength returns the length of a word function getStringLength(word) { return word.length; } -console.log(getStringLength("Turtles")); +// console.log(getStringLength("Turtles")); -//getStringLength returns the length of a sentence - function getStringLength(sentence) { - return sentence.length; -} -console.log(getStringLength("A wild sentence appeared!")); +// //getStringLength returns the length of a sentence +// function getStringLength(sentence) { +// return sentence.length; +// } +// console.log(getStringLength("A wild sentence appeared!")); //multiply multiplies numbers function multiply(a, b, c) { return a * b * c; } -console.log(multiply(2, 3, 6)); +// console.log(multiply(2, 3, 6)); //multiply multiplies different numbers -function multiply(a, b, c) { - return a * b * c; -} -console.log(multiply(2, 3, 4)); +// function multiply(a, b, c) { +// return a * b * c; +// } +// console.log(multiply(2, 3, 4)); /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE =====