From 7529056219dde72f1101148e9733d2ea30070295 Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Sun, 20 Nov 2022 22:18:47 +0000 Subject: [PATCH 1/6] Completed exercises and mandatory --- .vscode/launch.json | 17 ++++++++++++++ exercises/B-hello-world/exercise.js | 2 ++ exercises/C-variables/exercise.js | 4 ++++ exercises/D-strings/exercise.js | 3 +++ exercises/E-strings-concatenation/exercise.js | 6 +++++ exercises/F-strings-methods/exercise.js | 5 ++++- exercises/F-strings-methods/exercise2.js | 7 ++++++ exercises/G-numbers/exercise.js | 11 ++++++++++ exercises/I-floats/exercise.js | 9 ++++++++ exercises/J-functions/exercise.js | 4 +++- exercises/J-functions/exercise2.js | 4 +++- exercises/K-functions-parameters/exercise.js | 4 +++- exercises/K-functions-parameters/exercise2.js | 5 ++++- exercises/K-functions-parameters/exercise3.js | 3 +++ exercises/K-functions-parameters/exercise4.js | 5 +++++ exercises/K-functions-parameters/exercise5.js | 4 ++++ exercises/L-functions-nested/exercise.js | 16 ++++++++++++++ extra/1-currency-conversion.js | 8 +++++-- extra/2-piping.js | 22 ++++++++++++------- mandatory/1-syntax-errors.js | 13 ++++++----- mandatory/2-logic-error.js | 7 +++--- mandatory/3-function-output.js | 3 +++ mandatory/4-tax.js | 9 ++++++-- 23 files changed, 145 insertions(+), 26 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..d5a35f96a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/exercises/E-strings-concatenation/exercise.js" + } + ] +} \ No newline at end of file diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..1365d60e2 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ console.log("Hello world"); + +console.log ("Hello"); \ No newline at end of file diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..28ad712d4 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,7 @@ // Start by creating a variable `greeting` +let 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..e8756ed60 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 = `This is a string`; +let 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..adadbec94 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,9 @@ // Start by creating a variable `message` +let greeting="Hello, my name is"; + +let name="Mohamed"; + +let message=`${greeting} ${name}` + console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..5c7b88746 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,6 @@ // Start by creating a variable `message` -console.log(message); +let name="Mohamed"; +let nameLength = name.length; + +console.log(nameLength); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..843329854 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,10 @@ const name = " Daniel "; + +let greeting = "Hello, my name is"; + + +let message = `${greeting} ${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..28f18a369 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,12 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents = 73; +let numberOfMentors = 17; +let totalNumber = numberOfMentors + numberOfStudents; + +console.log(`Number of students: ${numberOfStudents}`) +console.log(`Number of mentors: ${numberOfMentors}`); +console.log(`Total number of students and mentors: ${totalNumber}`); + + + + diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..71c0fdb6b 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,11 @@ var numberOfStudents = 15; var numberOfMentors = 8; + +let totalNumber = numberOfMentors + numberOfStudents; + +let percentageStudents = Math.round(numberOfStudents / totalNumber * 100); +let percentageMentors = Math.round(numberOfMentors / totalNumber * 100); + +console.log(`Percentage students: ${percentageStudents}%`); + +console.log(`Percentage mentors: ${percentageMentors}%`) \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..6e6b527e1 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,9 @@ function halve(number) { // complete the function here + let result =number/2; + return result; } -var result = halve(12); +var result = halve(13); console.log(result); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..233607cdd 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,9 @@ function triple(number) { // complete function here + let result = number*3; + return result; } -var result = triple(12); +var result = triple(10); console.log(result); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..64ed940b6 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,8 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { // Calculate the result of the function and return it + let result = a * b; + return result; } // 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..5eb3d597f 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,8 @@ // Declare your function first - +function divide (a,b) { + let result=a/b; + return result; +} 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..f64dcdeeb 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,4 +1,7 @@ // Write your function here +function createGreeting (name) { + return `Hello ${name}`; +} var greeting = createGreeting("Daniel"); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..b2ceafd6d 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,10 @@ // Declare your function first +function add (a,b) { + return a+b; +} + // 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..34de1d3a0 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,9 @@ // 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/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..ce2a71a08 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,21 @@ +function upperCaseName(name) { + return name.toUpperCase(); +} + +function greeting(name) { + return `HELLO ${upperCaseName(name)}`; +} + + + var mentor1 = "Daniel"; var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +console.log (greeting(mentor1)); +console.log(greeting(mentor2)); +console.log(greeting(mentor3)); +console.log(greeting(mentor4)); +console.log(greeting(mentor5)); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..cbdde045e 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 Math.round(price * 1.4*100)/100; +} /* CURRENCY CONVERSION @@ -15,7 +17,9 @@ 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) { + return Math.round(price*0.99*5.7*100)/100; +} /* ======= 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..84e1e9126 100644 --- a/extra/2-piping.js +++ b/extra/2-piping.js @@ -16,26 +16,32 @@ 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(a) { + return `£`+a; } + const startingValue = 2; // Why can this code be seen as bad practice? Comment your answer. -let badCode = +// It's hard to read it. +let badCode = format(multiply(add(startingValue,10),2)); + /* BETTER PRACTICE */ +let plusTen = add(startingValue,10); +let double = multiply(plusTen,2); +let poundFormat = format(double); +let goodCode = poundFormat; -let 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/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..b770d1278 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,18 +1,21 @@ // 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; + let total = a + b; - return "The total is total"; + return "The total is " + total; } + /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== 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..249b7c86b 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,9 +1,11 @@ // Add comments to explain what this function does. You're meant to use Google! +// The function returns a random number that's greater than or equal to 1 and less than 10. function getRandomNumber() { return Math.random() * 10; } // Add comments to explain what this function does. You're meant to use Google! +// This function merge two words together (word1 and word2). function combine2Words(word1, word2) { return word1.concat(word2); } @@ -11,6 +13,7 @@ function combine2Words(word1, 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. + return firstWord.concat(' ').concat(secondWord).concat(' ').concat(thirdWord); } /* diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..1353ecba2 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 * 1.2; +} /* CURRENCY FORMATTING @@ -17,7 +19,10 @@ function calculateSalesTax() {} Remember that the prices must include the sales tax (hint: you already wrote a function for this!) */ -function addTaxAndFormatCurrency() {} +function addTaxAndFormatCurrency(price) { + return `£${calculateSalesTax(price).toFixed(2)}` +} + /* =================================================== From ad5ad2c35272d8220eaad8ffb1e283db3c32c303 Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Tue, 22 Nov 2022 11:01:02 +0000 Subject: [PATCH 2/6] magic ball --- extra/3-magic-8-ball.js | 70 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 46f65f928..1126767ef 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -45,9 +45,35 @@ // This should log "The ball has shaken!" // and return the answer. -function shakeBall() { - //Write your code in here -} + 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.' + ] + let answer = ""; + + function shakeBall() { + //Write your code in here + console.log("The ball has shaken!"); + return (answer = answers[Math.floor(Math.random() * answers.length)]); + + }; + /* This function should say whether the answer it is given is @@ -58,10 +84,48 @@ function shakeBall() { This function should expect to be called with any value which was returned by the shakeBall function. */ + let positiveOrNegative =""; + function checkAnswer(answer) { //Write your code in here + switch (answer) { + case "It is certain.": + case "It is decidedly so.": + case "Without a doubt.": + case "Yes - definitely.": + case "You may rely on it.": + positiveOrNegative = "very positive"; + break; + case "As I see it, yes.": + case "Most likely.": + case "Outlook good.": + case "Yes.": + case "Signs point to yes.": + positiveOrNegative = "positive"; + 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.": + positiveOrNegative = "negative"; + 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.": + positiveOrNegative = "very negative"; + break; + } + return positiveOrNegative; } + + // shakeBall(); + // console.log(answer); + // checkAnswer("Very doubtful."); + // console.log(positiveOrNegative); /* ================================== ======= TESTS - DO NOT MODIFY ===== From b97289c52ff757dc2de81e18e02bb0e94fc4be1c Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Tue, 22 Nov 2022 12:39:57 +0000 Subject: [PATCH 3/6] Update 3-magic-8-ball.js --- extra/3-magic-8-ball.js | 74 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 1126767ef..4c945900b 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,3 +1,4 @@ +// const { toBeOneOf } = require("jest-extended"); /** Let's peer into the future using a Magic 8 Ball! @@ -45,35 +46,35 @@ // This should log "The ball has shaken!" // and return the answer. - 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.' - ] - let answer = ""; - - function shakeBall() { - //Write your code in here - console.log("The ball has shaken!"); - return (answer = answers[Math.floor(Math.random() * answers.length)]); - - }; - +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.", +]; +let answer = ""; + +function shakeBall() { + //Write your code in here + console.log("The ball has shaken!"); + return (answer = answers[Math.floor(Math.random() * answers.length)]); +} /* This function should say whether the answer it is given is @@ -84,7 +85,7 @@ This function should expect to be called with any value which was returned by the shakeBall function. */ - let positiveOrNegative =""; +let positiveOrNegative = ""; function checkAnswer(answer) { //Write your code in here @@ -121,11 +122,10 @@ function checkAnswer(answer) { return positiveOrNegative; } - - // shakeBall(); - // console.log(answer); - // checkAnswer("Very doubtful."); - // console.log(positiveOrNegative); +// shakeBall(); +// console.log(answer); +// checkAnswer("Very doubtful."); +// console.log(positiveOrNegative); /* ================================== ======= TESTS - DO NOT MODIFY ===== @@ -165,7 +165,9 @@ test("magic 8 ball returns different values each time", () => { ); } - let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); + let seenPositivities = new Set( + Array.from(seenAnswers.values()).map(checkAnswer) + ); if (seenPositivities.size < 2) { throw Error( "Expected to random answers with different positivities each time shakeBall was called, but always got the same one" From 87baef2f6bc33e302e7ed3d1f7b5b21c5e4d69cc Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Thu, 24 Nov 2022 15:37:03 +0000 Subject: [PATCH 4/6] Jest-extended library --- extra/3-magic-8-ball.js | 2 +- package.json | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index 4c945900b..a241f7994 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -1,4 +1,4 @@ -// const { toBeOneOf } = require("jest-extended"); +const { toBeOneOf } = require("jest-extended"); /** Let's peer into the future using a Magic 8 Ball! diff --git a/package.json b/package.json index 93e0861e3..9e7f2ffeb 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,21 @@ "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/issues" }, "jest": { - "setupFilesAfterEnv": ["jest-extended"], + "setupFilesAfterEnv": [ + "jest-extended" + ], "projects": [ { "displayName": "mandatory", - "testMatch": ["/mandatory/*.js"] + "testMatch": [ + "/mandatory/*.js" + ] }, { "displayName": "extra", - "testMatch": ["/extra/*.js"] + "testMatch": [ + "/extra/*.js" + ] } ] }, From e737e2b94a520f20dfe1f265aab67b2eb5033e88 Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Sun, 27 Nov 2022 14:04:39 +0000 Subject: [PATCH 5/6] adjust --- extra/3-magic-8-ball.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/3-magic-8-ball.js b/extra/3-magic-8-ball.js index a241f7994..780d8eac0 100644 --- a/extra/3-magic-8-ball.js +++ b/extra/3-magic-8-ball.js @@ -73,7 +73,7 @@ let answer = ""; function shakeBall() { //Write your code in here console.log("The ball has shaken!"); - return (answer = answers[Math.floor(Math.random() * answers.length)]); + return answer = answers[Math.floor(Math.random() * answers.length)]; } /* From 12f1bc2f984464e2a3a0f87d2758c8e56ad7d144 Mon Sep 17 00:00:00 2001 From: Mohamed Abdalla Date: Mon, 28 Nov 2022 20:26:16 +0000 Subject: [PATCH 6/6] changed let to const --- exercises/C-variables/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index 28ad712d4..1e20f1d0b 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,6 +1,6 @@ // Start by creating a variable `greeting` -let greeting = 'Hello World'; +const greeting = 'Hello World'; console.log(greeting); console.log(greeting);