-
-
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 |
---|---|---|
@@ -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, 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. Do you know why it is safe to delete |
||
|
||
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" |
Uh oh!
There was an error while loading. Please reload this page.