diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..c3cce97875 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/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 + +// Martin answer - line 3 is updating the value of the variable count by one and reassigning it back to the variable. This can also be written in shorthand using an increment operator - count++ diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..35bbec730e 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]}`; +console.log(initials); // 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..ffe9ecca29 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,9 @@ 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 ext = filePath.slice(filePath.lastIndexOf(".")); +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 diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aabb..e47dcc709a 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/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 + +/* +Martin answer - num is a random integer between minimum and maximum. Math.random() generates a decimal between (0,1), which is rounded down to the nearest integer by the Math.floor method. After multiplication the range is scaled up. Math.random is generating a random number up to 100 - +Math.floor(Math.random() * (maximum - minimum + 1) → [0, 100)) +Adding minimum - Math.floor(Math.random() * (maximum - minimum + 1)) + minimum - shifts the range up by 1 and the maximum number is now included in the range [mimimum, maximum] eg [1, 100] +*/ diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..e6b744a05b 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? diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..00370306fb 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,6 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; + +// Martin comment - const cannot be reassigned. I have changed the declaration to let which allows the value of age to be reassigned diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..3493d57343 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 ? -console.log(`I was born in ${cityOfBirth}`); +// Martin response - cityOfBirth was being logged before it was declared. I have moved the variable initialisation above the console.log line and this now works as expected. + 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 ec101884db..df7cc8b06b 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = (cardNumber % 10000).toString().padStart(4, "0"); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +8,7 @@ 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 + +// Martin comment before running the code - Slice is a string method. In this case it fails because it is being used against a number + +// Martin comment after running the code - when I run the code it gives the error - 'number.slice is not a function'. When I research online I discover this error occurs when the slice method is applied to a value that is not a string or an array. I have resolved this by doing research on line and discovering that modulo can be used to extract digits from a long number. By converting it to a string and adding a padStart of 4 characters, I ensure that 4 string characters are returned even in the event of a number such as 12340000 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..fc7ccccfba 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const _12HourClockTime = "8:53pm"; +console.log(_12HourClockTime); +const _24hourClockTime = "20:53"; +console.log(_24hourClockTime); + +// Martin comment - a variable cannot begin with a number. I have converted both to start with an underscore diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..a12ca6beb3 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; @@ -13,10 +13,21 @@ console.log(`The percentage change is ${percentageChange}`); // a) How many function calls are there in this file? Write down all the lines where a function call is made +// Martin answer - functions calls are on lines - 4, 5 with the use of the Number function + // 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? +// Martin answer - There is a syntax error on line 5, comma missing between current item and replacement item in replaceAll method + // c) Identify all the lines that are variable reassignment statements +// Martin answer - lines 4 and 5 + // d) Identify all the lines that are variable declarations +// Martin answer - lines 1, 2, 7 and 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +/* +Martin answer - the purpose of this expression is to use the replaceAll method using a search string and replacement string. In this case all commas are removed from carPrice (",") and replaced with a blank (""), converting the formatted number of "10,000" to "10000". This is passed into a Number() method which converts the string to a number - 10000 +*/ diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..6ba26841e8 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,30 @@ console.log(result); // a) How many variable declarations are there in this program? +// Martin answer - there are 6 variable declarations on lines 1, 3, 4, 6, 7, 9 + // b) How many function calls are there? +// Martin answer - there are no function calls + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Martin answer - the expression movieLength % 60 uses the modulo operator which extracts the value remaining seconds after movieLength is divided by 60 + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Martin answer - the expression totalMinutes = (movieLength - remainingSeconds) / 60 subtracts remaining seconds from movieLength (total length of movie in seconds) and divides the result by 60 (representing seconds in one minute) to return the total number of minutes + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Martin answer - result represents a formatted express of the movie duration as hours, minutes and seconds. A better name for this variable is formattedMovieDuration + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +/* - Martin answer - it works for numerical numbers I have tried, positive and negative, but in the real world you would want to prevent entry of negative numbers. +I have tried adding trailing zeros (8784.00), but JavaScript automatically converts them to a number - 8784. +I have tried converting it to a string ("8784" or "8784.00"), but I get the same results as JavaScript automatically converts them to a number. +Enter text returns all values as NaN - NaN:NaN:NaN + +*/ diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..725fb71456 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring( ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 @@ -25,3 +26,17 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// MARTIN ANSWERS BELOW +/* + 1. const penceString = "399p" - initialises a string variable with the value of "399p" + 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1) - removes the trailing p so that the value of penceStringWithoutTrailingP is "399" + 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") pads the value to a minimum of 3 spaces, with 0 being added if the value is less than 3 characters. In this case holding there are 3 characters - "399". + 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); - uses the substring method to extract the first value from paddedPenceNumberString and assign it to the variable pounds. In this case the first value is "3" + 5. const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + - this uses the substring method to extract the last 2 values in paddedPenceNumberString, followed immediately with the padEnd method which adds zeros if the number of characters is less than 2 spaces. In this case, the value of "99" is assigned to the pence variable + + 6. console.log(`£${pounds}.${pence}`) is a log of the variables pounds and pence in a formatted string which will print the value - "£3.99" +*/