diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..9f5c9011bc 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,10 @@ + let count = 0; 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 +In line 3 , the new item adds 1 to turn count into a new value . + +1 adds into an old value and update its own value , it becomes a new value now. \ 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 47561f6175..b8b2e5e974 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,15 @@ 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 = ``; + +function first(firstName, middleName, lastName){ + + let initials = firstName.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1); + + return `${initials}`; +} +console.log(first(firstName, middleName, lastName)); + // 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..adce16115b 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,13 +11,26 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); + +// 1. Get the base (file.txt) const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); -// Create a variable to store the dir part of the filePath variable +const dir = filePath.slice(0, lastSlashIndex); + +// Create a variable to store the dir part of the filePath +const ext = filePath.slice(filePath.lastIndexOf(".") + 1); + // Create a variable to store the ext part of the variable -const dir = ; -const 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..0cb3c7dce2 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,18 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +Math.floor is a function to make sure it is a whole number. + +The fuction will randomly choose a decimal number from 0 - 1. +After that will do the Math(100-1+1) and multiply the decimal value and add 1. + + + + // 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 + + + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..f2702bcd14 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -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..0c6ddfff50 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,7 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +let age = 0; // Initialize the age variable +age = age + 1; // Reassign the value by 1 +console.log(age); + + diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..1cdbd587a9 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"; +const cityOfBirth = "BOlton"; +const sentence = `I was born in ${cityOfBirth}`; +console.log(sentence); \ 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 ec101884db..01f7180b71 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,13 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); + +because it doesn't have console.log, so it doesn't print the result. +but it gives the function errors. + +it is not what I expected, because we should change the number into string, because it is not a +function. + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..06c2f6483b 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 twelvehourClockTime = "8:53pm"; +const twentyfourhourClockTime = "20:53"; + +console.log(twelvehourClockTime); +console.log(twentyfourhourClockTime); + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..c85e12e86a 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,30 @@ 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 +6 // 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? +It lacks comma insides the replaceAll. -// c) Identify all the lines that are variable reassignment statements +// 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 +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +It is to take out the comma insides the number of carPrice. + + + + + + + + + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..06129c3fd8 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 = 6; // 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? - +//6 // b) How many function calls are there? - +//1 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +//It divides totalMinutes into 60, see how much remainingMinutes left. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +//After deducting the totalMinutes, and divide the second. How much do he have the totalMinutes ? // e) What do you think the variable result represents? Can you think of a better name for this variable? +//The total duration of the movieLength. // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +//for one hour = 3600 minutes . 1:00:00 +//for 59 minutes = 3540 minutes . 0:59:0 +//for seconds = 6 seconds . 0:0:6 diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..03bfa166b9 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,32 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +1. const penceString = "399p": initialises a string variable with the value "399p" +2. +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); the purpose of this is to take out the p. + + +3. +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +if it is shorter than 3 characters, the 0 will add in front of the number ; but 399 is already 3 numbers so we donot need to do anything. + + + +4. const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); the length of the string is 3 , after -2 , it will become 1. The const pounds will take out 3 and it will become 3. + + +5. const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + It will take out the number from the end to 1 ; so it will have 99. + +6.console.log(`£${pounds}.${pence}`); + +Finally it will put the pounds and the pence into the template and print it out. The result will be £${3}.${99} ; \ No newline at end of file