diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..58314b8bb0 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,8 @@ count = count + 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 +// This line (line 3), is updating the value of the count variable. The = operator is an assignment +// operator which assigns the value on the right (count + 1) to the variable on the left (count). +// count = count + 1; +// This operation is called increment. +// An increment means increasing the value of a variable by 1. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..0238a71c60 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,7 @@ 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 = ``; +let initials = firstName[0] + middleName[0] + lastName[0]; // 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..98ab371c3f 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex); +const ext = base.slice(base.lastIndexOf(".")); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aabb..59d1d93fd1 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,16 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 + +// Math.random returns a random number between 0 (inclusive) and 1 (exclusive). +// const minimum = 1; const maximum = 100; +1 so, 100 - 1 + 1 = 100 +// Math.random() * 100 means the decimal number will be multiplied by 100, so the range of possible values is from 0 to 99.9999... +// Math.floor rounds the number down to the nearest whole number, so the possible values are from 0 to 99. +// Eg. 12.9 becomes 12, 0.3 becomes 0, 99.9 becomes 99. +// Brackets(), Multiply*, Function(Math.floor), Addition+ +// 1) Pick a random number between 0 and 1, eg. 0.5 +// 2) Multiply that number by 100, eg. 0.5 * 100 = 50 +// 3) Round that number down to the nearest whole number, eg. Math.floor(50) = 50 +// 4) Add 1 to that number, eg. 50 + 1 = 51 + +// num represents a random whole number between 1 and 100. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..9b8f503446 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ -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 +// 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? +// The forward slash is used to indicate a comment in JavaScript, so the computer will ignore +// these lines when running the program. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..4d0c1ffd86 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,10 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); + +// The code above will output 34, because we are reassigning the value of +// age to be the current value of age plus 1. So, 33 + 1 = 34. +// The error in the code is that it was trying to reassign a value to a variable +// that was declared with let. This is not allowed in JavaScript. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..88686319d1 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,8 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + +// The error happens because cityOfBirth is used before it is defined. JavaScript runs +// code from top to bottom, so the variable must be declared first. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..1b749ecae0 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,14 @@ 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 + +// Prediction: The code will not work because slice() is being used on cardNumber, which is a number. +// The slice() method only works on strings or arrays, not numbers. +// So I predict JavaScript will throw an error saying that slice is not a function for a number. + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); + +// After running the code, I got the output "4213", which is the last 4 digits of the card number. +// This is what i expected. diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d1..13a18b94cc 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,11 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// Variables cannot start with a number. +// When JavaScript tries to run the code, it throws a SyntaxError. +// To fix the error we can rename the variable to start with a letter, a underescore_ or a dollar sign $ instead of a number. + +const twelveHourClockTime = "8:53"; +const twentyFourHourClockTime = "08:53"; + +// I changed the time to match the 12 and 24 hr clock format. diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..7ba5517292 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -11,12 +11,49 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below -// a) How many function calls are there in this file? Write down all the lines where a function call is made +// a) How many function calls are there in this file? Write down +// all the lines where a function call is made + +// There are 5 function calls in this file. They are on lines 1, +// 2, 4, and 5. The functions being called are replaceAll() and Number(). +// Line 4: replaceAll and Number (2) +// Line 5: replaceAll and Number (2) +// Line 10: console.log (1) + + + +// 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? + +// The error is occuring on line 5. It is occuring because there is a +// missing comma between the replaceAll "". +// We can fix this by adding a comma between the two empty strings in +// the replaceAll function on line 5, like this: +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + -// 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? // 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 -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Lines 1,2,7 and 8. +// 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? +// carPrice.replaceAll(",", "") - Replaces all commas "," in the string with nothing "". +// e.g "10,000" becomes "10000". +// Number() - Converts the resulting string "10000" into the number 10000. +// The purpose of this is to take a string with commas (like "10,000") and turn it into a +// number (10000) that we can do math with. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..c66c6a6929 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,25 +1,73 @@ -const movieLength = 8784; // length of movie in seconds +//const movieLength = 8784; // length of movie in seconds -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; +//const remainingSeconds = movieLength % 60; +//const totalMinutes = (movieLength - remainingSeconds) / 60; -const remainingMinutes = totalMinutes % 60; -const totalHours = (totalMinutes - remainingMinutes) / 60; +//const remainingMinutes = totalMinutes % 60; +//const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -console.log(result); +//const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +//console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? +// There are 6 variable declarations in this program. They are on lines 1, 3, 5, 7, 9 and 11. The variables being +// declared are movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result. + + + // b) How many function calls are there? +// There is 1 function calls in this program. It is on lines 10. The functions is the console.log(). + + + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// % is the remainder operator. +// movieLength % 60 - This gives the remainder when movieLength is divided by 60. +// Since there are 60 seconds in a minute, this tells us how many leftover seconds there are after counting full minutes. +// For example, 8784 % 60 = 24. So there are 24 seconds left after counting the minutes. + + + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// movieLength - remainingSecond - This gives the total number of seconds in the movie after removing the leftover seconds. +// Divide by 60 - this converst the seconds into total full minutes. eg. (8784 - 24) = 8760, 8760 / 60 = 146. + + + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result is a string that represents the movie length in hours:minutes:seconds +// For example: 2:26:24 (2 hours, 26 minutes, 24 seconds) +// Better name can be movieDurationString. + + + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds +// for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example, +// if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it +// may also cause issues with the calculations. + +const movieLength = 65; + +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const formattedHours = String(totalHours).padStart(2, "0"); +const formattedMinutes = String(remainingMinutes).padStart(2, "0"); +const formattedSeconds = String(remainingSeconds).padStart(2, "0"); + +const movieDurationString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; + +console.log(movieDurationString); // 00:01:05 diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..307daeab2d 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,14 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that contains the +// original string without the last character (the "p"). +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): creates a new string variable +// that pads the previous string with leading zeros until it is at least 3 characters long. eg. "45" becomes "045" and "7" becomes "007". +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): creates a new string variable that contains +// the first part of the padded string, which represents the pounds. It takes all characters except the last two. eg. "045" becomes "0" and "399" becomes "3". +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): creates a new string variable that contains the +// last two characters of the padded string, which represents the pence. It also pads it with trailing zeros if necessary to ensure it is 2 characters long. +// eg. "045" becomes "45" and "7" becomes "70". +// 6. console.log(`£${pounds}.${pence}`): outputs the final price in pounds and pence format to the console. For example, if penceString is "399p", +// it will output "£3.99". If penceString is "45p", it will output "£0.45". If penceString is "7p", it will output "£0.07".