diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..d94416aed 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 + +/* +it is making count be the sum of count + 1. +For example if this is part of a loop it will allow count to keep adding 1 each time the loop runs. +*/ \ 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..58487de2d 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -5,6 +5,6 @@ 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.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1); // 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..9247b2fcc 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ 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, filePath.lastIndexOf("/") + 1); +const ext = filePath.slice(-4); // 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..02fd9eb99 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -3,7 +3,15 @@ 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 +/* +Firstly its not running as it is showing as undefined as it is lacking something like console.log +After adding console.log(num) and running it a few times I get 62, 45, 35, 10. +For what it is doing: Math.floor is the sum of a random number from 0 to 0.9999... which is then multiplied by the maxium - minimun + 1, so 100 - 1 + 1. + That gives a number from 0-99, then then adds the minimun which is 1, so that gives a random number from 1-100. +*/ \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..a5a9b16b3 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ 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 +We don't want the computer to run these 2 lines - how can we solve this problem? + +// giving SyntaxError +// You need to add // to each line or /* before the first line and */ after the last one to make it a comment. \ 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..7d61b7dd4 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -2,3 +2,11 @@ const age = 33; age = age + 1; + +// this gives a TypeError +// age is already a const at the start and calling it again just as age wont work, naming it like "older" would work better: + +// const age = 33; +// let older = (age + 1); +// console.log(older) + diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..f389327b6 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -3,3 +3,8 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +//ReferenceError is happening as the const is after the console.log request for it, so the order is wrong. + +// 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..efd5f0ad6 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,16 @@ 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 + +/* +cardNumber is giving a number and that can't be used for slice, could not remember the name for that error at this time. +after running the code it shows thats a TypeError. cardNumber.slice is not a function +*/ + +const cardNumber = 4533787178994213; +let number = cardNumber.toString() +const last4Digits = number.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..4389885a7 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,8 @@ const 12HourClockTime = "8:53pm"; const 24hourClockTime = "20:53"; + +// This is a Syntax error as cant have numbers for an identifier at the start of it. +// Changing to say: time12HourClock would work + +// const time12HourClock = "8:53pm"; +// const time24hourClock = "20:53"; \ No newline at end of file diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..f050f3c7a 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -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 + // 5 times its been called: + // 4 and 5 for Number and replaceAll + // 10 for 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? + // SyntaxError, its from line 5 in replaceAll("," "") + // its missing a , between the two arguments so it needs to be (",", "") // c) Identify all the lines that are variable reassignment statements + // There are two reassignment statements in lines 4 and 5 // d) Identify all the lines that are variable declarations - + // They are 1,2 and 7,8 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + // It is using Number to make the string into a number but in order to do that the string needs to contain numerical letters only. + // So it then uses in the () to remove all of the , with nothing using the replaceAll function so that 10,000 becomes 10000 \ 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..0ddede88e 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -1,25 +1,32 @@ -const movieLength = 8784; // length of movie in seconds +// const movieLength = 8784; // length of movie in seconds -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; +// const remainingSeconds = movieLength % 60; +// const totalMinutes = (movieLength - remainingSeconds) / 60; -const remainingMinutes = totalMinutes % 60; -const totalHours = (totalMinutes - remainingMinutes) / 60; +// const remainingMinutes = totalMinutes % 60; +// const totalHours = (totalMinutes - remainingMinutes) / 60; -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -console.log(result); +// const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +// 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, lines 1,3,4,6,7,9 // b) How many function calls are there? + //1 at 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 + //This is using a remainder, this us seeing how many times 8784 goes into 60 and what is remaining which is 24 in this case: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder // d) Interpret line 4, what does the expression assigned to totalMinutes mean? + // It is the sum of movieLength minus remainingSeconds then divided by 60 // e) What do you think the variable result represents? Can you think of a better name for this variable? + // its showing the length of the movie in hours, minutes and seconds, could be better called something like MovieDuration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + //Yes it will as long as the input is numerical numbers only, it does not work with any extra symbols, spaces or letters. diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..db4abf51e 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,24 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +/* +3-6. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1) + This is taking penceString and removing the p from it so its just a string with the numerical letters only. + +8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + This is taking penceStringWithoutTrailingP ensuring that the minimum length of the numerical string is at least 3 numbers. + It is filling out with 0s from the front so if it was 3 it would have 003 + +9-12. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + this is taking paddedPenceNumberString and removing the last 2 numerical numbers from the string/ + This takes the 399 and makes it 3 + + +14-16. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + This is taking paddedPenceNumberString and first reduces the length to the last 2 numbers in the string. + It then uses padEnd to ensure that there is a at least two 00 if no numbers are present. + +18. console.log(`£${pounds}.${pence}`); + This shows the pounds and pence with a £ at the start and a . between them. + making this show the pence to pounds as £3.99 +*/ \ No newline at end of file