diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 46c150e15..6e1505c43 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -16,18 +16,18 @@ Please complete the details below this message # Your Details -- Your Name: -- Your City: -- Your Slack Name: +- Your Name: Jude Ricketts +- Your City: Ldn 8 +- Your Slack Name: Loves Pictures # Homework Details -- Module: -- Week: +- Module: JS1 +- Week: 1 # Notes -- What did you find easy? +- What did you find easy? - - What did you find hard? diff --git a/exercises/A-setup-ide/README.md b/exercises/A-setup-ide/README.md index de230419b..314a64cbf 100644 --- a/exercises/A-setup-ide/README.md +++ b/exercises/A-setup-ide/README.md @@ -1,13 +1,15 @@ -There are some tools that will help you to write code. One of these, [Prettier](https://prettier.io/), formats your code, making it easier for you and others to read. - -### 1. Install prettier - -- In Visual Studio open the extensions panel (see https://code.visualstudio.com/docs/editor/extension-gallery#_browse-and-install-extensions) -- Search for `Prettier - Code formatter` -- Click install on the top result - -### 2. Enable formatting on save - -- In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings) -- Search for `editor format` -- Set `editor.formatOnSave` and `editor.formatOnPaste` to true +There are some tools that will help you to write code. One of these, [Prettier](https://prettier.io/), formats your code, making it easier for you and others to read. + +### 1. Install prettier + +- In Visual Studio open the extensions panel (see https://code.visualstudio.com/docs/editor/extension-gallery#_browse-and-install-extensions) +- Search for `Prettier - Code formatter` +- Click install on the top result + +### 2. Enable formatting on save + +- In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings) +- Search for `editor format` +- Set `editor.formatOnSave` - Did this + +and `editor.formatOnPaste` to true - Would not load this - to be revisited diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 27282c4af..4b743b96b 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -1,18 +1,18 @@ -It is programming tradition that the first thing you do in any language is make it output 'Hello world!'. - -We'll do this in JavaScript, using something called `console.log()`. - -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 -- Run the program using node (`node exercise.js`) - -### 2. Experiment - -- Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. -- Try to console.log() several things at once. -- What happens when you get rid of the quote marks? -- What happens when you console.log() just a number without quotes? +It is programming tradition that the first thing you do in any language is make it output 'Hello world!'. + +We'll do this in JavaScript, using something called `console.log()`. + +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 +- Run the program using node (`node exercise.js`) + +### 2. Experiment + +- Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'. +- Try to console.log() several things at once. +- What happens when you get rid of the quote marks? +- What happens when you console.log() just a number without quotes? diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..451eaa815 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"); +console.log("My name is Jude" + " hello world :) "); diff --git a/exercises/C-variables/README.md b/exercises/C-variables/README.md index 40859b3a8..9b0867773 100644 --- a/exercises/C-variables/README.md +++ b/exercises/C-variables/README.md @@ -1,26 +1,26 @@ -When you write code, you'll want to create shortcuts to data values so you can don't have to write out the same value every time. - -We can use _variable_ to create a reference to a value. - -```js -var greeting = "Hello world"; - -console.log(greeting); -``` - -The program above will print "Hello world" to the console. Notice how it uses the value assigned to the variable `greeting`. - -## Exercise - -- 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`. - -## Expected result - -``` -Hello world -Hello world -Hello world -``` +When you write code, you'll want to create shortcuts to data values so you can don't have to write out the same value every time. + +We can use _variable_ to create a reference to a value. + +```js +var greeting = "Hello world"; + +console.log(greeting); +``` + +The program above will print "Hello world" to the console. Notice how it uses the value assigned to the variable `greeting`. + +## Exercise + +- 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`. + +## Expected result + +``` +Hello world +Hello world +Hello world +``` diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..34739e54a 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ -// Start by creating a variable `greeting` - -console.log(greeting); +// 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/README.md b/exercises/D-strings/README.md index fd0a664ae..3f5887c0c 100644 --- a/exercises/D-strings/README.md +++ b/exercises/D-strings/README.md @@ -1,29 +1,29 @@ -In programming there are different _types of_ data. You've used one data type already: **string**. - -Computers recognise strings as a sequence of characters but to humans, strings are simply lines of text. - -```js -var message = "This is a string"; -``` - -Notice that strings are always wrapped **inside of quote marks**. We do this so that the computer knows when the string starts and ends. - -You can check that the data is a string by using the `typeof` operator: - -```js -var message = "This is a string"; -var messageType = typeof message; - -console.log(messageType); // logs 'string' -``` - -## Exercise - -- Write a program that logs a message and its type - -## Expected result - -``` -This is a string -string -``` +In programming there are different _types of_ data. You've used one data type already: **string**. + +Computers recognise strings as a sequence of characters but to humans, strings are simply lines of text. + +```js +var message = "This is a string"; +``` + +Notice that strings are always wrapped **inside of quote marks**. We do this so that the computer knows when the string starts and ends. + +You can check that the data is a string by using the `typeof` operator: + +```js +var message = "This is a string"; +var messageType = typeof message; + +console.log(messageType); // logs 'string' +``` + +## Exercise + +- Write a program that logs a message and its type + +## Expected result + +``` +This is a string +string +``` diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..738cbb5f2 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,5 @@ -// Start by creating a variable `message` - -console.log(message); +// Start by creating a variable `message` + +x = "This is a string"; +console.log(x); +console.log(typeof x); diff --git a/exercises/E-strings-concatenation/README.md b/exercises/E-strings-concatenation/README.md index cba54dbca..9427124d9 100644 --- a/exercises/E-strings-concatenation/README.md +++ b/exercises/E-strings-concatenation/README.md @@ -1,20 +1,20 @@ -You can add two strings together using the plus operator (`+`): - -```js -var greetingStart = "Hello, my name is "; -var name = "Daniel"; - -var greeting = greetingStart + name; - -console.log(greeting); // Logs "Hello, my name is Daniel" -``` - -## Exercise - -- Write a program that logs a message with a greeting and your name - -## Expected result - -``` -Hello, my name is Daniel -``` +You can add two strings together using the plus operator (`+`): + +```js +var greetingStart = "Hello, my name is "; +var name = "Daniel"; + +var greeting = greetingStart + name; + +console.log(greeting); // Logs "Hello, my name is Daniel" +``` + +## Exercise + +- Write a program that logs a message with a greeting and your name + +## Expected result + +``` +Hello, my name is Daniel +``` diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..7a021dbbd 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,8 @@ -// Start by creating a variable `message` - -console.log(message); +// Start by creating a variable `message` + +var greetingStart = "Hello, my name is "; +var myName = "Jude"; + +var greeting = greetingStart + myName; + +console.log(greeting); diff --git a/exercises/F-strings-methods/README.md b/exercises/F-strings-methods/README.md index fb07ec8ef..1798bdf16 100644 --- a/exercises/F-strings-methods/README.md +++ b/exercises/F-strings-methods/README.md @@ -1,40 +1,40 @@ -You can find out how many characters there are in a string by using the `length` property of a string: - -```js -var name = "Daniel"; -var nameLength = name.length; - -console.log(nameLength); // Logs 6 -``` - -You can also get a modified version of a string by calling _string methods_. Let's try one: - -```js -var name = "Daniel"; -var nameLowerCase = name.toLowerCase(); - -console.log(nameLowerCase); // "daniel" -``` - -You can find out more about string properties and methods by searching for "JavaScript string methods". - -## Exercise 1 - -- Log a message that includes the length of your name - -## Expected result - -``` -My name is Daniel and my name is 6 characters long -``` - -## Exercise 2 - -- Log the same message using the variable, `name` provided -- Use the `.trim` method to remove the extra whitespace - -## Expected result - -``` -My name is Daniel and my name is 6 characters long -``` +You can find out how many characters there are in a string by using the `length` property of a string: + +```js +var name = "Daniel"; +var nameLength = name.length; + +console.log(nameLength); // Logs 6 +``` + +You can also get a modified version of a string by calling _string methods_. Let's try one: + +```js +var name = "Daniel"; +var nameLowerCase = name.toLowerCase(); + +console.log(nameLowerCase); // "daniel" +``` + +You can find out more about string properties and methods by searching for "JavaScript string methods". + +## Exercise 1 + +- Log a message that includes the length of your name + +## Expected result + +``` +My name is Daniel and my name is 6 characters long +``` + +## Exercise 2 + +- Log the same message using the variable, `name` provided +- Use the `.trim` method to remove the extra whitespace + +## Expected result + +``` +My name is Daniel and my name is 6 characters long +``` diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..8dabf87bf 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,28 @@ -// Start by creating a variable `message` - -console.log(message); +// Start by creating a variable `message` + +// exercise 1 +var myName = "Jude"; +var nameLength = myName.length; +console.log(nameLength); // Logs 4 + +// exercise 2 a +var myName = ` JUDE RICKETTS `; +var nameLowerCase = myName.toLowerCase(); +console.log(nameLowerCase.trim()); // "jude ricketts" + +// exercise 2 b +let text = " Jude Ricketts "; +let result = text.replace(/^\s+|\s+$/gm, ""); +console.log(result); + +// exercise 3 +// this needs to use a similar stucture to; console.log(str1.concat(' ', str2)); +var myName = "Jude "; +var nameLength = myName.length; +var myChars = " characters long"; + +console.log( + "My name is " + myName + "and my name is " + nameLength + myChars + "." +); + +// str.trim() diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..30307dac5 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,9 @@ -const name = " Daniel "; - -console.log(message); +// exercise 2 a +var myName = ` JUDE RICKETTS `; +var nameLowerCase = myName.toLowerCase(); +console.log(nameLowerCase.trim()); // "jude ricketts" + +// exercise 2 b +let text = " Jude Ricketts "; +let result = text.replace(/^\s+|\s+$/gm, ""); +console.log(result); diff --git a/exercises/G-numbers/README.md b/exercises/G-numbers/README.md index 76a79dab2..b8c55b588 100644 --- a/exercises/G-numbers/README.md +++ b/exercises/G-numbers/README.md @@ -1,29 +1,29 @@ -The next data type we will learn is **number**. - -Unlike strings, numbers do not need to be wrapped in quotes. - -```js -var age = 30; -``` - -You can use mathematical operators to caclulate numbers: - -```js -var sum = 10 + 2; // 12 -var product = 10 * 2; // 20 -var quotient = 10 / 2; // 5 -var difference = 10 - 2; // 8 -``` - -## Exercise - -- Create two variables `numberOfStudents` and `numberOfMentors` -- Log a message that displays the total number of students and mentors - -## Expected result - -``` -Number of students: 15 -Number of mentors: 8 -Total numnber of students and mentors: 23 -``` +The next data type we will learn is **number**. + +Unlike strings, numbers do not need to be wrapped in quotes. + +```js +var age = 30; +``` + +You can use mathematical operators to caclulate numbers: + +```js +var sum = 10 + 2; // 12 +var product = 10 * 2; // 20 +var quotient = 10 / 2; // 5 +var difference = 10 - 2; // 8 +``` + +## Exercise + +- Create two variables `numberOfStudents` and `numberOfMentors` +- Log a message that displays the total number of students and mentors + +## Expected result + +``` +Number of students: 15 +Number of mentors: 8 +Total numnber of students and mentors: 23 +``` diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..20f481e10 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,24 @@ -// Start by creating a variables `numberOfStudents` and `numberOfMentors` +const numberOfStudents = 15; +console.log(numberOfStudents); + +const numberOfMentors = 8; +console.log(numberOfMentors); + +// add two numbers +const sum = numberOfStudents + numberOfMentors; + +// display the number of students +console.log("The total number of students is: " + numberOfStudents); + +// display the number of mentees +console.log("The total number of mentees is: " + numberOfMentors); + +// display the sum +console.log( + "The total number of students is " + + numberOfStudents + + "plus the total number of mentors are " + + numberOfMentors + + "making the total number: " + + sum +); diff --git a/exercises/I-floats/README.md b/exercises/I-floats/README.md index bbc9e29c2..c257f8bc8 100644 --- a/exercises/I-floats/README.md +++ b/exercises/I-floats/README.md @@ -1,23 +1,23 @@ -Numbers can be integers (whole numbers) or floats (numbers with a decimal). - -```js -var preciseAge = 30.612437; -``` - -Floats can be rounded to the nearest whole number using the `Math.round` function: - -```js -var preciseAge = 30.612437; -var roughAge = Math.round(preciseAge); // 30 -``` - -## Exercise - -- Using the variables provided in the exercise calculate the percentage of mentors and students in the group - -## Expected result - -``` -Percentage students: 65% -Percentage mentors: 35% -``` +Numbers can be integers (whole numbers) or floats (numbers with a decimal). + +```js +var preciseAge = 30.612437; +``` + +Floats can be rounded to the nearest whole number using the `Math.round` function: + +```js +var preciseAge = 30.612437; +var roughAge = Math.round(preciseAge); // 30 +``` + +## Exercise + +- Using the variables provided in the exercise calculate the percentage of mentors and students in the group + +## Expected result + +``` +Percentage students: 65% +Percentage mentors: 35% +``` diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..b69c3a792 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,14 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +var total = 23; + +// what is number of students as a % of the total +var numberOfStudents = 15; + +var percent = (numberOfStudents * 100) / total; +console.log("Percentage student: " + Math.round(percent) + "%"); +// ------------------------ + +//what is number of mentors as a % of the total +var numberOfMentors = 8; + +var percent = (numberOfMentors * 100) / total; +console.log("Percentage mentors: " + Math.round(percent) + "%"); diff --git a/exercises/J-functions/README.md b/exercises/J-functions/README.md index 22105ca2a..1be896f69 100644 --- a/exercises/J-functions/README.md +++ b/exercises/J-functions/README.md @@ -1,42 +1,42 @@ -Functions are blocks of code that can do a task as many times as you ask it to. They take an input and return an output. - -Here's a function that doubles a number: - -```js -function double(number) { - return number * 2; -} -``` - -To use the function we need to: -a) _call_ it with an input and -b) assign the returned value to a variable - -```js -var result = double(2); - -console.log(result); // 4 -``` - -## Exercise - -- Complete the function in exercise.js so that it halves the input -- Try calling the function more than once with some different numbers - -> Remember to use the return keyword to get a value out of the function - -## Expected result - -``` -6 -``` - -## Exercise 2 - -- Complete the function in exercise2.js so that it triples the input - -## Expected result - -``` -36 -``` +Functions are blocks of code that can do a task as many times as you ask it to. They take an input and return an output. + +Here's a function that doubles a number: + +```js +function double(number) { + return number * 2; +} +``` + +To use the function we need to: +a) _call_ it with an input and +b) assign the returned value to a variable + +```js +var result = double(2); + +console.log(result); // 4 +``` + +## Exercise + +- Complete the function in exercise.js so that it halves the input +- Try calling the function more than once with some different numbers + +> Remember to use the return keyword to get a value out of the function + +## Expected result + +``` +6 +``` + +## Exercise 2 + +- Complete the function in exercise2.js so that it triples the input + +## Expected result + +``` +36 +``` diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..7c703c1d1 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,15 @@ -function halve(number) { - // complete the function here -} - -var result = halve(12); - -console.log(result); +// exercise 1 +function halve(number) { + return number / 2; +} + +var result = halve(12); +console.log(result); + +// exercise 2 +function triples(number) { + return number * 3; +} + +var result = triples(12); +console.log(result); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..1493ab5e2 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,6 @@ -function triple(number) { - // complete function here -} - -var result = triple(12); - -console.log(result); +function triple(number) { + return number * 3; +} + +var result = triple(12); +console.log(result); diff --git a/exercises/K-functions-parameters/README.md b/exercises/K-functions-parameters/README.md index 1614e221b..b7ff14b7c 100644 --- a/exercises/K-functions-parameters/README.md +++ b/exercises/K-functions-parameters/README.md @@ -1,70 +1,70 @@ -The input given to a function is called a **parameter**. - -A function can take more than one parameter: - -```js -function add(a, b) { - return a + b; -} -``` - -When you write a function (sometimes called _declaring a function_) you assign names to the parameters inside of the parentheses (`()`). Parameters can be called anything. - -This function is exactly the same as the on above: - -```js -function add(num1, num2) { - return num1 + num2; -} -``` - -## Exercise - -- Write a function that multiplies two numbers together - -## Expected result - -``` -12 -``` - -## Exercise 2 - -- From scratch, write a function that divides two numbers - -## Expected result - -``` -0.75 -``` - -## Exercise 3 - -- Write a function that takes a name (a string) and returns a greeting - -## Expected result - -``` -Hello, my name is Daniel -``` - -## Exercise 4 - -- 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` - -## Expected result - -``` -137 -``` - -## Exercise 5 - -- Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) - -## Expected result - -``` -Hello, my name is Daniel and I'm 30 years old -``` +The input given to a function is called a **parameter**. + +A function can take more than one parameter: + +```js +function add(a, b) { + return a + b; +} +``` + +When you write a function (sometimes called _declaring a function_) you assign names to the parameters inside of the parentheses (`()`). Parameters can be called anything. + +This function is exactly the same as the on above: + +```js +function add(num1, num2) { + return num1 + num2; +} +``` + +## Exercise + +- Write a function that multiplies two numbers together + +## Expected result + +``` +12 +``` + +## Exercise 2 + +- From scratch, write a function that divides two numbers + +## Expected result + +``` +0.75 +``` + +## Exercise 3 + +- Write a function that takes a name (a string) and returns a greeting + +## Expected result + +``` +Hello, my name is Daniel +``` + +## Exercise 4 + +- 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` + +## Expected result + +``` +137 +``` + +## Exercise 5 + +- Write a function that takes a name (a string) and an age (a number) and returns a greeting (a string) + +## Expected result + +``` +Hello, my name is Daniel and I'm 30 years old +``` diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..647d4d6cf 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,22 @@ -// Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it -} - -// Assign the result of calling the function the variable `result` -var result = multiply(3, 4); - -console.log(result); +// Complete the function so that it takes input parameters +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); +console.log(result); + +// exercise 2 +function divides(a, b) { + return a / b; +} +var result = divides(3, 4); +console.log(result); + +// exercise 3 +function myName(a) { + return "Hello my name is " + a; +} +var result = myName("Jude"); +console.log(result); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..080735a56 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,9 @@ -// Declare your function first - -var result = divide(3, 4); - -console.log(result); +// Declare your function first +function divide(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 = divide(3, 4); + +console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..0ad351250 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,18 @@ -// Write your function here - -var greeting = createGreeting("Daniel"); - -console.log(greeting); +// Write your function here +function createGreeting(name) { + // returning the greeting result + return "Hello, my name is " + name + "!"; +} +//call the fuction here +var greeting = createGreeting("Daniel"); +// var greeting = "Hello, my name is " + name + "!" + +console.log(greeting); + +// option b +// exercise 3 +function myName(a) { + return "Hello my name is " + a; +} +var result = myName("Jude"); +console.log(result); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..4da9ff33e 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,11 @@ -// Declare your function first - -// Call the function and assign to a variable `sum` - -console.log(sum); +// Declare your function first + +// var a = 13 +// var b = 124 + +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..a071bad0f 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,8 @@ -// Declare your function here - -const greeting = createLongGreeting("Daniel", 30); - -console.log(greeting); +// Declare your function here +function createLongGreeting(a, b) { + return "Hello my name is " + a + " and I am " + b + " years old"; +} + +const greeting = createLongGreeting("Daniel", 30); + +console.log(greeting); diff --git a/exercises/L-functions-nested/README.md b/exercises/L-functions-nested/README.md index 1c4ae9c8a..cd606cb99 100644 --- a/exercises/L-functions-nested/README.md +++ b/exercises/L-functions-nested/README.md @@ -1,34 +1,34 @@ -Functions are very powerful. - -- You can write more than one line of code inside of functions. -- You can use variables inside of functions. -- You can call other functions inside of functions! - -```js -function getAgeInDays(age) { - return age * 365; -} - -function createCreeting(name, age) { - var ageInDays = getAgeInDays(age); - var message = - "My Name is " + name + " and I was born over " + ageInDays + " days ago!"; - return message; -} -``` - -## Exercise 1 - -- In `exercise.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one. -- 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. - -## Expected result - -``` -HELLO DANIEL -HELLO IRINA -HELLO MIMI -HELLO ROB -HELLO YOHANNES -``` +Functions are very powerful. + +- You can write more than one line of code inside of functions. +- You can use variables inside of functions. +- You can call other functions inside of functions! + +```js +function getAgeInDays(age) { + return age * 365; +} + +function createCreeting(name, age) { + var ageInDays = getAgeInDays(age); + var message = + "My Name is " + name + " and I was born over " + ageInDays + " days ago!"; + return message; +} +``` + +## Exercise 1 + +- In `exercise.js` you have been provided with the names of some mentors. Write a program that logs a shouty greeting to each one. +- 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. + +## Expected result + +``` +HELLO DANIEL +HELLO IRINA +HELLO MIMI +HELLO ROB +HELLO YOHANNES +``` diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..039a1beeb 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"; +var mentor1 = "Daniel"; +var mentor2 = "Irina"; +var mentor3 = "Mimi"; +var mentor4 = "Rob"; +var mentor5 = "Yohannes"; + +function shoutyGreeting(a) { + //input 1 + + // Hello + mentor - uppercase + return "HELLO " + a.toUpperCase(); +} +//call + +console.log(shoutyGreeting(mentor1)); +console.log(shoutyGreeting(mentor2)); +console.log(shoutyGreeting(mentor3)); +console.log(shoutyGreeting(mentor4)); +console.log(shoutyGreeting(mentor5)); diff --git a/extra/1-currency-conversion.js b/extra/1-currency-conversion.js index 75b3c6aab..f30a7c666 100644 --- a/extra/1-currency-conversion.js +++ b/extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(priceInUSD) { + priceInUSD = 1.4 * priceInPounds * 0.71; + return Number(priceInUSD.toFixed(2)); +} /* CURRENCY CONVERSION @@ -15,7 +18,10 @@ 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 priceInBRL = 0.99 * priceInPounds * 5.7; + return Number(priceInBRL.toFixed(2)); +} /* ======= 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..542f45b62 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,40 +1,47 @@ -// There are syntax errors in this code - can you fix it to pass the tests? - -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 getTotal(a, b) { - total = a ++ b; - - return "The total is total"; -} - -/* -=================================================== -======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - -There are some Tests in this file that will help you work out if your code is working. - -To run the tests for just this one file, type `npm test -- --testPathPattern 1-syntax-errors` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) - -=================================================== -*/ - -test("addNumbers adds numbers correctly", () => { - expect(addNumbers(3, 4, 6)).toEqual(13); -}); - -test("introduceMe function returns the correct string", () => { - expect(introduceMe("Sonjide", 27)).toEqual( - "Hello, my name is Sonjide and I am 27 years old" - ); -}); - -test("getTotal returns a string describing the total", () => { - expect(getTotal(23, 5)).toEqual("The total is 28"); -}); +// There are syntax errors in this code - can you fix it to pass the tests? + +function addNumbers(a, b, c) { + return a + b + c; +} +// how do I look at the output of each +console.log(addNumbers); + +// declare the peramiters - new +const name = "Jude"; +const age = 35; + +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; +} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== + +There are some Tests in this file that will help you work out if your code is working. + +To run the tests for just this one file, type `npm test -- --testPathPattern 1-syntax-errors` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) + +=================================================== +*/ + +test("addNumbers adds numbers correctly", () => { + expect(addNumbers(3, 4, 6)).toEqual(13); +}); + +test("introduceMe function returns the correct string", () => { + expect(introduceMe("Sonjide", 27)).toEqual( + "Hello, my name is Sonjide and I am 27 years old" + ); +}); + +test("getTotal returns a string describing the total", () => { + expect(getTotal(23, 5)).toEqual("The total is 28"); +}); diff --git a/mandatory/2-logic-error.js b/mandatory/2-logic-error.js index 9cca7603b..d711ed520 100644 --- a/mandatory/2-logic-error.js +++ b/mandatory/2-logic-error.js @@ -1,51 +1,57 @@ -// The syntax for this function is valid but it has an error, find it and fix it. - -function trimWord(word) { - return wordtrim(); -} - -function getStringLength(word) { - return "word".length(); -} - -function multiply(a, b, c) { - a * b * c; - return; -} - -/* -=================================================== -======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - -There are some Tests in this file that will help you work out if your code is working. - -To run the tests for just this one file, type `npm test -- --testPathPattern 2-logic-error` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -=================================================== -*/ - -test("trimWord trims leading and trailing whitespace", () => { - expect(trimWord(" CodeYourFuture ")).toEqual("CodeYourFuture"); -}); - -test("trimWord doesn't remove whitespace in the middle of the string", () => { - expect(trimWord(" CodeYourFuture teaches coding ")).toEqual( - "CodeYourFuture teaches coding" - ); -}); - -test("getStringLength returns the length of a word", () => { - expect(getStringLength("Turtles")).toEqual(7); -}); - -test("getStringLength returns the length of a sentence", () => { - expect(getStringLength("A wild sentence appeared!")).toEqual(25); -}); - -test("multiply multiplies numbers", () => { - expect(multiply(2, 3, 6)).toEqual(36); -}); - -test("multiply multiplies different numbers", () => { - expect(multiply(2, 3, 4)).toEqual(24); -}); +// The syntax for this function is valid but it has an error, find it and fix it. + +function trimWord(word) { + result = word.trim(); + return result; +} + +// ------------exercise 2 +function getStringLength(word) { + "Hello World ".replace(/\s+/g, ""); + return word.length; +} +var a = 2; +var b = 3; +var c = 6; +var d = 4; + +function multiply(a, b, c) { + return a * b * c; +} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== + +There are some Tests in this file that will help you work out if your code is working. + +To run the tests for just this one file, type `npm test -- --testPathPattern 2-logic-error` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("trimWord trims leading and trailing whitespace", () => { + expect(trimWord(" CodeYourFuture ")).toEqual("CodeYourFuture"); +}); + +test("trimWord doesn't remove whitespace in the middle of the string", () => { + expect(trimWord(" CodeYourFuture teaches coding ")).toEqual( + "CodeYourFuture teaches coding" + ); +}); + +test("getStringLength returns the length of a word", () => { + expect(getStringLength("Turtles")).toEqual(7); +}); + +test("getStringLength returns the length of a sentence", () => { + expect(getStringLength("A wild sentence appeared!")).toEqual(25); +}); + +test("multiply multiplies numbers", () => { + expect(multiply(2, 3, 6)).toEqual(36); +}); + +test("multiply multiplies different numbers", () => { + expect(multiply(2, 3, 4)).toEqual(24); +}); diff --git a/mandatory/3-function-output.js b/mandatory/3-function-output.js index 5a953ba60..86c0d015f 100644 --- a/mandatory/3-function-output.js +++ b/mandatory/3-function-output.js @@ -1,37 +1,46 @@ -// Add comments to explain what this function does. You're meant to use Google! -function getRandomNumber() { - return Math.random() * 10; -} - -// Add comments to explain what this function does. You're meant to use Google! -function combine2Words(word1, word2) { - 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. -} - -/* -=================================================== -======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - -There are some Tests in this file that will help you work out if your code is working. - -To run the tests for just this one file, type `npm test -- --testPathPattern 3-function-output` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -================================== -*/ - -test("concatenate example #1", () => { - expect(concatenate("code", "your", "future")).toEqual("code your future"); -}); - -test("concatenate example #2", () => { - expect(concatenate("I", "like", "pizza")).toEqual("I like pizza"); -}); - -test("concatenate doesn't only accept strings", () => { - expect(concatenate("I", "am", 13)).toEqual("I am 13"); -}); +// Add comments to explain what this function does. You're meant to use Google! + +//writing a fiction with a varialbe of getRandomNumber +function getRandomNumber() { + //Math.random, returns a number between 0 (inclusive) and 1 (exclusive), them multiplies it by 10 and adds 1 + var getRandomNumber = Math.floor(Math.random() * 10 + 1); + return Math.random() * 10; +} + +// Add comments to explain what this function does. You're meant to use Google! +//thi is a function called combine2Words with a return of word 1 + +function combine2Words(word1, word2) { + 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. + return firstWord + " " + secondWord + " " + thirdWord; +} +//call the fuction +console.log(concatenate("Jude", " Good", " Morn")); + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== + +There are some Tests in this file that will help you work out if your code is working. + +To run the tests for just this one file, type `npm test -- --testPathPattern 3-function-output` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +================================== +*/ + +test("concatenate example #1", () => { + expect(concatenate("code", "your", "future")).toEqual("code your future"); +}); + +test("concatenate example #2", () => { + expect(concatenate("I", "like", "pizza")).toEqual("I like pizza"); +}); + +test("concatenate doesn't only accept strings", () => { + expect(concatenate("I", "am", 13)).toEqual("I am 13"); +}); diff --git a/mandatory/4-tax.js b/mandatory/4-tax.js index ba77c7ae2..446bdd6ba 100644 --- a/mandatory/4-tax.js +++ b/mandatory/4-tax.js @@ -1,55 +1,63 @@ -/* - SALES TAX - ========= - A business requires a program that calculates how much the price of a product is including sales tax - Sales tax is 20% of the price of the product. -*/ - -function calculateSalesTax() {} - -/* - CURRENCY FORMATTING - =================== - The business has informed you that prices must have 2 decimal places - They must also start with the currency symbol - Write a function that adds tax to a number, and then transforms the total into the format £0.00 - - Remember that the prices must include the sales tax (hint: you already wrote a function for this!) -*/ - -function addTaxAndFormatCurrency() {} - -/* -=================================================== -======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - -There are some Tests in this file that will help you work out if your code is working. - -To run the tests for just this one file, type `npm test -- --testPathPattern 4-tax` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) -=================================================== -*/ - -test("calculateSalesTax for £15", () => { - expect(calculateSalesTax(15)).toEqual(18); -}); - -test("calculateSalesTax for £17.50", () => { - expect(calculateSalesTax(17.5)).toEqual(21); -}); - -test("calculateSalesTax for £34", () => { - expect(calculateSalesTax(34)).toEqual(40.8); -}); - -test("addTaxAndFormatCurrency for £15", () => { - expect(addTaxAndFormatCurrency(15)).toEqual("£18.00"); -}); - -test("addTaxAndFormatCurrency for £17.50", () => { - expect(addTaxAndFormatCurrency(17.5)).toEqual("£21.00"); -}); - -test("addTaxAndFormatCurrency for £34", () => { - expect(addTaxAndFormatCurrency(34)).toEqual("£40.80"); -}); +/* + SALES TAX + ========= + A business requires a program that calculates how much the price of a product is including sales tax + Sales tax is 20% of the price of the product. +*/ + +// price of the product /100 * 20% (100% + 20 % ) sales tax + +function calculateSalesTax(a) { + return a * 0.2 + a; +} + +/* + CURRENCY FORMATTING + =================== + The business has informed you that prices must have 2 decimal places + They must also start with the currency symbol + Write a function that adds tax to a number, and then transforms the total into the format £0.00 + + Remember that the prices must include the sales tax (hint: you already wrote a function for this!) +*/ + +function addTaxAndFormatCurrency(a) { + salesTax = calculateSalesTax(a); // v = 18 + Decimal = "£" + salesTax.toFixed(2); // to 2 decimal places add £ + return Decimal; +} + +/* +=================================================== +======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== + +There are some Tests in this file that will help you work out if your code is working. + +To run the tests for just this one file, type `npm test -- --testPathPattern 4-tax` into your terminal +(Reminder: You must have run `npm install` one time before this will work!) +=================================================== +*/ + +test("calculateSalesTax for £15", () => { + expect(calculateSalesTax(15)).toEqual(18); +}); + +test("calculateSalesTax for £17.50", () => { + expect(calculateSalesTax(17.5)).toEqual(21); +}); + +test("calculateSalesTax for £34", () => { + expect(calculateSalesTax(34)).toEqual(40.8); +}); + +test("addTaxAndFormatCurrency for £15", () => { + expect(addTaxAndFormatCurrency(15)).toEqual("£18.00"); +}); + +test("addTaxAndFormatCurrency for £17.50", () => { + expect(addTaxAndFormatCurrency(17.5)).toEqual("£21.00"); +}); + +test("addTaxAndFormatCurrency for £34", () => { + expect(addTaxAndFormatCurrency(34)).toEqual("£40.80"); +}); 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" + ] } ] },