diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..fa9cd03993 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,9 @@ let count = 0; count = count + 1; +// The assignment operator (=) stores the result of count + 1 in the variable count. i.e. incrementing the count by 1. + // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + + diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..8860864100 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -1,11 +1,16 @@ +// Declare a variable called initials that stores the first character of each string. +// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. + let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; -// Declare a variable called initials that stores the first character of each string. -// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. +let initials = firstName[0] + middleName[0] + lastName[0]; + +console.log(initials); + +//I have run this code on chrome console and the answer was CKJ + -let initials = ``; -// https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28e..e9eb955ce2 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -15,9 +15,10 @@ const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable +const dir = filePath.slice(0, lastSlashIndex); + // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const ext = base.slice(base.lastIndexOf(".") + 1); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aabb..03d6abfd1c 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,22 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing + +//Expressions in JS are evaluated from inside out, following the brackets. So (maximum - minimum + 1) would become 100-1+1=100. +//Then we move on to the next outer bracket "Math.random()", which is a function designed to generate random numbers. We multiply that random number +//by 100 and that is our brackets calculated for now. Next in the expression is the function "math.floor" which rounds down numbers to the +//nearest whole number, to which we add the minimum at the end. Which is number 1. + +// I have run this code on my Chrome console multiple times and got a diffrent number each time. + +const minimum = 1; +const maximum = 100; + +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..a5a89d1c5f 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,10 @@ This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +We don't want the computer to run these 2 lines - how can we solve this problem? + +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? + +/* +This is just an instruction for the first activity - but it is just for human consumption +We don't want the computer to run these 2 lines - how can we solve this problem? +*/ diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..aa514f25a8 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,14 @@ const age = 33; age = age + 1; + +/* +Error here is that value for "const" cannot be reassigned. If we wanna see the code work +we must use "let" keyword because the variable can be changed. +*/ + +let age = 33; +age = age + 1; +console.log(age); + +//I have ran this code and got 34 on chrome console diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..0fbdc57f6f 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,8 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +//We must declare the variable first then use it, because JS reads from top to bottom. + +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..a79be603b4 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -7,3 +7,19 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + + +/* +The code won't work because the cardNumber is a numeric value and "slice()" works with strings in JS. +I have run the code to get "VM1691:2 Uncaught TypeError: cardNumber.slice is not a function" error. +I guess, JS is trying to find slice function on the cardNumber and since it is not a string it confirms +that it is in fact not a function. +*/ + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); + +console.log(last4Digits); + +//this code worked because i have converted the cardNumber value to string then sliced the last four digits. diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..28f5fe93fb 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,9 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; + +/* +I have predicted the code won't run due to the fact that "8:53pm" should be "08:53" +Got this error "Uncaught SyntaxError: Invalid or unexpected token", meaning it is a syntax error. +I have searched it up to find that variables can't start with numbers because it makes it easier +for JS to read code by removing a mathematical ambiguity. +*/ diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..211b889298 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -13,10 +13,31 @@ console.log(`The percentage change is ${percentageChange}`); // a) How many function calls are there in this file? Write down all the lines where a function call is made +/* +There are three function call names, but two functions have been made twice inside another, adding up to five function calls being made overall. +1. carPrice.replaceAll(",", "") +2. Number(carPrice.replaceAll(",", "")) this expression contain two, one inside the other +3. priceAfterOneYear.replaceAll(",", "") +4. Number(priceAfterOneYear.replaceAll(",", "")) this expression contains two as well +5. console.log(`The percentage change is ${percentageChange}`) +*/ + // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +//I have run the code and got a syntax error on line 5, due to two arguments not being seperated by a comma. + // c) Identify all the lines that are variable reassignment statements +//carPrice = Number(carPrice.replaceAll(",", "")); +//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + // d) Identify all the lines that are variable declarations +//let carPrice = "10,000"; +//let priceAfterOneYear = "8,543"; +//const priceDifference = carPrice - priceAfterOneYear; +//const percentageChange = (priceDifference / carPrice) * 100; + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +//Removing all the comma for calculation then convert the string into a number. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..b381bd4e12 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,26 @@ console.log(result); // a) How many variable declarations are there in this program? +// Six, every line that starts with "const" keyword. + // b) How many function calls are there? +// One, line 10. + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// It calculates how many seconds are left over. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// It first removes the leftover seconds from the total movie length, then converts the remaining seconds into minutes. + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// It represents a formatted time string and since it's a movie, i would change "result" variable to "movieDuration". + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// The code wouldn't work with decimal numbers "because movieLength" is a whole number, also negative numbers won't work +//because movies can't be less than zero. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..f19c3b2580 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,22 +1,35 @@ -const penceString = "399p"; +const penceString = "399p"; // This line shows price in pence with a letter "p" attached at the end. -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); + +const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); + +// Above line of code removes the "p", using "penceString.substring" function by removing the last character with "-1", +//out of a four digit string "399p". const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) +// We are using "padstart" method to ensure the string contains three characters at least, +//and if necessary to add "0" to the front. + + +const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); + +// We are calculating the pounds const variable by taking away the last two digits from "paddedPenceNumberString" const. + +const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); +//First we extracting the last two digits with "(paddedPenceNumberString.length - 2)", then making ensure the pence part +//has two digits always with "padEnd(2, "0")". Basically calculating the const pence variable. + console.log(`£${pounds}.${pence}`); +//Uses a template literal to combine the pounds and pence values into a currency format and prints the result to the console. + + + + + // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds