diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..c408d20d6 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,6 @@ 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 changing the value of the count variable by increasing 1 to its current value. +// The = operator is used to assign the new value back to the count variable. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..599c438bf 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,11 @@ 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 getInitial(name) { + return name[0]; +} -// https://www.google.com/search?q=get+first+character+of+string+mdn +let initials = + getInitial(firstName) + getInitial(middleName) + getInitial(lastName); +console.log(initials); diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..71d130f06 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/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 = ; - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file +const dir = filePath.slice(0, lastSlashIndex); +console.log(`The dir part of ${filePath} is ${dir}`); +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(`The ext part of ${filePath} is ${ext}`); diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..a4e5ee658 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 +console.log(num); +// num is a random integer between 1 and 100. +// Math.random() generates a random floating-point number between 0 and 1. +// Multiplying this by (maximum - minimum + 1) scales it to a range of 0 to 100. +// Math.floor() rounds down the result to the nearest integer number. +// Finally, adding minimum shifts the range to be between 1 and 100. diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..997eae365 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 7a43cbea7..0d4b2fdcb 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +let age = 30; // Change const to let to allow reassignment +age += 1; // Increment age by 1 +console.log(age); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..48309110d 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -3,3 +3,9 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// The variable cityOfBirth should be declared before it is used in the console.log statement. +/* +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); +*/ \ 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 ec101884d..dcd6fc5cc 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,7 @@ +/* The old code: 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 +9,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 + +/* My prediction: The code will give an error because the minus sign(-4) and slice works only with strings, not numbers. + +Consideration: The error is a TypeError: cardNumber.slice is not a function. mostly it was because I predicted that numbers do not have a slice method. About the minus sign, I have learned about positive and negative indexing in strings, now I can see the difference. + + +To fix this, we need to convert cardNumber to a string before calling the slice method. +*/ + +// Updated code: +const cardNumber = 4533787178994213; +const last4DigitsCorrected = cardNumber.toString().slice(-4); +console.log(last4DigitsCorrected); diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..9294537b5 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,13 @@ +/* + The old code: const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; +console.log(24hourClockTime);*/ + +// It is giving a syntax error +// The reason is that variable names cannot start with a number + +// Updated code: +const hourClockTime12 = "20:53"; +const hourClockTime24 = "08:53"; +console.log(hourClockTime24, hourClockTime12); diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..9eefffa1f 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -13,10 +13,52 @@ 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 +// There are 5 function calls in this file: +// line 4: carPrice.replaceAll(",", "") +// line 4: Number(carPrice.replaceAll(",", "")) +//line 5: priceAfterOneYear.replaceAll("," "") +// line 5: Number(priceAfterOneYear.replaceAll("," "")) +// line 9: console.log(`The percentage change is ${percentageChange}`) + + + // 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's a SyntaxError in line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));. + The error is occurring because there is a missing comma between the two arguments of the replaceAll function here ("," ""). + To fix this problem, we need to add a comma between the two arguments: replaceAll(",", ""). + */ + + // c) Identify all the lines that are variable reassignment statements +/*reusing the same variable carPrice but updating its value: +carPrice = Number(carPrice.replaceAll(",", "")); + +reusing the same variable priceAfterOneYear but updating its value: +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +*/ + + // d) Identify all the lines that are variable declarations +/*declaring a new variable carPrice: +let carPrice = "10,000"; + +declaring a new variable priceAfterOneYear: +let priceAfterOneYear = "8,543"; + +declaring a new variable priceDifference: +const priceDifference = carPrice - priceAfterOneYear; + +declaring a new variable percentageChange: +const percentageChange = (priceDifference / carPrice) * 100; +*/ + + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* +The expression Number(carPrice.replaceAll(",", "")) is converting the carPrice string into a number. +The purpose of this expression is to get a numeric value for carPrice that can be used for calculations. +*/ diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..d3f74a424 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,52 @@ 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: +1.Line 1 : const movieLength. +2.Line 3 : const remainingSeconds. +3.Line 4 : const totalMinutes. +4.Line 6 : const remainingMinutes. +5.Line 7 : const totalHours. +6.Line 9 : const result. +*/ // b) How many function calls are there? +/* There is one function call: +Line 10 : console.log(). +*/ + // c) Using documentation, explain what the expression movieLength % 60 represents + +/* The movieLength represents the length of the movie in seconds, the operand % is remaining (Modulo) returns the remaining of the movieLength divided by 60(to convert sec into minutes) but it only gives the remaining of this operation. + */ + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* +The expression assigned to completed total minutes of the movie, by subtract the movie length in sec from the remaining seconds and divide the result by 60 to convert it to minutes +*/ + // e) What do you think the variable result represents? Can you think of a better name for this variable? +/* + The variable result display the total length of the movie in these form (Hours:Minutes:seconds) in string format. + + I can think of "MovieDuration" as a better name because it gives more description to the output. +*/ + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +/* +I did experimenting the code by putting different value to the movieLength and it works just fine. + +test numbers and its outcomes: +1. 36000sec>> 10:0:0 +2. 120 sec >> 0:2:0 +3. 0 sec >> 0:0:0 +4. 555555.55sec >> 154:19:15.550000000046566 + +As the results show the code is working fine even with unexpected large numbers. +*/ diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..8aae5651e 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,9 @@ 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): represent to takes a substring from index 0 up to (but not including) penceString.length - 1. +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): assign to ensures the numeric string is at least 3 characters long by adding leading zeros if needed. +// 4. const pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2): Takes the substring from the start up to (but not including) the last two characters. +// 5. substring(length - 2) : use to returns the last two characters (the pence digits). padEnd(2, "0") would append trailing zeros if the result were shorter than 2. +// 6. console.log(`£${pounds}.${pence}`): assign to display the price in a human-readable pounds-and-pence format (£x.yy) diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..52333cc5c 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -12,7 +12,12 @@ invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +Answer: A pop up window displaying a text (Hello World!) will appear on the top and an OK button. It's a temporarily effect, but it required a user interaction to press OK before continuing. + Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. What effect does calling the `prompt` function have? What is the return value of `prompt`? + +Answer: A pop up window appears with a title and a question (What is your name?), also an answering space area. At the right down of the window there are two buttons to the user, Ok and cancel. +After typing myName and press OK the console display the answer(myName). diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..407335b03 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -6,11 +6,28 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +-The output : ƒ log() { [native code] } + Now enter just `console` in the Console, what output do you get back? +-The output : console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + a lot of functions under it. + Try also entering `typeof console` -Answer the following questions: +-The output : object + +-Answer the following questions: What does `console` store? + +-Answer : console is an object with debugging methods. +It does not store variables or result. It provide methods for outputting , data , object and more. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +Answer: +-console is a built-in object. +-log and assert are properties of that object, specifically functions (methods). +-The console.assert() static method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. +-The console.log() static method outputs a message to the console. +-'.' means use this method from this object.