-
-
Notifications
You must be signed in to change notification settings - Fork 197
LONDON | May-2025 | Sisay Mehari | Module-Structuring-and-Testing-Data | Sprint-1 #505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 16 commits
34e9cdd
f410da4
eb12f81
808b276
7cfefd4
b90784d
e2de90d
474cd7c
1243e03
6f5b62f
bcf1740
cbd9de0
07f3b56
90abf10
2792c56
9269348
5435e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ 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, lastSlashIndex); | ||
const ext = base.slice(dotIndex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is |
||
// https://www.google.com/search?q=slice+mdn |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,29 @@ const minimum = 1; | |
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? | ||
/// 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 | ||
//num stores a random whole number in the range from the minimum value (1) to the maximum value (100), including both ends. | ||
// 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 | ||
//const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
//This line generates a random integer between minimum and maximum, inclusive. Here's a breakdown: | ||
//Math.random() returns a decimal between 0 (inclusive) and 1 (exclusive). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also use the concise and precise interval notation to describe a range of values. |
||
//→ MDN: Math.random() | ||
|
||
//(maximum - minimum + 1) defines the size of the range, including the maximum. | ||
|
||
//Multiplying the two scales the random number to a desired range. | ||
|
||
//Math.floor(...) rounds down to ensure a whole number. | ||
//→ MDN: Math.floor() | ||
|
||
//+ minimum shifts the range so it starts at the minimum value. | ||
|
||
// Result: num is a random integer from minimum to maximum, inclusive. | ||
|
||
//Example: If minimum = 1 and maximum = 100, num will be a random whole number between 1 and 100. | ||
//I added console.log(num) and ran the program multiple times. I observed that num always gives a different whole number between 1 and 100. This helped me understand that the expression creates a random integer by multiplying a random decimal by the range size, rounding down, and then shifting it by the minimum value. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
//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? | ||
//To make sure the computer doesn't run them, I turned them into comments using // so they’re ignored by the JavaScript engine. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
let age = 33; | ||
age = age + 1; | ||
//Or we can use like this | ||
age += 1; | ||
console.log(age); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
// Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
// what's the error ? | ||
/* | ||
QUESTION: | ||
The following code fails to print "I was born in Bolton". What's the error? | ||
|
||
console.log(`I was born in ${cityOfBirth}`); | ||
console.log(`I was born in ${cityOfBirth}`); | ||
const cityOfBirth = "Bolton"; | ||
|
||
ANSWER: | ||
The error occurs because: | ||
1. We're trying to use 'cityOfBirth' before it's declared | ||
2. const/let variables can't be accessed before declaration (Temporal Dead Zone) | ||
|
||
SOLUTION: | ||
const cityOfBirth = "Bolton"; // Declare first | ||
console.log(`I was born in ${cityOfBirth}`); // Now works | ||
|
||
EXPLANATION: | ||
- Order matters in JavaScript for const/let variables | ||
- The fixed version follows proper variable declaration order | ||
- This avoids the Temporal Dead Zone reference error | ||
- Output will now correctly show: "I was born in Bolton" | ||
*/ | ||
|
||
// Corrected implementation: | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
// Problem Case (invalid syntax): | ||
// const 12HourClockTime = "20:53"; // ❌ SyntaxError: Invalid variable name | ||
// const 24hourClockTime = "08:53"; // ❌ Also invalid (starts with number) | ||
|
||
// Solution: | ||
const twelveHourClockTime = "08:53"; // ✅ Valid (starts with letter) | ||
const twentyFourHourClockTime = "20:53"; // ✅ Valid | ||
|
||
/* Explanation: | ||
1. JavaScript variable naming rules: | ||
- Cannot start with a digit (0-9) | ||
- Must start with letter, underscore (_), or dollar sign ($) | ||
|
||
2. Fix approach: | ||
- Changed numeric prefixes to words ("twelve" instead of 12) | ||
- Maintained clear, descriptive names | ||
- Kept the same time values as strings | ||
|
||
3. Why strings are appropriate: | ||
- Time formats with colons are best represented as strings | ||
- No arithmetic operations needed on these values | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ let carPrice = "10,000"; | |
let priceAfterOneYear = "8,543"; | ||
|
||
carPrice = Number(carPrice.replaceAll(",", "")); | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); // This line has the error | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // This line was added in your example | ||
|
||
const priceDifference = carPrice - priceAfterOneYear; | ||
const percentageChange = (priceDifference / carPrice) * 100; | ||
|
@@ -11,12 +12,55 @@ 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 | ||
/* | ||
a) How many function calls are there in this file? Write down all the lines where a function call is made | ||
*/ | ||
// There are 7 function calls in this file. They are: | ||
// Line 4: | ||
// carPrice.replaceAll(",", "") → Calls the replaceAll() method on the carPrice string. | ||
// Number(...) → Converts the result to a number using the Number() function. | ||
// Line 5: | ||
// priceAfterOneYear.replaceAll(",", "") → Calls the replaceAll() method on priceAfterOneYear. | ||
// Number(...) → Converts the result to a number using the Number() function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 6 should have replaced line 5. |
||
// Line 6: | ||
// priceAfterOneYear.replaceAll(",", "") → Calls the replaceAll() method on priceAfterOneYear. | ||
// Number(...) → Converts the result to a number using the Number() function. | ||
// Line 10: | ||
// console.log(...) → Calls the log() function to print to the console. | ||
|
||
// 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? | ||
/* | ||
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 occurs on Line 5: | ||
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); | ||
// because the replaceAll() method is missing a comma between its two arguments. It should be: | ||
// priceAfterOneYear.replaceAll(",", "") | ||
// The fix is to add the comma between the arguments to make it syntactically correct. | ||
|
||
// c) Identify all the lines that are variable reassignment statements | ||
/* | ||
c) Identify all the lines that are variable reassignment statements | ||
*/ | ||
// The variables carPrice and priceAfterOneYear are reassigned on lines 4, 5, and 6: | ||
// carPrice = Number(carPrice.replaceAll(",", "")); // Line 4 | ||
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // Line 5 (assuming fixed) | ||
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // Line 6 | ||
// These lines update the existing variables by converting their string values (with commas) into numbers without commas. | ||
|
||
// d) Identify all the lines that are variable declarations | ||
/* | ||
d) Identify all the lines that are variable declarations | ||
*/ | ||
// The variable declarations happen on the following lines: | ||
// Line 1: let carPrice = "10,000"; | ||
// Line 2: let priceAfterOneYear = "8,543"; | ||
// Line 8: const priceDifference = carPrice - priceAfterOneYear; | ||
// Line 9: const percentageChange = (priceDifference / carPrice) * 100; | ||
// So, there are 4 variable declarations in total. | ||
|
||
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
/* | ||
e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
*/ | ||
// The expression Number(carPrice.replaceAll(",", "")) performs two tasks: | ||
// 1. carPrice.replaceAll(",", ""): This removes all commas from the carPrice string. | ||
// For example, if carPrice = "10,000", this part of the expression turns it into "10000". | ||
// 2. Number(...): This then converts the cleaned string "10000" into a number: 10000. | ||
// This expression is used to convert a string that looks like a number with commas (like "10,000") into an actual number (10000) so that it can be used in mathematical calculations. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,54 @@ 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? | ||
/* | ||
a) How many variable declarations are there in this program? | ||
*/ | ||
// There are 6 variable declarations in the program. Each is declared using the const keyword: | ||
// 1. const movieLength = 8784; | ||
// 2. const remainingSeconds = movieLength % 60; | ||
// 3. const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
// 4. const remainingMinutes = totalMinutes % 60; | ||
// 5. const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
// 6. const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
|
||
// b) How many function calls are there? | ||
/* | ||
b) How many function calls are there? | ||
*/ | ||
// There is 1 function call in this program: | ||
// console.log(result); → This is a call to the console.log() function, which outputs the value of result to the console. | ||
|
||
// c) Using documentation, explain what the expression movieLength % 60 represents | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
/* | ||
c) Using documentation, explain what the expression movieLength % 60 represents | ||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
*/ | ||
// The expression movieLength % 60 uses the modulus operator %, which returns the remainder after dividing one number by another. | ||
// It calculates how many seconds are left over after converting the total movieLength (in seconds) into whole minutes. | ||
// So, if movieLength is 8784 seconds, 8784 % 60 gives 24, meaning 24 seconds remain after making as many full minutes as possible. | ||
// Reference: MDN Docs - Modulus operator | ||
|
||
// d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
/* | ||
d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
*/ | ||
// Line 4 calculates the total number of full minutes in the movie length by subtracting the remaining seconds from the total seconds and then dividing by 60. | ||
// const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
// This ensures that only complete minutes are counted, ignoring the leftover seconds. | ||
// For example, if movieLength is 8784 seconds and remainingSeconds is 24, then: | ||
// totalMinutes = (8784 - 24) / 60 = 8760 / 60 = 146 minutes. | ||
|
||
// e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
/* | ||
e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
*/ | ||
// The variable result represents the movie length formatted as a string in hh:mm:ss (hours, minutes, seconds) format. | ||
// A better name for this variable could be: | ||
// movieDurationFormatted (or movieTimeFormatted) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placing the adjective "formatted" in front of the noun "MovieDuration" would probably make the variable name sound more natural. |
||
// This would make the purpose of the variable more clear when reading the code. | ||
|
||
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
/* | ||
f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
*/ | ||
// Yes, the code works for various non-negative integer values of movieLength. For example: | ||
// When movieLength = 59, the result is 0:0:59, which is correct. | ||
// When movieLength = 732, the result is 0:12:12, which is also accurate. | ||
// The code handles the conversion from seconds to hours, minutes, and seconds properly for different input values. | ||
// (Note: The code assumes movieLength is a non-negative integer. It would produce decimal results or unexpected formats for negative or non-integer inputs, but for typical movie lengths, it works.) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,21 @@ console.log(`£${pounds}.${pence}`); | |
|
||
// To begin, we can start with | ||
// 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
// This value represents a monetary amount in pence, with a trailing "p" to indicate "pence" (e.g., 399 pence). | ||
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
// This line removes the last character ("p") from the penceString. | ||
// It uses .substring() to extract all characters from index 0 up to (but not including) the final character. | ||
// Purpose: To isolate the numeric portion of the string so that calculations can be performed. | ||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
// This line ensures the string has at least 3 characters by adding "0" to the start if it's too short. | ||
// Rationale: This guarantees that the number has enough digits to split correctly into pounds and pence, even if the original number is small (e.g., "5p" becomes "005"). | ||
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
// This line extracts the pounds part by slicing the string from the beginning up to the last 2 characters. | ||
// Rationale: In UK currency, the last two digits represent pence, and the digits before that represent pounds. | ||
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
// This line extracts the last 2 characters as the pence part. | ||
// If for any reason it's shorter than 2 digits, it adds a "0" to the end to ensure the format is correct. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What reason would it be? |
||
// Purpose: Guarantees a consistent two-digit pence value. | ||
// 6. console.log(`£${pounds}.${pence}`); | ||
// This line constructs and prints the final price string in pounds and pence format, using template literals. | ||
// Rationale: This is the output the user will see, displaying the price in standard British currency format (e.g., £3.99). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Operation like
count = count + 1
is very common in programming, and there is a programming term describing such operation.Can you find out what one-word programming term describes the operation on line 3?