diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..2499f2681 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/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 +console.log(`${count}`); + +// line 3 is reassigning the value of count by changing it to count+1 +// and the = sign takes the value on the right and stores it into the variable on the left +// in this case in line 3 the the variable is assigned a new value telling it to take the old one and add 1 to it. diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..138e8fc56 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,7 @@ const 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. -const initials = ``; +const initials = firstName[0] + middleName[0] + lastName[0]; +console.log(`${initials}`); // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..032b8192a 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,10 @@ 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 = filePath.slice(filePath.lastIndexOf(".") + 1); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log(`The directory of ${filePath} is ${dir}`); +console.log(`The extension of ${filePath} is ${ext}`); + +// https://www.google.com/search?q=slice+mdn diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..6b84d89e3 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,9 @@ 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 +console.log(num); +// num is a random whole number between minimum and maximum. +// Math.random() gives a decimal between 0 and 1. +// Then we multiply by (maximum - minimum + 1) to get a range of possible values. +// Math.floor(...) turns that into a whole number. +// Finally, we add minimum so the number starts at minimum instead of 0. diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..5551734e8 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -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? + */ diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..ad624ecd4 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,7 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +/*const makes the variable fixed, so age cannot be changed. to reassign the value +I need to change const to let. +*/ +let age = 33; age = age + 1; diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..05b5adf42 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/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}`); +/* The log was trying to use cityOfBirth before the variable exists. + JavaScript reads the code in order, so the variable must be declared before it is used.*/ +// I logged the message before creating the cityOfBirth variable, so JavaScript couldn't find it yet. I need to declare the variable before using it. const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..e8418963c 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/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,12 @@ 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 + +//This won’t work because cardNumber is a number, and numbers don’t have the slice method. +//console.log(`${last4Digits}`); +// when I run it I see: TypeError: cardNumber.slice is not a function because slice() only works on strings or arrays. +// To fix it, I need to turn the number into a string first, then slice the last 4 characters. +const cardNumber = 453787178094213; +const last4Digits = String(cardNumber).slice(-4); + +console.log(last4Digits); diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..352d8d09b 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +/*const 12HourClockTime = "8:53pm"; +const 24hourClockTime = "20:53";*/ +//JavaScript does not allow variable names that start with digits. + +const twelveHourClockTime = "8:53pm"; +const twentyFourHourClockTime = "20:53"; diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..97ee4a04a 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/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; @@ -20,3 +20,29 @@ console.log(`The percentage change is ${percentageChange}`); // 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? + +// Answers: +/*a) in total there are 5 function calls in the file: +carPrice.replaceAll(",", "") +Number(carPrice.replaceAll(",", "")) +priceAfterOneYear.replaceAll(",", "") +Number(priceAfterOneYear.replaceAll(",", "")) +console.log(...) */ + +/* b) The problem is on line 5. replaceAll is missing the comma between its + two inputs, so JavaScript can’t read it properly. Once I add the comma, + the function works normally. correct version (priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));*/ + +/* c) The reassignment statements are: +-carPrice = Number(carPrice.replaceAll(",", "")); +-priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); */ + +/* d) The variable declaration are: +-let carPrice = "10,000"; +-let priceAfterOneYear = "8,543"; +-const priceDifference = carPrice - priceAfterOneYear; +-const percentageChange = (priceDifference / carPrice) * 100; */ + +/* e) the expression Number(carPrice.replaceAll(",","")) removes the commas from the price string + and then converts it into a number. This is done so the price can be used in + calculations like subtraction and division. */ diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..f72663a27 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,25 @@ 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 total: movieLength, remainingSeconds, totalMinutes, + remainingMinutes, totalHours, and result.*/ // b) How many function calls are there? +// -There is one function call: console.log(result); // 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 means find the reminder after dividing by 60. + It represents the number of seconds left over after all the full minutes have been removed. */ // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* -The expression assigned to totalMinutes removes the leftover seconds and then divides + the remaining total seconds by 60. This gives the total number of whole minutes.*/ // e) What do you think the variable result represents? Can you think of a better name for this variable? +/* -The variable result represents the movie length as a readable time string in the format hours:minutes:seconds. + A better name could be formattedTime or timeString.*/ // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +/* -This code works for normal values of movieLength that are non-negative whole numbers of seconds. + it does not work for negative values and decimal values.*/ diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..1f33b50d7 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -2,13 +2,13 @@ const penceString = "399p"; const penceStringWithoutTrailingP = penceString.substring( 0, - penceString.length - 1 + penceString.length - 1, ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); const pounds = paddedPenceNumberString.substring( 0, - paddedPenceNumberString.length - 2 + paddedPenceNumberString.length - 2, ); const pence = paddedPenceNumberString @@ -25,3 +25,15 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Step-by-step breakdown: +//const penceString = "399p"; creates a string containing the amount in pence, including the letter p. +/*const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + removes the final p so the string becomes "399".*/ +/*const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +makes sure the number has at least 3 characters by adding leading zeros if needed.*/ +/*const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + takes the part before the last 2 digits, which is the pounds value.*/ +/*const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +takes the last 2 digits, which are the pence value, and makes sure they are 2 digits long.*/ +/*console.log(`£${pounds}.${pence}`); prints the final money value in pounds format, like£3.99`.*/