diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..7737ea4fd 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ 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 +// The '=' assignment operator. It assigns the value on the right hand side to the variable on the left. +// The variable 'count' is initially assigned the value of 0. +// And the result is assigned back to count. +// This mean 'count; is updated each time this line runs. \ 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..919ba4ca5 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,11 @@ 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]; +console.log(initials); // https://www.google.com/search?q=get+first+character+of+string+mdn - +// Explaning the solution: I assign 'initials' to a new string variable made by combining the +// first character of 'firstName', 'middleName' and 'lastName'. +// Strings in JavaScript are indexable, so using [0] accesses the first +// character of each string. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..c15498357 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -12,12 +12,19 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); +const lastDotIndex = filePath.lastIndexOf("."); 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(lastDotIndex + 1); -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +console.log("Dir:", dir); +console.log("Ext:", ext); +// https://www.google.com/search?q=slice+mdn +// Explaining the solution: lastIndexOf() is used to locate boundaries (/ and .) +// slice() is used to extract the required parts of the string. +// No values are hard-coded, so the solution works for similar file paths. +// The result correctly seperats the path into dir and ext. \ 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..abe8b41f5 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -2,8 +2,13 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - +console.log(num); // 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 +// Explaning the solution: console.log(num); returns a number. +// Upon investigation Math.Floor - returns and whole int that is the less than or equal to its numeric argument. +// Math.randon - returns a random float/int +// Now putting my understanding together - I understand that the const num when logged using console.log returns a value in this order +// A number at random is chosen and is multiplied with the maximum value (of that number) - the minimum value is then added to one (+ 1) and then added to the minimum of the total. \ 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 cf6c5039f..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? \ 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 7a43cbea7..102667f82 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = Math.floor(Math.random()); age = age + 1; +console.log(age); +// Explaning the solution: Assigning age to a variable that is not constant - then utalising Math.Floor and Math.Random to assign age a whole random number. +// I then allow age to be added to 1 +// I then log to the console to verify the age. \ 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 e09b89831..a7401245d 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // 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}`); \ 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 ec101884d..aec264a30 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,12 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Before running the code, make and explain a prediction about why the code won't work // 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 may not work due to the const variable being an int. The slice method usually works on strings. +// Updating the const last4Digits with .toString() allowed the const cardNumber to convert from int to string and therefore the solution works as expected. diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..b5e407f64 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; + +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); +//Explaining the solution: The issue is that the syntax to assign a const var needs to be a string. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..c093ce682 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,24 @@ 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? +// 1) carPrice.replaceAll(",", "") +// 2) Number(carPrice.replaceAll(",", "")) +// 3) priceAfterOneYear.replaceAll(",", "") +// 4) Number(priceAfterOneYear.replaceAll(",", "")) +// 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? +// The syntax error is originating on line 5, this error occurs because replaceAll is missing ',' in between the arguments - priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , ""));. // 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 +// The lines that are variable declarations use let or const: let carPrice, let priceAfterOneYear, const priceDifference and const percentageChange. // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// This particular expression is doing two steps. First it takes "10,000" and removes the commas so the output then is "10000" +// The second step converts the the string into a number - this is likely because the next function then subtracts the number to find the difference between +// carPrice and priceAfterOneYear and stores it within priceDifference variable declaration. \ 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 47d239558..59d19202c 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 = -4382.3817; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,32 @@ 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? +//This program has 6 variable declarations: +//const movieLength = 8784; +//const remainingSeconds = movieLength % 60; +//const totalMinutes = (movieLength - remainingSeconds) / 60; +//const remainingMinutes = totalMinutes % 60; +//const totalHours = (totalMinutes - remainingMinutes) / 60; +//const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; // b) How many function calls are there? +// There is 1 function call within this program +//1) 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 modulus operator returns the remainder after dividing movieLength by 60. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// remainingSeconds removes leftover seconds +// movieLength - remainingSeconds leave only full seconds +// Dividing by 60 converts seconds to minutes +// This expression converts the total movie length from seconds into whole minutes, excluding any leftover second. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// The variable result represents the formatted movie duration into hours, minutres and seconds. +// A better variable name may be: movieDuration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Upon experimenting with the const movieLength I have tested edge cases. The code works well for positive integer values. +// Handling edge cases such as negative value, floats lead to confusing results. \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..214cbf234 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,23 @@ 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); +// penceString.length -1: points to the index just before the last character. The substring(0, ect) takes everything from the start up to the last character (but not including the last character) +// The result removes the trailing "p" so the output becomes "399p" --> "399" + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");: +// Ensures the pence int has at least 3 characters by adding leading zeros if required. +// This is useful for values under 100p so the pounds/pence split works accurately. + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// This takes everything except the last two digits +// The last two digits represent the pence, so whatever is left over from the calculation is the pounds. + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// The substring(paddedPenceNumberString.length - 2) takes the last two characters of the padded string (e.g. "399") and extacts the pence portion "99" +// The .padEnd(2, "0") ensures the pence part is always 2 digits by adding trailing zeros if needed. +// In most cases there are already two digits, however part of the code protects against outputs such as "3" becoming "30" which would be an inaccurate outcome. + +// 6. console.log(`£${pounds}.${pence}`); +// Prints the final formatted price in pounds and pence. An example would be "399p" would print as "£3.99 \ No newline at end of file