diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index 185e9a33..76af28af 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -17,6 +17,8 @@ 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 -let dirPath=filePath.slice(0,-8); +let dirPath = filePath.slice(0, lastSlashIndex); console.log(dirPath); -console.log(base); +let extPath = base.includes(".") ? base.slice(base.lastIndexOf(".") + 1) : ""; +console.log(extPath); + diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 554c0798..8a75667a 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -3,9 +3,9 @@ const maximum = 100; 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 -// Try logging the value of num several times to build an idea of what the program is doing - math,random gives a value between 0 and 1, however 0 and 1 are not included. max - min +1 is 100 so a random number multiplied by 100 is anywhere between 0 and 100 but 0 and 100 not included. then add +1 and then 100 is included. math. floor rounds the number. so this function is giving us a random number between 0 and 100 +// math.random gives a value between 0 (inclusive) and 1 (exclusive). +// max - min + 1 is 100, so a random number multiplied by 100 is between 0 (inclusive) and 100 (exclusive). +// Adding +1 shifts the range to be between 1 and 101 (exclusive). +// Math.floor rounds the number, so this function gives us a random whole number between 1 and 100 (inclusive). -console.log(num); \ No newline at end of file +console.log(num); diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 0ab706f3..004090d5 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -11,10 +11,13 @@ 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 - line 4,5,8,10 +// a) How many function calls are there in this file? +//Write down all the lines where a function call is made - line 4,5,8,10 // b) Identify all the lines that are variable reassignment statements - line 4,5, -// c) Identify all the lines that are variable declarations - line 1,2 +// c) Identify all the lines that are variable declarations - line 1,2, 7, 8 -// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? - it is replacing all commas, so commas are being deleted +// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - +//what is the purpose of this expression? - it is replacing all commas, so commas are being deleted +//the number() chnages string to a number \ No newline at end of file diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 5d9bdd5d..59898d6f 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -15,7 +15,7 @@ console.log(result); // a) How many variable declarations are there in this program? - just 7 -// b) How many function calls are there? - 2 +// b) How many function calls are there? - there's only 1 which is console.log // c) Using documentation on MDN, explain what the expression movieLength % 60 represents - The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend. diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 7b3d03aa..5afcb9b0 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -1,4 +1,4 @@ -const penceString = "399p"; //this defines the value of the pence in a string +const penceString = "399p"; //this defines the value of the pence in a string which is 399p const penceStringWithoutTrailingP = penceString.substring( 0, @@ -6,7 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring( ); // substring means it extracts a character, it is usually written like this substring(indexStart) or substring(indexStart,indexEnd) // in this example it is substring (0, penceString.lenght -1) which means 0 is the first character which is 3 in this example and -1 means the last character is being cut off so it will be 399 -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // this means it will add a character or value to the string until it reaches the length required +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // this means it will add a character or value to the string until it reaches the length required which gives a value of 399 as it already is 3 lenghts long const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2 @@ -15,9 +15,9 @@ const pounds = paddedPenceNumberString.substring( const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); // substring in this example means it will cut off the first 2 characters fromt he start so it will be 3 - // padend means it will add the specified character starting from the end until it reaches the desired number + // padend means it will add the specified character starting from the end until it reaches the desired number which gives a value of 99 -console.log(`£${pounds}.${pence}`); // this will log the string value of pound and pence +console.log(`£${pounds}.${pence}`); // this will log the string value of pound and pence which is £.99 // This program takes a string representing a price in pence // The program then builds up a string representing the price in pounds