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 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 diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..adab86dd4 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,8 @@ -// Start by creating a variable `greeting` +// Program to print a `greeting` variable in the console 3 times +// Tried to reduce code repetition by using a for loop -console.log(greeting); +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 2cffa6a81..cf890fb61 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,28 @@ -// Start by creating a variable `message` +// 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 and has a 3:1 chance of printing the expected result :) -console.log(message); +// Declare and initialise variables. +let messageChoice = 0; +let messageType; +let message = ""; + +// Set `messageChoice` variable to a random number between 0 and 2 rounded to the nearest integer. +messageChoice = Math.round(Math.random() * 2); + +// Determine which message to display using the random `messageChoice` value. +if (messageChoice == 0) { + message = "This is a string"; +} else if (messageChoice == 1) { + message = 25.5; +} else { + message = true; +} + +// Sets `messageType` variable equal to the variable `message` type (string, number or boolean). +messageType = typeof message; + +// Outputs the selected `message` and `messageType` +console.log(` +${message} +${messageType}`); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..0b16f8c9e 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,10 @@ -// Start by creating a variable `message` +// Program which states my first name to practice concatenation. -console.log(message); +// Set and initialise variables and constants +const MY_NAME = "Andrew"; +let greetingStart = "Hello, my name is "; +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 2cffa6a81..e50423e6e 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,19 @@ -// 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 + +// Declare and initialise variables and constants +const MY_NAME = "Andrew"; +const NAME_LENGTH = MY_NAME.length; +let message = ""; + +// Update Message & output message +message = + "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 b4b46943d..03b78adaa 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,18 @@ -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 ". + +// Declare and initialise variables and constants +const MY_NAME = " Daniel ".trim(); +const NAME_LENGTH = MY_NAME.length; +let message = ""; + +// Update Message & output message +message = + "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 49e7bc00b..c18c95aaf 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,19 @@ -// 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`. + +// Declare and initialise variables +let numberOfStudents = 15; +let numberOfMentors = 8; +let totalStudentsAndMentors = 0; +let message = ""; + +// 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}`; + +console.log(message); diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..2ab067171 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,29 @@ -var numberOfStudents = 15; -var numberOfMentors = 8; +// 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 = ""; + +// 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); + +// 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. +percentageStudents = Math.round(percentValue * numberOfStudents); +PercentageMentors = Math.round(percentValue * numberOfMentors); + +// String literal syntax is used to concatenated a `result` string +result = ` +Percentage students: ${percentageStudents}% +Percentage mentors: ${PercentageMentors}% +`; + +// Output the result to the console +console.log(result); 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)); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..38afedbfe 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,9 +1,13 @@ -// 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)); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..1ea61b3aa 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -1,5 +1,16 @@ -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 (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"]; + +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)); +}