-
-
Notifications
You must be signed in to change notification settings - Fork 195
London | May 2025 | Samuel Tarawally | Sprint 1 | Coursework #660
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
e679e30
8c74dfc
fbf11cc
ea9be3b
a5c9d8d
46d1dd2
cc4f7ed
7b3ee54
2e29de5
5f11640
34f5029
f242ff4
072210b
bad38d6
53989ad
420a3ef
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,4 +1,6 @@ | ||
node_modules | ||
.DS_Store | ||
.vscode | ||
**/.DS_Store | ||
**/.DS_Store | ||
**/runme.md | ||
.devcontainer/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ 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(`The random number is: ${num}`); | ||
Comment on lines
+10
to
+11
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. Can you describe how the expression at line 4 works? Can you also describe the possible values that could be assigned to
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. It works by calculating a random number with a value greater than or equal to 0 and less than 1 using From this |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
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? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
// The `const` keyword cannot be reassigned to a different object or array. | ||
// Variable references of this type are considered immutable. | ||
// The `let` keyword allows variables to be declared and reassigned. | ||
let age = 33; | ||
age = age + 1; | ||
console.log(age); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// 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}`); | ||
// Variables must be declared before they are used. | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.slice(-4); | ||
// The following code will throw an error when run | ||
// since the variable cardNumber is an integer. | ||
// The slice method is not available on integers. | ||
// 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 | ||
// Converting the integer to a string first will fix the issue. | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = String(cardNumber).slice(-4); | ||
console.log(last4Digits); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
// Variable names in JavaScript cannot start with a number. | ||
// This code throws a SyntaxError when run. | ||
// const 12HourClockTime = "20:53"; | ||
// const 24hourClockTime = "08:53"; | ||
|
||
// The variable names should be valid identifiers, e.g. | ||
const twelveHourClockTime = "20:53"; | ||
const twentyFourHourClockTime = "08:53"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,48 @@ const pence = paddedPenceNumberString | |
|
||
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 | ||
// This programme takes a string representing a price in pence | ||
// It then constructs 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 | ||
// You need to provide a step-by-step breakdown of each line in this programme | ||
// Describe the purpose and reasoning behind each step | ||
|
||
// To begin, we can start with | ||
// 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
// To begin, we start with: | ||
// 1. const penceString = "399p": Initialises a string variable with the value "399p". | ||
// This represents the initial price in pence that the programme will process. | ||
|
||
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); | ||
// This line removes the trailing 'p' from `penceString` by: | ||
// - `penceString.length`: Obtains the total length of `penceString`, which is 4. | ||
// - `penceString.length - 1`: Calculates the index of the last character, which is 'p'. | ||
// - `penceString.substring(0, penceString.length - 1)`: Extracts the substring from the start of `penceString` up to, but not including, the last character. | ||
// - The result is the removal of the 'p' character, leaving "399". | ||
|
||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
// This line declares a constant variable named `paddedPenceNumberString`. | ||
// - It applies the `padStart()` method to `penceStringWithoutTrailingP` ("399"). | ||
// - `padStart(3, "0")` checks if the string's length is less than 3. | ||
// - If so, it adds "0" to the beginning of the string until its length reaches 3. | ||
// - In this case, "399" already has a length of 3, so it remains unchanged. | ||
// - The purpose of this step is to ensure the pence value always has at least three digits (e.g., "5p" becomes "005", "99p" becomes "099"). | ||
// - This is important for consistently extracting the pounds and pence parts later, especially for values less than 100p. | ||
|
||
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); | ||
// This line extracts the pounds part of the price. | ||
// - It declares a constant variable named `pounds`. | ||
// - It uses `substring(0, paddedPenceNumberString.length - 2)` on `paddedPenceNumberString` ("399"). | ||
// - This extracts the substring from the start of the string up to, but not including, the last two characters. | ||
// - In this example, it extracts "3" from "399". | ||
|
||
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
// This line extracts and formats the pence part of the price. | ||
// - It declares a constant variable named `pence`. | ||
// - It uses `substring(paddedPenceNumberString.length - 2)` on `paddedPenceNumberString` ("399"). | ||
// - This extracts the last two characters of the string, which are "99". | ||
// - The `padEnd(2, "0")` method ensures the extracted pence value has at least two digits. | ||
Comment on lines
+53
to
+58
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. No, |
||
|
||
// 6. console.log(`£${pounds}.${pence}`); | ||
// This final line constructs and prints the formatted price string to the console. | ||
// - It uses a template literal to format the output as "£{pounds}.{pence}". | ||
// - In this case, it outputs "£3.99", which is the price in pounds and pence format. | ||
// - The `console.log` function displays the final result in the console. |
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.
I believe it's an increment operation.