From e19980fa7bc0afd8dbfac64bb8c523b46b4f63ff Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sat, 26 Jun 2021 16:49:02 +0100 Subject: [PATCH 01/14] A-setup-ide complete --- exercises/A-setup-ide/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/A-setup-ide/README.md b/exercises/A-setup-ide/README.md index de230419b..706b7387b 100644 --- a/exercises/A-setup-ide/README.md +++ b/exercises/A-setup-ide/README.md @@ -1,4 +1,4 @@ -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. +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 From 3df4d7a6420c919a93c8ee3c0204c74a4c0b7e60 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sat, 26 Jun 2021 18:52:54 +0100 Subject: [PATCH 02/14] B-hello-world complete --- exercises/B-hello-world/README.md | 4 ++++ exercises/B-hello-world/exercise.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/exercises/B-hello-world/README.md b/exercises/B-hello-world/README.md index 8c704fb43..89c6eca2f 100644 --- a/exercises/B-hello-world/README.md +++ b/exercises/B-hello-world/README.md @@ -14,5 +14,9 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!". - 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? +When executing "node exercise.js" from the terminal with no quote marks I received the error "SyntaxError: missing ) after argument list, which makes sense as with no quotation marks the debugger/interpreter is expecting only a one word value within the brackets while with the quotation marks it is expecting a string which can have multiple words with spaces". + - What happens when you console.log() just a number without quotes? +The output is a value and is highlighted in a base colour in the terminal, however, if a number is put into quotation marks it becomes a string and is highlighted in a different (string) colour. \ No newline at end of file diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..5777e1351 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,9 @@ console.log("Hello world"); + +console.log("Hello World. I just started learning JavaScript!"); + +console.log(12); + +console.log("12"); + +console.log(Hello World. No Quotes!); \ No newline at end of file From 12833899f7a39eb1a23717ea09aee673ffc61636 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sat, 26 Jun 2021 19:45:04 +0100 Subject: [PATCH 03/14] C-variables complete --- exercises/C-variables/exercise.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..e5e4da8ec 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,9 @@ -// Start by creating a variable `greeting` +// Program to print a `greeting` variable in the console 3 times +// Hopefully it is ok to be a bit creative with my solution and reduce code repetition :) -console.log(greeting); +let greeting = + ". My name is Andrew and Ive been learning how to code with YourFuture since 10/04/21"; + +for (let i = 1; i < 4; i++) { + console.log(i + greeting); +} From 482aea575d31573ae128f763453fcc35594bc1c0 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sat, 26 Jun 2021 21:11:50 +0100 Subject: [PATCH 04/14] D-strings Complete --- exercises/D-strings/exercise.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..186a82326 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,23 @@ -// Start by creating a variable `message` +// Program that selects one of the three basic types of variables then outputs it along with its type. +// Again, hopefully its ok that I added some randomness to this exercise, I had a lot of fun doing it :) -console.log(message); +// Declare the `messageChoice` variable, then set it to a random number between 0 and 2 rounded to the nearest integer. +let messageChoice = Math.round(Math.random() * 2); + +// Declares the `message` variable. +let message; + +// Determines which message to display using the random `messageChoice` value. +if (messageChoice == 0) { + message = "Hello world!"; +} else if (messageChoice == 1) { + message = 25.5; +} else { + message = true; +} + +// Declares `messageType` variable and sets it equal to the variable `message` type (string, number or boolean) +let messageType = typeof message; + +// Outputs the selected `message` and `messageType` +console.log(message + " is a " + messageType); From 0d8b20dbb0eaeabf4e2e6d663c81f71ce22a41cc Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sat, 26 Jun 2021 23:04:57 +0100 Subject: [PATCH 05/14] E-strings-concatenation complete --- exercises/E-strings-concatenation/exercise.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..96355aff8 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); +// Program which states my first name to practice concatenation. +let greetingStart = "Hello, my name is "; +let myName = "Andrew"; +let greeting = greetingStart + myName; +console.log(greeting); From 02b3ed65cea57baa03fd4c0a0858bfaffc6debc2 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 10:28:12 +0100 Subject: [PATCH 06/14] F-strings-methods complete --- exercises/F-strings-methods/exercise.js | 10 +++++++++- exercises/F-strings-methods/exercise2.js | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..a03e6be00 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,11 @@ -// Start by creating a variable `message` +// Program that outputs a concatenated string stored in variable `message` containing constant `myName` +// and constant `nameLength` after calculating `myNames` character length using string method .length. +// I used constants here as `myName` will never change and consequently its length wont either, however +// the variable `message` concatenation may be updated so I declared it using let + +const myName = "Andrew"; +const nameLength = myName.length; +let message = + "My name is " + myName + " and my name is " + nameLength + " characters long"; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..51d7c0366 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,10 @@ -const name = " Daniel "; +// This program performs the same basic function as exercise.js +// however using the string method .trim() to remove the white space +// at both sides of the string " Daniel ". + +const myName = " Daniel ".trim(); +const nameLength = myName.length; +let message = + "My name is " + myName + " and my name is " + nameLength + " characters long"; console.log(message); From dc27fb3b037b9551d71cc1e90c9f3afcd1a22a99 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 11:09:03 +0100 Subject: [PATCH 07/14] G-numbers complete --- exercises/G-numbers/exercise.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..5e70c2d12 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,13 @@ -// Start by creating a variables `numberOfStudents` and `numberOfMentors` +// Program that outputs the value of `numberOfStudents`, `numberOfMentors` +// and `totalStudentsAndMentors` after using template string literal syntax to concatenate +// the string stored in `message`. + +let numberOfStudents = 15; +let numberOfMentors = 8; +const totalStudentsAndMentors = numberOfStudents + numberOfMentors; + +let message = `Number of students: ${numberOfStudents} +Number of mentors: ${numberOfMentors} +Total number of students and mentors: ${totalStudentsAndMentors}`; + +console.log(message); From 9db6e9610ddb3281c4bb3fd68985fa486c55e87c Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 14:19:07 +0100 Subject: [PATCH 08/14] I-floats complete --- exercises/I-floats/exercise.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..9c637d415 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,24 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +// Program to calculate the percentage of students and mentors in a group. +let numberOfStudents = 15; +let numberOfMentors = 8; + +// I decided to declare the variable `percentValue` first and set its value equal to the +// actual percentage value for the group size as this reduces the required nesting and hopefully +// makes the code easier follow. +let percentValue = 100 / (numberOfStudents + numberOfMentors); + +// I then performed the main calculation using Math.round for both students and mentors, +// declaring and storing the results in their respective variables. Alternatively I could have calculated +// only one of the percentages then subtracted it from 100 (which I think would have been more efficient) +// but felt the spirit of the exercise was to use Math.floor so did it this way. +let percentageStudents = Math.round(percentValue * numberOfStudents); +let PercentageMentors = Math.round(percentValue * numberOfMentors); + +// After declaring the variable `result`, using string literal syntax I concatenated a result string +let result = ` +Percentage students: ${percentageStudents}% +Percentage mentors: ${PercentageMentors}% +`; + +// Output the result to the console +console.log(result); From 5e0a6a37f39d35b5d3d87392bcc0b6904a790a07 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 15:06:35 +0100 Subject: [PATCH 09/14] J-functions complete --- exercises/J-functions/exercise.js | 12 +++++++++--- exercises/J-functions/exercise2.js | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..9f1a79c26 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,13 @@ function halve(number) { - // complete the function here + // Function to half the input parameter `number` + return result = number / 2; } -var result = halve(12); +// Testing with specified input which also produces an integer value output +console.log(halve(12)); -console.log(result); +// Testing with an input that produces a float value output +console.log(halve(1)); + +// Testing 0 just to see what happens :) +console.log(halve(0)); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..19528adaf 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,7 +1,13 @@ function triple(number) { - // complete function here + // Function to triple the input parameter `number` + return (result = number * 3); } -var result = triple(12); +// Testing with specified integer input that should produces an integer value output +console.log(triple(12)); -console.log(result); +// Testing with a float input that should produce a float output +console.log(triple(0.3)); + +// Testing with 0 for fun again :) +console.log(triple(0)); From 599fc7921e7d98ff2dc0221b7763c6efb360ea1a Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 15:40:40 +0100 Subject: [PATCH 10/14] K-functions-parameters complete --- exercises/K-functions-parameters/exercise.js | 15 ++++++++++----- exercises/K-functions-parameters/exercise2.js | 16 +++++++++++++--- exercises/K-functions-parameters/exercise3.js | 9 +++++---- exercises/K-functions-parameters/exercise4.js | 15 ++++++++++++--- exercises/K-functions-parameters/exercise5.js | 10 ++++++---- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..a42fdb431 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,14 @@ // Complete the function so that it takes input parameters -function multiply() { - // Calculate the result of the function and return it +function multiply(numOne, numTwo) { + // Function to multiply two parameters together + return (result = numOne * numTwo); } -// Assign the result of calling the function the variable `result` -var result = multiply(3, 4); +// Testing by calling the function with suggested parameters +console.log(multiply(3, 4)); -console.log(result); +// Testing by calling the function with two float parameters +console.log(multiply(3.5, 4.7)); + +// Testing with calling the function with 0 as parameters +console.log(multiply(0, 0)); diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..936b3b4a4 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,15 @@ -// Declare your function first +function divide(numOne, numTwo) { + // Function to divide two parameters together + return (result = numOne / numTwo); +} -var result = divide(3, 4); +// Testing by calling the function with suggested parameters +console.log(divide(3, 4)); -console.log(result); +// Testing by calling the function with two float parameters +console.log(divide(3.5, 4.7)); + +// Testing with calling the function with 0 as parameters, interesting +// unlike the previous multiply function which produced a 0, this +// produces a NaN... +console.log(divide(0, 0)); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..1e6795e7e 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,6 @@ -// Write your function here +function greeting(myName) { + // Function takes myName parameter (string) and adds it to a greeting + return `Hello, my name is ${myName}`; +} -var greeting = createGreeting("Daniel"); - -console.log(greeting); +console.log(greeting("Daniel")); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..55e4416d4 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,14 @@ -// Declare your function first +function add(numOne, numTwo) { + // Function to add two parameters together + let sum = numOne + numTwo; + return sum; +} -// Call the function and assign to a variable `sum` +// Testing by calling the function with suggested parameters +console.log(add(13, 124)); -console.log(sum); +// Testing by calling the function with two float parameters +console.log(add(3.5, 4.7)); + +// Testing with calling the function with 0 as parameters +console.log(add(0, 0)); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..4da8109de 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,7 @@ -// Declare your function here +function createLongGreeting(myName, myAge) { + // Function takes myName parameter (string) and adds it to a greeting + return `Hello, my name is ${myName} and I'm ${myAge} years old`; +} -const greeting = createLongGreeting("Daniel", 30); - -console.log(greeting); +// Testing by calling the function with suggested parameters. +console.log(createLongGreeting("Daniel", 30)); From 6099521de170b6c6cb363d5268ca494d18b5ea78 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Sun, 27 Jun 2021 20:10:35 +0100 Subject: [PATCH 11/14] L-functions-nested complete --- exercises/L-functions-nested/exercise.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..e3f00eab1 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,14 @@ -var mentor1 = "Daniel"; -var mentor2 = "Irina"; -var mentor3 = "Mimi"; -var mentor4 = "Rob"; -var mentor5 = "Yohannes"; +// I hope the way I have solved this exercise is ok although the readme seemed to have +// some wiggle room but happy to redo if needed. As its the last exercise I wanted +// to try and be a bit creative so to solve this I have put the pre-defined mentor names into +// an array then iterated through it using a for loop which contains two nested ES6 +// arrow functions. Had great fun figuring out how to solve it like this:) + +let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; + +for (let i = 0; i < mentors.length; i++) { + let mentor = mentors[i]; + const getNameInUpperCase = (mentor) => mentor.toUpperCase(); + const shoutyGreeting = (mentor) => `HELLO ${getNameInUpperCase(mentor)}`; + console.log(shoutyGreeting(mentor)); +} From ac748fb11afa8b388352e2ff3abe59f8335e42e5 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Mon, 28 Jun 2021 10:30:28 +0100 Subject: [PATCH 12/14] Final polish before submission --- exercises/C-variables/exercise.js | 5 ++--- exercises/D-strings/exercise.js | 9 ++++++--- exercises/E-strings-concatenation/exercise.js | 2 +- exercises/I-floats/exercise.js | 2 +- exercises/L-functions-nested/exercise.js | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index e5e4da8ec..adab86dd4 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,8 +1,7 @@ // Program to print a `greeting` variable in the console 3 times -// Hopefully it is ok to be a bit creative with my solution and reduce code repetition :) +// Tried to reduce code repetition by using a for loop -let greeting = - ". My name is Andrew and Ive been learning how to code with YourFuture since 10/04/21"; +let greeting = ". Hello world"; for (let i = 1; i < 4; i++) { console.log(i + greeting); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 186a82326..165e1b78a 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,5 +1,6 @@ // Program that selects one of the three basic types of variables then outputs it along with its type. -// Again, hopefully its ok that I added some randomness to this exercise, I had a lot of fun doing it :) +// Hopefully its ok that I added some randomness to this exercise by highlighting the three main data types, +// If not or it causes jest to fail I will re-write, it was fun though :) // Declare the `messageChoice` variable, then set it to a random number between 0 and 2 rounded to the nearest integer. let messageChoice = Math.round(Math.random() * 2); @@ -9,7 +10,7 @@ let message; // Determines which message to display using the random `messageChoice` value. if (messageChoice == 0) { - message = "Hello world!"; + message = "This is a string"; } else if (messageChoice == 1) { message = 25.5; } else { @@ -20,4 +21,6 @@ if (messageChoice == 0) { let messageType = typeof message; // Outputs the selected `message` and `messageType` -console.log(message + " is a " + messageType); +console.log(` +${message} +${messageType}`); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 96355aff8..2894071c6 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,5 +1,5 @@ // Program which states my first name to practice concatenation. +const myName = "Andrew"; let greetingStart = "Hello, my name is "; -let myName = "Andrew"; let greeting = greetingStart + myName; console.log(greeting); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index 9c637d415..6136318e2 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -4,7 +4,7 @@ let numberOfMentors = 8; // I decided to declare the variable `percentValue` first and set its value equal to the // actual percentage value for the group size as this reduces the required nesting and hopefully -// makes the code easier follow. +// makes the code easier read. let percentValue = 100 / (numberOfStudents + numberOfMentors); // I then performed the main calculation using Math.round for both students and mentors, diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index e3f00eab1..debf89255 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,5 @@ // I hope the way I have solved this exercise is ok although the readme seemed to have -// some wiggle room but happy to redo if needed. As its the last exercise I wanted +// some wiggle room (hope jest does!) but happy to redo if needed. As its the last exercise I wanted // to try and be a bit creative so to solve this I have put the pre-defined mentor names into // an array then iterated through it using a for loop which contains two nested ES6 // arrow functions. Had great fun figuring out how to solve it like this:) From da7e5ac45700e4353005e1a79350daaf28d0735a Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Wed, 30 Jun 2021 20:37:11 +0100 Subject: [PATCH 13/14] Updated const naming convention & improved comments Updated const naming convention to be more distinguishable from let variables using camel case. In several cased tweaked the comments to better explain how the program works for clarity. --- exercises/D-strings/exercise.js | 4 ++-- exercises/E-strings-concatenation/exercise.js | 4 ++-- exercises/F-strings-methods/exercise.js | 10 +++++++--- exercises/F-strings-methods/exercise2.js | 10 +++++++--- exercises/G-numbers/exercise.js | 6 +++--- exercises/L-functions-nested/exercise.js | 10 ++++++---- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 165e1b78a..2b4eb3316 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,6 +1,6 @@ // Program that selects one of the three basic types of variables then outputs it along with its type. -// Hopefully its ok that I added some randomness to this exercise by highlighting the three main data types, -// If not or it causes jest to fail I will re-write, it was fun though :) +// Hopefully its ok that I added some randomness to this exercise by highlighting the three main data types, +// If not or it causes jest to fail I will re-write, it was fun though and has a 3:1 chance of printing the expected result :) // Declare the `messageChoice` variable, then set it to a random number between 0 and 2 rounded to the nearest integer. let messageChoice = Math.round(Math.random() * 2); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2894071c6..a414dc581 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,5 +1,5 @@ // Program which states my first name to practice concatenation. -const myName = "Andrew"; +const MY_NAME = "Andrew"; let greetingStart = "Hello, my name is "; -let greeting = greetingStart + myName; +let greeting = greetingStart + MY_NAME; console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index a03e6be00..60cc48784 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -3,9 +3,13 @@ // I used constants here as `myName` will never change and consequently its length wont either, however // the variable `message` concatenation may be updated so I declared it using let -const myName = "Andrew"; -const nameLength = myName.length; +const MY_NAME = "Andrew"; +const NAME_LENGTH = MY_NAME.length; let message = - "My name is " + myName + " and my name is " + nameLength + " characters long"; + "My name is " + + MY_NAME + + " and my name is " + + NAME_LENGTH + + " characters long"; console.log(message); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index 51d7c0366..762a4bee9 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -2,9 +2,13 @@ // however using the string method .trim() to remove the white space // at both sides of the string " Daniel ". -const myName = " Daniel ".trim(); -const nameLength = myName.length; +const MY_NAME = " Daniel ".trim(); +const NAME_LENGTH = MY_NAME.length; let message = - "My name is " + myName + " and my name is " + nameLength + " characters long"; + "My name is " + + MY_NAME + + " and my name is " + + NAME_LENGTH + + " characters long"; console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 5e70c2d12..476fb4e5d 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1,10 +1,10 @@ -// Program that outputs the value of `numberOfStudents`, `numberOfMentors` -// and `totalStudentsAndMentors` after using template string literal syntax to concatenate +// Program that outputs the value of `numberOfStudents`, `numberOfMentors` +// and `totalStudentsAndMentors` after using template string literal syntax to concatenate // the string stored in `message`. let numberOfStudents = 15; let numberOfMentors = 8; -const totalStudentsAndMentors = numberOfStudents + numberOfMentors; +let totalStudentsAndMentors = numberOfStudents + numberOfMentors; let message = `Number of students: ${numberOfStudents} Number of mentors: ${numberOfMentors} diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index debf89255..1ea61b3aa 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,8 +1,10 @@ // I hope the way I have solved this exercise is ok although the readme seemed to have -// some wiggle room (hope jest does!) but happy to redo if needed. As its the last exercise I wanted -// to try and be a bit creative so to solve this I have put the pre-defined mentor names into -// an array then iterated through it using a for loop which contains two nested ES6 -// arrow functions. Had great fun figuring out how to solve it like this:) +// some wiggle room (hope jest does!) but happy to redo if needed as I enjoyed thinking +// of an alternative solution. As its the last exercise I wanted to try and be a bit creative +// so to solve this I have put the pre-defined mentor names into an array then iterated +// through it using a for loop which contains two nested ES6 arrow functions with omitted +// return and curly brackets as they are on one line. Had great fun figuring out how to +// solve it like this:) let mentors = ["Daniel", "Irina", "Mimi", "Rob", "Yohannes"]; From 1c05f876c20982248f0ac4562e26e42fdf58cbb7 Mon Sep 17 00:00:00 2001 From: AndyR <50073313+Andy-Robertson@users.noreply.github.com> Date: Thu, 1 Jul 2021 11:22:46 +0100 Subject: [PATCH 14/14] Code Tidy Restructured some of the code with a focus on declaring and initialising variables first to improve readability. --- exercises/D-strings/exercise.js | 16 ++++++------ exercises/E-strings-concatenation/exercise.js | 7 +++++- exercises/F-strings-methods/exercise.js | 6 ++++- exercises/F-strings-methods/exercise2.js | 6 ++++- exercises/G-numbers/exercise.js | 10 ++++++-- exercises/I-floats/exercise.js | 25 +++++++++++-------- exercises/K-functions-parameters/exercise.js | 1 - 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2b4eb3316..cf890fb61 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -2,13 +2,15 @@ // Hopefully its ok that I added some randomness to this exercise by highlighting the three main data types, // If not or it causes jest to fail I will re-write, it was fun though and has a 3:1 chance of printing the expected result :) -// Declare the `messageChoice` variable, then set it to a random number between 0 and 2 rounded to the nearest integer. -let messageChoice = Math.round(Math.random() * 2); +// Declare and initialise variables. +let messageChoice = 0; +let messageType; +let message = ""; -// Declares the `message` variable. -let message; +// Set `messageChoice` variable to a random number between 0 and 2 rounded to the nearest integer. +messageChoice = Math.round(Math.random() * 2); -// Determines which message to display using the random `messageChoice` value. +// Determine which message to display using the random `messageChoice` value. if (messageChoice == 0) { message = "This is a string"; } else if (messageChoice == 1) { @@ -17,8 +19,8 @@ if (messageChoice == 0) { message = true; } -// Declares `messageType` variable and sets it equal to the variable `message` type (string, number or boolean) -let messageType = typeof message; +// Sets `messageType` variable equal to the variable `message` type (string, number or boolean). +messageType = typeof message; // Outputs the selected `message` and `messageType` console.log(` diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index a414dc581..0b16f8c9e 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,5 +1,10 @@ // Program which states my first name to practice concatenation. + +// Set and initialise variables and constants const MY_NAME = "Andrew"; let greetingStart = "Hello, my name is "; -let greeting = greetingStart + MY_NAME; +let greeting = ""; + +// Update and output `greeting` +greeting = greetingStart + MY_NAME; console.log(greeting); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 60cc48784..e50423e6e 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -3,9 +3,13 @@ // I used constants here as `myName` will never change and consequently its length wont either, however // the variable `message` concatenation may be updated so I declared it using let +// Declare and initialise variables and constants const MY_NAME = "Andrew"; const NAME_LENGTH = MY_NAME.length; -let message = +let message = ""; + +// Update Message & output message +message = "My name is " + MY_NAME + " and my name is " + diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index 762a4bee9..03b78adaa 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -2,9 +2,13 @@ // however using the string method .trim() to remove the white space // at both sides of the string " Daniel ". +// Declare and initialise variables and constants const MY_NAME = " Daniel ".trim(); const NAME_LENGTH = MY_NAME.length; -let message = +let message = ""; + +// Update Message & output message +message = "My name is " + MY_NAME + " and my name is " + diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 476fb4e5d..c18c95aaf 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -2,11 +2,17 @@ // and `totalStudentsAndMentors` after using template string literal syntax to concatenate // the string stored in `message`. +// Declare and initialise variables let numberOfStudents = 15; let numberOfMentors = 8; -let totalStudentsAndMentors = numberOfStudents + numberOfMentors; +let totalStudentsAndMentors = 0; +let message = ""; -let message = `Number of students: ${numberOfStudents} +// Calculate total number of students +totalStudentsAndMentors = numberOfStudents + numberOfMentors; + +// Concatenate string +message = `Number of students: ${numberOfStudents} Number of mentors: ${numberOfMentors} Total number of students and mentors: ${totalStudentsAndMentors}`; diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index 6136318e2..2ab067171 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,21 +1,26 @@ // Program to calculate the percentage of students and mentors in a group. + +// Declare and initialise variables let numberOfStudents = 15; let numberOfMentors = 8; +let percentValue = 0; +let percentageStudents = 0; +let PercentageMentors = 0; +let result = ""; -// I decided to declare the variable `percentValue` first and set its value equal to the -// actual percentage value for the group size as this reduces the required nesting and hopefully -// makes the code easier read. -let percentValue = 100 / (numberOfStudents + numberOfMentors); +// Set `percentValue` value equal to the actual percentage value for the group size as +// this reduces the required nesting and hopefully makes the code easier read. +percentValue = 100 / (numberOfStudents + numberOfMentors); -// I then performed the main calculation using Math.round for both students and mentors, -// declaring and storing the results in their respective variables. Alternatively I could have calculated +// Performed main calculations using Math.round for both students and mentors, +// storing the results in their respective variables. Alternatively I could have calculated // only one of the percentages then subtracted it from 100 (which I think would have been more efficient) // but felt the spirit of the exercise was to use Math.floor so did it this way. -let percentageStudents = Math.round(percentValue * numberOfStudents); -let PercentageMentors = Math.round(percentValue * numberOfMentors); +percentageStudents = Math.round(percentValue * numberOfStudents); +PercentageMentors = Math.round(percentValue * numberOfMentors); -// After declaring the variable `result`, using string literal syntax I concatenated a result string -let result = ` +// String literal syntax is used to concatenated a `result` string +result = ` Percentage students: ${percentageStudents}% Percentage mentors: ${PercentageMentors}% `; diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index a42fdb431..38afedbfe 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,4 +1,3 @@ -// Complete the function so that it takes input parameters function multiply(numOne, numTwo) { // Function to multiply two parameters together return (result = numOne * numTwo);