-
-
Notifications
You must be signed in to change notification settings - Fork 197
Manchester | 25-ITP-May | Jennifer Isidienu | Sprint 1 | Coursework #683
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
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,4 @@ | ||
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 comment to ignore the two lines so the computer doesn't run them. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
age = age + 1; | ||
// Trying to create an age variable and then reassign the value by 1 | ||
|
||
let age = 33; // Create a changeable variable | ||
age = age + 1; // Increase the value by 1 | ||
|
||
console.log("New age:", age); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
// 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}`); | ||
// console.log(`I was born in ${cityOfBirth}`); | ||
// const cityOfBirth = "Bolton"; | ||
|
||
// The error occurs because the variable `cityOfBirth` is being used before it has been declared. | ||
// In JavaScript, variables declared with `const` must be initialized before they can be used. | ||
// To fix this, we need to declare the variable before using it in the console.log statement. | ||
|
||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.slice(-4); | ||
const last4Digits = String(cardNumber).slice(-4); | ||
console.log(last4Digits); | ||
|
||
|
||
// 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 | ||
|
||
// The code will not work because `cardNumber` is a number, | ||
// and the `slice` method is not available on numbers. | ||
// The `slice` method is a string method, so we need to convert | ||
// `cardNumber` to a string first. | ||
// The prediction is correct, and the error stated: | ||
// cardNumber.slice is not a function" | ||
// After converting `cardNumber` to a string, the code now works correctly. | ||
// The last4Digits variable now correctly stores the last 4 digits of cardNumber | ||
// The Output is "4213" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const twelveHourClockTime = "08:53"; | ||
const twentyFourHourClockTime = "20:53"; | ||
|
||
console.log("12-hour clock time:", twelveHourClockTime); | ||
console.log("24-hour clock time:", twentyFourHourClockTime); |
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" | ||
|
||
// 2. const penceStringWithoutTrailingP = penceString.substring( | ||
// 0, penceString.length - 1): removes the trailing 'p' from the string. | ||
|
||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") | ||
// : pads the string with leading zeros to ensure it has at least 3 characters. | ||
|
||
// 4. const pounds = paddedPenceNumberString.substring | ||
// (0, paddedPenceNumberString.length - 2): extracts the pounds part of the string | ||
// by taking all characters except the last two because it represents the pence. | ||
|
||
// 5. const pence = paddedPenceNumberString.substring | ||
// (paddedPenceNumberString.length - 2).padEnd(2, "0"): | ||
// extracts the pence part of the string by taking the last two characters | ||
// and ensuring it has exactly 2 characters by padding with zeros if necessary. | ||
Comment on lines
+39
to
+42
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. Could we expect this program to work as intended for any valid 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. I think it's needed because the program may not work as intended if we deleted. Am I 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. Why don't you try executing the script without using 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. Everything still works fine. It seems that .padstart(3, "0") already ensures the string has at least 3 characters. So, when we extract the last two characters for the pence part, no extra padding is needed afterward. |
||
|
||
// 6. console.log(`£${pounds}.${pence}`): prints the final formatted string | ||
// in pounds and pence e.g. £3.99. |
Uh oh!
There was an error while loading. Please reload this page.