diff --git a/Prep b/Prep new file mode 160000 index 000000000..4035ae055 --- /dev/null +++ b/Prep @@ -0,0 +1 @@ +Subproject commit 4035ae055563c1874746d5640e42517e5dc645ab diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..6b5382e30 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 + +// = in line 3 is assigning the rule that counting must start at 0. \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..c8effcf6f 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,11 @@ 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..5f4d04e89 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); +console.log(dir); +const lastDotIndex = base.lastIndexOf("."); +const ext = base.slice(lastDotIndex + 1); +console.log(ext); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..145b5237e 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -7,3 +7,8 @@ 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 represents any randomly generated number between 1 and 100. +// Math.random pulls a random decimal, Math.floor rounds down to nearest whole number, (maximum - minimum + 1) sets the range, adding the minimum of 1 at the end allows for the possibility of a random number being 100 instead of 99. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..fb40d0b61 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; +let age = 33; age = age + 1; + +console.log(age); + diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..c441b2e77 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,6 @@ // 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}`); + diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..aeedfcf79 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 last4Digits = `${cardNumber}`.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,8 @@ 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 + +// Card Number is declared as a number and not a string, so slice won't work. +console.log(last4Digits); +// Yes this is what I predicted. The error is - cardNumber.slice is not a function. +// I used a template literal because it will automatically convert the number to a string before slicing. diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..c7690b0fa 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 twelveHourClockTime = "8:53pm"; +const twentyFourhourClockTime = "20:53"; + +console.log(twelveHourClockTime); +console.log(twentyFourhourClockTime); + diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..627ebb163 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; @@ -12,11 +12,27 @@ 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. +// carPrice = Number(carPrice.replaceAll(",", "")); +// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +// 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? +// The error is coming from line 5. There is a separator missing from the function call. Insert a comma to fix the problem. // c) Identify all the lines that are variable reassignment statements +// Line 4 carPrice = Number(carPrice.replaceAll(",", "")); +// Line 5 priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); // d) Identify all the lines that are variable declarations +// Line 1 let carPrice = "10,000"; +// Line 2 let priceAfterOneYear = "8,543"; +// Line 7 const priceDifference = carPrice - priceAfterOneYear; +// Line 8 const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// carPrice.replaceAll(",", "") removes every comma from the string, turning "10,000" into "10000". +// Number(...) then converts that string into the number 10000. +// The purpose is to turn a formatted price string into a real number so it can be used in calculations. +// Without the commas removed, Number("10,000") would return NaN. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..985e2d411 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. // b) How many function calls are there? +// There is 1 function call. // 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. It returns what is left over after dividing one number by another. +// movieLength % 60 gives the seconds left over once all the full minutes are taken out of the movie length. +// 8784 % 60 = 24, because 8784 seconds is 146 full minutes with 24 seconds left over. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// It subtracts the leftover seconds from the movie length, leaving a number that divides evenly by 60 +// (8784 - 24 = 8760), then divides by 60 to get the number of whole minutes (8760 / 60 = 146). +// So totalMinutes is the total number of complete minutes in the movie. // 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 would be movieLengthHHMMSS. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..3f0e152b3 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,28 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// 3. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1): +// Takes the characters from index 0 up to (but not including) the last character. +// penceString.length is 4, so this is substring(0,3), resulting in "399". +// The purpose is to remove the "p" so only digits remain. + +// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") +// Adds "0"s to the start until the string is at least 3 characters long. +// The purpose is to guarantee at least one digit for pounds and two digits for pence. + +// 9. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); +// The purpose is to make the pounds all the digits before the last two, by taking everything except the last two characters: substring(0,1), giving "3" + +// 14. const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// substring(1) takes from index 1 to the end, giving "99". +// padEnd(2, "0") adds "0"s to the end until it is two characters long, so "99" stays "99". +// The purpose is too ensure the pence are always the last two digits. +// The padEnd does not change anything here since line 8 already guarantees at leas 3 characters, so the last two characters always exist. + +// 18. console.log(`£${pounds}.${pence}`); +// Call function using a template literal to combine "£", the pounds, a ".", and the pence, then prints "£3.99". + + + +