From e707bd218706f34fc425cd30669bc1d00cbba888 Mon Sep 17 00:00:00 2001 From: CYF Date: Thu, 15 Sep 2022 22:00:08 +0100 Subject: [PATCH] first commit --- README.md | 4 ++-- exercises/B-hello-world/README.md | 2 +- exercises/B-hello-world/exercise.js | 1 + exercises/C-variables/README.md | 2 +- exercises/C-variables/exercise.js | 4 +++- exercises/D-strings/exercise.js | 5 ++-- exercises/E-strings-concatenation/exercise.js | 6 +++-- exercises/F-strings-methods/exercise.js | 5 ++-- exercises/F-strings-methods/exercise2.js | 6 +++-- exercises/G-numbers/exercise.js | 4 ++++ exercises/J-functions/exercise.js | 2 +- exercises/J-functions/exercise2.js | 2 +- exercises/K-functions-parameters/exercise.js | 4 ++-- exercises/K-functions-parameters/exercise2.js | 4 +++- exercises/K-functions-parameters/exercise4.js | 9 +++++-- exercises/K-functions-parameters/exercise5.js | 3 +++ exercises/L-functions-nested/README.md | 6 ++--- exercises/L-functions-nested/exercise.js | 24 +++++++++++++++---- mandatory/1-syntax-errors.js | 12 +++++----- mandatory/2-logic-error.js | 11 +++++---- mandatory/3-function-output.js | 9 ++++--- mandatory/4-tax.js | 9 +++++-- package.json | 12 +++++++--- 23 files changed, 100 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index dff05d7cd..c1d1921ed 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The exercises are split into three folders: `exercises`, `mandatory` and `extra` The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson. -## Running the code/tests + Running the code/tests The files for the mandatory/extra exercises are intended to be run as jest tests. @@ -14,7 +14,7 @@ The files for the mandatory/extra exercises are intended to be run as jest tests - To run the tests for all mandatory/extra exercises, run `npm test` - To run only the tests for the mandatory exercises, run `npm test -- --selectProjects mandatory` - To run only the tests for the extra exercises, run `npm test -- --selectProjects extra` -- To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-) +- To run a single exercise/test (for example `mandatory/1-writer.js`), ` (Remember, you `npm test -- --testPathPattern mandatorycan use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-) For more information about tests, look here: diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 27282c4af..3a87bc5ea 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -7,7 +7,7 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". ### 1. Run the program - Open a terminal window -- Change directory to this folder (`cd exercises/B-hello-world`) - assuming you're at the project root +- Change directory to this folder (cd exercises/B-hello-w`orld`) - assuming you're at the project root - Run the program using node (`node exercise.js`) ### 2. Experiment diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..b96785966 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,2 @@ console.log("Hello world"); +console.log("Hello World. I just started learning JavaScript!."); diff --git a/exercises/C-variables/README.md b/exercises/C-variables/README.md index 40859b3a8..86f48973b 100644 --- a/exercises/C-variables/README.md +++ b/exercises/C-variables/README.md @@ -15,7 +15,7 @@ The program above will print "Hello world" to the console. Notice how it uses th - Add a variable `greeting` to exercise.js (make sure it comes _before_ the console.log) - Print your `greeting` to the console 3 times -> Remember: to run this exercise you must change directory to the `C-variables`. If you already have a terminal window open for the previous exercise you can do this by running the command `cd ../C-variables`. +> Remember: to run this exercise you must change directory to the `C-variables`. If you already have a terminal window open for the previous exercise you can do this by running the command ` .cd./C-variables`. ## Expected result diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..3244c32f2 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..14f5be92a 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - -console.log(message); +var message = "This is the string " +var messageType = typeof message; +console.log(messageType); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..8db503e68 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 = "Hello, my name is "; + var name = "Daniel"; + 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..be8031297 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` - -console.log(message); +var name = "Justinas"; +var nameLength = name.length; +console.log(nameLength); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..e0a10ae3d 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,5 @@ -const name = " Daniel "; +const greetingStart = "Hello, my name is "; +const name = " Justinas "; +const greeting = greetingStart + name; -console.log(message); +console.log(greeting); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..56e5e2133 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,5 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfStudents = 15; +const numberOfMentors = 8; +const totalNumbers = numberOfStudents + numberOfMentors; +console.log(totalNumbers); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..e7278f7bd 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,5 +1,5 @@ function halve(number) { - // complete the function here + return number / 2; } var result = halve(12); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..95ef62512 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; } var result = triple(12); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..a16e99144 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(number) { +return number * 4; } // 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..0c70a87c2 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(number){ + return number / 4; + } var result = divide(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..08b78f8bd 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 (number){ + return number; +} // Call the function and assign to a variable `sum` +var sum = add (13 + 124); + + console.log(sum); + -console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..9d6c850ad 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,4 +1,7 @@ // Declare your function here +function createLongGreeting(name, age){ + return greeting = 'Hello, my name is + name + and I am +age + years old' +} const greeting = createLongGreeting("Daniel", 30); diff --git a/exercises/L-functions-nested/README.md b/exercises/L-functions-nested/README.md index 1c4ae9c8a..973a73a99 100644 --- a/exercises/L-functions-nested/README.md +++ b/exercises/L-functions-nested/README.md @@ -9,10 +9,10 @@ function getAgeInDays(age) { return age * 365; } -function createCreeting(name, age) { +createCreeting(name, age) { var ageInDays = getAgeInDays(age); var message = - "My Name is " + name + " and I was born over " + ageInDays + " days ago!"; + "My Name is " + name + " and I was born over " + ageInDaysfunctio + " days ago!"; return message; } ``` @@ -29,6 +29,6 @@ function createCreeting(name, age) { HELLO DANIEL HELLO IRINA HELLO MIMI -HELLO ROB +HELLO ROBunction createCreeting(name, age HELLO YOHANNES ``` diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..ab3893323 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,19 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; + + +const mentor1 = "Daniel"; +const mentor2 = "Irina"; +const mentor3 = "Mimi"; +const mentor4 = "Rob"; +const mentor5 = "Yohannes"; + +function getMentorsUppercase(mentor){ + return mentor.toUpperCase(); +} +function concatShoutyGreeting(mentor){ + return "HELLO " + getMentorsUppercase(mentor); +} +console.log(concatShoutyGreeting(mentor1)); +console.log(concatShoutyGreeting(mentor2)); +console.log(concatShoutyGreeting(mentor3)); +console.log(concatShoutyGreeting(mentor4)); +console.log(concatShoutyGreeting(mentor5)); \ No newline at end of file diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..82f862952 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,16 +1,16 @@ // 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; - return "The total is total"; + return "The total is " + (a + b) ; + } /* diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..c1f36a33a 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,16 +1,19 @@ // 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..02f635699 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,17 +1,20 @@ -// 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! The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user. 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!The concat() method joins two or more strings. The concat() method does not change the existing strings. The concat() method returns a new string. + function combine2Words(word1, word2) { return word1.concat(word2); } function concatenate(firstWord, secondWord, thirdWord) { + return firstWord.concat(" " + secondWord.concat(` ${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. -} /* =================================================== diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..7936bc41e 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -5,8 +5,10 @@ Sales tax is 20% of the price of the product. */ -function calculateSalesTax() {} +function calculateSalesTax(amount) { + return amount *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(number) { +return '\u00A3' + calculateSalesTax(number).toFixed(2); +} /* =================================================== 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" + ] } ] },