-
-
Notifications
You must be signed in to change notification settings - Fork 195
London | May 2025 | Houssam Lahlah | Coursework/sprint 1 #657
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 all commits
4f906b1
6bbd872
80eefd3
7f4d5bf
cf582c5
3c9abdd
b276dfb
b50a6c4
b980390
7d43c64
0284fac
ba702df
78d0604
af998dd
7623716
6a8fc1c
1b0b5b3
d477732
2a31470
64a6dc6
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 |
---|---|---|
|
@@ -3,7 +3,74 @@ 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 and running the program several times to build an idea of what the program is doing | ||
// 1- In this exercise, you will need to work out what num represents? | ||
|
||
// the num gives a random whole number between 1 and 100 like 73, 12, or 100. | ||
|
||
// 2- Try breaking down the expression and using documentation to explain what it means | ||
/* | ||
1. Math.random() | ||
Returns a random decimal number between 0 and 1 but never gives 1.0. | ||
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.
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. Thanks for the explanation! |
||
Example: 0.24 | ||
2. (maximum - minimum + 1) | ||
This gives number of possible values. | ||
Without the +1, we'd only get the difference, not the full count. | ||
for example: | ||
5 - 1 = 4 → but there are actually 5 numbers: 1, 2, 3, 4, 5 | ||
So we add +1 to include both ends of the range. | ||
3. Math.random() * (maximum - minimum + 1) | ||
This gives a random decimal number between 0 and 100 (like 24, 65 ...) | ||
Because we want the random decimal scaled to the size of the range of possible values. | ||
For example, if we want a number between 1 and 100 (inclusive), there are 100 possible numbers (1, 2, ..., 100). | ||
Multiplying by 100 means the decimal is scaled up to cover all those possibilities before rounding. | ||
4. Math.floor(...) | ||
This rounds the decimal down to the nearest whole number. | ||
Example: Math.floor(78.43) → 78 | ||
5. + minimum | ||
we add the minimum to shift the range correctly, and make sure the random number up to start from minimum. | ||
5-1- for example if we remove the + minimum | ||
5-1-1 Math.random() 0.9999 * 99 + 1 → only goes up to 99.999... → max = 99.999... → floor = 100 (but very unlikely) | ||
now 100 becomes very hard to reach, and in many cases, you never get it. | ||
5-1-2 Math.random() 0.00 * 99 + 1 → only goes up to 0... → max = 0... → floor = 0 (now the minimum is 0, and can appears) | ||
conclusion : when we don’t add + minimum, there is a chance that 1 appears, but it’s not the guaranteed minimum anymore — | ||
and the range starts at 0, not 1. | ||
5-2- when we add +minimum | ||
now we make sure the min and max can appear in the final results and make sure the minimum is 1 not 0. | ||
Minimum appears when random = 0 | ||
Maximum appears when random is almost 1 (like 0.9999...). | ||
example : Math.random() * 99 + 1 → up to 0.99 → max = 99 → floor = 99 → +1 = 100 (so more possibilities for 100 to appears) | ||
*/ | ||
|
||
//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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
// we can turn the lines into comments. we can use: | ||
|
||
// for single-line comments, or | ||
|
||
/* for multi-line comments. */ | ||
|
||
/* | ||
|
||
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 don't want the computer to run these 2 lines - how can we solve this problem? | ||
|
||
*/ | ||
|
||
// we can use comments to make sure the computer ignores the lines, comment them out using : | ||
|
||
// // single line or /* multi-line */. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
// Trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
age = age + 1; | ||
let age = 33; // Declare a variable called age and assign it the value 33 | ||
age = age + 1; // Reassign age to be its current value plus 1 | ||
console.log(age); // Output: 34 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
// what's the error ? | ||
// what's the error ? The error here is due to using a variable before it's declared. | ||
|
||
console.log(`I was born in ${cityOfBirth}`); | ||
|
||
// Fixed: Declare the variable before using it | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
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 | ||
// 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 | ||
|
||
// We want last4Digits to store the last 4 digits of cardNumber | ||
|
||
// Prediction before running: | ||
// This will cause an error because cardNumber is a number, | ||
// and numbers don't have the slice() method. slice() works only on strings or arrays. | ||
|
||
// Running the code would give: | ||
// TypeError: cardNumber.slice is not a function | ||
|
||
// Why? | ||
// Because slice() is not defined for numbers in JavaScript. | ||
|
||
// Fix: | ||
// Convert cardNumber to a string first, so we can use slice() on it. | ||
// Then slice the last 4 characters to get the last 4 digits. | ||
|
||
const last4Digits = String(cardNumber).slice(-4); | ||
|
||
console.log(last4Digits); // Output: 4213 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
// Original variable names are invalid because variable names cannot start with a number | ||
// const 12HourClockTime = "20:53"; // This will cause a syntax error | ||
// const 24hourClockTime = "08:53"; // Also invalid for the same reason | ||
|
||
// Fix: | ||
// Variable names must start with a letter, underscore (_), or dollar sign ($). | ||
// So we rename the variables to valid names below: | ||
|
||
const hour12ClockTime = "20:53"; // Valid variable name for 12-hour clock time | ||
const hour24ClockTime = "08:53"; // Valid variable name for 24-hour clock time | ||
|
||
// Explanation: | ||
// Variable names cannot begin with a number in JavaScript, | ||
// so prefixing them with a letter or underscore solves the problem. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
const penceString = "399p"; | ||
// 1. Initializes a string variable with the value "399p" representing the price in pence with a trailing 'p'. | ||
|
||
const penceStringWithoutTrailingP = penceString.substring( | ||
0, | ||
penceString.length - 1 | ||
); | ||
// 2. Removes the trailing 'p' by taking a substring from index 0 up to (but not including) the last character. | ||
// Result: "399" | ||
|
||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
// 3. Ensures the string is at least 3 characters long by padding with leading zeros if needed. | ||
// In this case, "399" is already 3 characters, so it remains "399". | ||
|
||
const pounds = paddedPenceNumberString.substring( | ||
0, | ||
paddedPenceNumberString.length - 2 | ||
); | ||
// 4. Extracts the pounds part by taking all characters except the last two. | ||
// For "399", this is "3" (the hundreds place). | ||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) | ||
.padEnd(2, "0"); | ||
// 5. Extracts the last two characters as the pence part. | ||
// For "399", this is "99". | ||
// Then pads the string on the right with zeros if it’s shorter than 2 characters (not needed here). | ||
Comment on lines
+25
to
+27
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. When you mentioned "not needed here", do you mean we can delete 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. heloo, |
||
|
||
console.log(`£${pounds}.${pence}`); | ||
|
||
// This program takes a string representing a price in pence | ||
// The program then builds up a string representing the price in pounds | ||
|
||
// 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 | ||
// 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
// 6. Prints the formatted price string with a £ sign, pounds, and pence separated by a dot. | ||
// Output: "£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?
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.
Hello,
sorry about the delay, You're absolutely right In programming, "increment" means to increase the value of a variable, usually by 1. So count = count + 1 is incrementing the value of count, howver I avoided to write the specific word and put simple explanation for it.
thank you for your sugestion.