diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..deb173eb5 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,14 @@ let count = 0; -count = count + 1; +count = count + 3; // 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 + +// Line 3 is updating the value of the count variable by adding 1 to its current value. The = operator +// is used to assign the new value (count + 1) back to the count variable. +// we can also say that the = operator is taking the value on the right side (count + 1) and storing it +// in the variable on the left side (count). + +// We can see the result of our code by logging the count variable to the console +console.log(count); \ 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 47561f617..f95a9ff86 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,14 @@ 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 = ``; +// You can use the .charAt() method of strings to get a character at a specific position in a string. +// The first character is at position 0, the second at position 1, and so on. + + +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); // https://www.google.com/search?q=get+first+character+of+string+mdn +// Log the initials variable to the console +console.log(initials); + diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..474f3b9ce 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,20 @@ 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 = ; +// The dir part is everything before the base part +// The ext part is everything after the last "." in the base part + +// You can use the .lastIndexOf() method of strings to find the position of the last occurrence of a specific character in a string. +// For the dir part, you need to find the position of the last "/" in the filePath string. +// For the ext part, you need to find the position of the last "." in the base string. + +const dir = filePath.slice(0, lastSlashIndex + 1); // this will include the last "/" +// if you don't want the last "/", use lastSlashIndex instead of lastSlashIndex + 1 + +const ext = base.slice(base.lastIndexOf(".")); // this will include the "." +// if you don't want the ".", use filePath.lastIndexOf(".") + 1 instead of filePath.lastIndexOf(".") + +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); // 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 292f83aab..c68fec229 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -4,6 +4,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? +// num is a variable that stores a random integer between minimum and maximum. +// the num variable is calculated using the Math.random() function, which generates a random floating-point number between 1 and 100. + // Try breaking down the expression and using documentation to explain what it means +// const num stores the result of the expression on the right-hand side of the equals sign +// the = expression is used to assign a value to a variable +// the expression (maximum - minimum + 1) calculates the range of numbers between minimum and maximum. +// adding minimum shifts the range to start from the minimum value instead of 0. + // It will help to think about the order in which expressions are evaluated +// 1. (maximum - minimum + 1) is evaluated first, resulting in 100 +// 2. Math.random() is called, generating a random floating-point number between 0 (inclusive) and 1 (exclusive) +// 3. The result of Math.random() is multiplied by the result of (maximum - minimum + 1), scaling the random number to the desired range +// + // Try logging the value of num and running the program several times to build an idea of what the program is doing +// The running the the program several times you can see that the program is generating a random number between 1 and 100. + +console.log(num); diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..a7e08e8a9 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,7 @@ -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? */ + +// we can use // to comment out a single line +// or we can use /* to start a multi-line comment +// and end it with */ + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..4e3d5f101 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +// to reassign the value of age by 1 we need to use let instead of const + +console.log(age); + diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..3ab420de6 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,7 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? +// we need to declare the variable before we use it in the console.log statement + -console.log(`I was born in ${cityOfBirth}`); 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 ec101884d..cce95a15d 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,21 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber + // However, the code isn't working +// the error is that slice is not a function for numbers, it is a function for strings and arrays + // Before running the code, make and explain a prediction about why the code won't work +// My prediction is that the code will give an error because slice is not a function for numbers + // Then run the code and see what error it gives. +// typeError: cardNumber.slice is not a function + // Consider: Why does it give this error? Is this what I predicted? If not, what's different? +// It gives this error because slice is not a function for numbers, it is a function for strings and arrays. This is what I predicted. + // Then try updating the expression last4Digits is assigned to, in order to get the correct value +const cardNumberString = cardNumber.toString(); +const last4DigitsCorrected = cardNumberString.slice(-4); +console.log(last4DigitsCorrected); // Should print 4213 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..14efa7127 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,12 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const a24HourClockTime = "20:53"; +const a12hourClockTime = "08:53"; + +// The 12HourClockTime variable should store the time in 12-hour format +// The 24hourClockTime variable should store the time in 24-hour format + +// However, the code isn't working +// the error is that the variable names are not valid because they start with a number +// Variable names cannot start with a number + +console.log(a24HourClockTime); // Should print: 08:53 +console.log(a12hourClockTime); // Should print: 20:53 diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..d0a5eee94 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; @@ -12,11 +12,20 @@ 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 +// there are 5 function calls in this file, they are on lines 4, 5, and 10 +// function calls: replaceAll(), replaceAll(), Number(), Number(), console.log() // 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 was coming from line 5 there was a comma missing between the 2 quotations ("," "") by adding the comma the code was fixed and it worked as it should. // c) Identify all the lines that are variable reassignment statements +// the variable reassignment statements are on lines 4 and 5 +// variable reassignments: carPrice = Number(carPrice.replaceAll(",", "")), priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")) // d) Identify all the lines that are variable declarations +// the variable declaration statements are on lines 1, 2, 7 and 8 +// variable declarations: let carPrice, let priceAfterOneYear, const priceDifference, const percentageChange // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* the expression is taking the variable carPrice and replacing all the commas in the string with nothing and then converting +the string into a number so that it can be used in calculations.*/ diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..6dcd7d532 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 7784; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -6,20 +6,30 @@ const totalMinutes = (movieLength - remainingSeconds) / 60; const remainingMinutes = totalMinutes % 60; const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +const result = `${String(totalHours).padStart(2, "0")}:${String( + remainingMinutes +).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; 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, 4, 6, 7 and 9 // b) How many function calls are there? +// there is 1 function calls in this program. And it is on 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 +// the expression movieLength % 60 is using the modulus operator (%) to find the remainder when movieLength (8784) is divided by 60. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* line 4 is calculating the total number of minutes in the movie by subtracting the remaining seconds from the total movie length in +seconds and then dividing that result by 60.*/ // e) What do you think the variable result represents? Can you think of a better name for this variable? +/* the variable result represents the total length of the movie in hours, minutes and seconds. A better name for this variable could +be formattedmovieDuration.*/ // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// yes this code will work for all values of movieLength as long as the value is a non-negative integer representing the length of a movie in seconds. diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..123e48586 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,21 +1,21 @@ -const penceString = "399p"; +const penceString = "1399p"; // stores the price in pence as a string with a trailing 'p' const penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1 -); +); // removes the trailing 'p' from the string to isolate the numeric part const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 -); +); // extracts the pounds part by taking all but the last two characters const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); + .substring(paddedPenceNumberString.length - 2) // extracts the last two characters as the pence part + .padEnd(2, "0"); // ensures the pence part is two digits by padding with '0' if necessary -console.log(`£${pounds}.${pence}`); +console.log(`£${pounds}.${pence}`); // outputs the formatted price in pounds and pence // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds