diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..f7dc1422d 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ 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 +//Line 3 is 'assigning' count to a value of the original value of count, plus one. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..d5ce8cd7c 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,8 @@ 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 ab90ebb28..c17d9b019 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,16 @@ 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 = ; +//To get the directory path we have to get a slice between two point, +//the beginning and the start of the last slash index which is stored +//in the variable lastSlashIndex.""" + +const dir = filePath.slice(0, lastSlashIndex); +console.log(`The directory part of ${filePath} is "${dir}".`) + +// To get the directory path we have to create last index of . as well +let lastDotIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDotIndex) ; +console.log(`The extension part of ${filePath} is "${ext}".`) // https://www.google.com/search?q=slice+mdn \ 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..8afcf2e84 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -6,4 +6,10 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 +// - Math.random() will create a floating point (decimal) between 0 and 1 but always less than 1. +// - Math.floor() gets the decimal multiplied by 100 and strips it down to the nearest whole number (integer) + // Try logging the value of num and running the program several times to build an idea of what the program is doing +// The first part of the num expression ensures we have a two digit number between 0 and 99. And the best we can get is 99. +// The addition of the minimum, 1 in this case ensures num can also be 100. +// This ensures that both the "minimum" and "maximum" value can be part of the range of generated answers for the "num" variable. \ 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..25547ec84 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,4 @@ -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? + +//we can solve this by simply using the double forward slash (quote sign) \ 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..90a005dc2 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,6 @@ const age = 33; age = age + 1; + +//TypeError: Assignment to constant variable. +// The second line clearly will not work since age was declared to be constant and CANNOT change. \ 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..4e0a6401e 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/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 ? +// It is a reference error and it is as a result of the variable being accessed before it is created +// The first line shoulfd come before the second line -console.log(`I was born in ${cityOfBirth}`); 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 ec101884d..622f85f38 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,14 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.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 + +// The code won't work because the variable "cardNumber" is not a string. +// Since it is a constant variable, we will have to change directly by putting quote signs around it to make it a string. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..463328611 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,10 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + +// In the above, error will occur as Variable names are not allowed to start with a number. +// This means that we must modify the names slightly. + +const Hour24ClockTime = "20:53"; +const Hour12ClockTime = "08:53"; + +console.log(Hour24ClockTime) \ 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..6ea864f52 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; @@ -12,11 +12,14 @@ 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 three; Ln4, Ln5, Ln10. // 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 as a result of the two arguments in Ln5 not being seperated // c) Identify all the lines that are variable reassignment statements - +// Ln4, Ln5, // d) Identify all the lines that are variable declarations +// Ln1, Ln2, Ln7 and Ln8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// The purpose is to take replace the commas in the number with "nothing" so that +// the number string can be converted to an actual number. \ 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..d19fac93b 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 = 34; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,21 @@ 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 no 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 +// That is the remainder operator and outputs the leftover after division of one operand by the other. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//It takes the total movie duration in seconds and removes the remainder that would come about if the +//total seconds were divided by 60: That way, line4 is sure to bring a whole number back. // e) What do you think the variable result represents? Can you think of a better name for this variable? +//It represents the movie duration in the format; HH:MM:SS. A good name would be "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 minimum movielength is 1 sec. The divided operand will always be returned if the dividing operand can go into it. \ 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..0c17ff48b 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,4 +1,4 @@ -const penceString = "399p"; +const penceString = "1509p"; const penceStringWithoutTrailingP = penceString.substring( 0, @@ -23,5 +23,13 @@ console.log(`£${pounds}.${pence}`); // You need to do a step-by-step breakdown of each line in this program // Try and describe the purpose / rationale behind each step -// To begin, we can start with +// Rational behind each step // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. Lns3-6 strip off the p using .substring() with indexing +// 3. Ln8 limits the string characters to 3 and will pad up from the front with zeros to get +// the 3 characters. it uses the pad.start() method with the arguments (3,"0") +// 4. Lns9-12 uses substring method again to fetch out the first character only. +// 5. Lns14-16 fetches the last two characters using the substring method again and uses the +// padstart method to make use there will always be a "0" added to make sure it is two characters +// if needed. +// Ln18 outputs the pound and pence literals