-
-
Notifications
You must be signed in to change notification settings - Fork 274
West Midlands | ITP September-2025 | Jonathan Boahene | Sprint 1 | Module Structuring and Testing Data #824
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
1eb4e07
bc143ce
5cb7a47
b949792
198f1ae
5357eab
25632b9
65e9285
34cf88c
eff1a8f
6123bb4
e8b8bb9
49bec3d
fea47a8
5b21ac9
ab08eb8
bb12ae4
868b2f3
5c9e5f7
9cc908b
f7704ee
479278e
75cbece
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 |
|---|---|---|
|
|
@@ -5,7 +5,34 @@ let lastName = "Johnson"; | |
| // Declare a variable called initials that stores the first character of each string. | ||
| // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. | ||
|
|
||
| let initials = ``; | ||
| // let initials = `${firstName.slice(0,1)}${middleName.slice(0,1)}${lastName.slice(0,1)}`; using slice on template literals | ||
|
|
||
| //let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; using charAt on template literals | ||
|
|
||
| //let initials = (firstName[0] || '') + (middleName[0] || '') + (lastName[0] || ''); using concatenation | ||
|
|
||
| // let initials = `${firstName[0] || ''}${middleName[0] || ''}${lastName[0] || ''}` //template literals | ||
|
|
||
| //let initials = [firstName, middleName, lastName].map(name => name.slice(0,1)).join('') | ||
|
|
||
| let initials = [firstName, middleName, lastName] | ||
|
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. This Solution is very good. |
||
| .map((name) => name.charAt(0)) | ||
| .join(""); | ||
|
|
||
| /* | ||
| There are more efficient ways of implementing the solution, however these concepts haven't been covered yet | ||
| In these scenarios the slice and charAt have been implemented through an array and map | ||
|
|
||
| let initials = [firstName, middleName, lastName].map(name => name.slice(0,1)).join('') | ||
| let initials = [firstName, middleName, lastName].map(name => name.charAt(0)).join('') | ||
|
|
||
|
|
||
| Direct use of `charAt`, `slice`, or indexing is simple and readable for a fixed number of names but becomes repetitive and less scalable. | ||
| Using an array with `map` is concise, scalable, and handles missing names well, making it best for variable-length inputs. | ||
| Template literals or concatenation with `||` are concise and handle missing names, but are still repetitive for many name parts. | ||
| For flexibility, the array + map approach is generally preferred. | ||
| */ | ||
|
|
||
| // https://www.google.com/search?q=get+first+character+of+string+mdn | ||
|
|
||
| console.log(initials); // display the output of initials to verify if successfuly executed | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,13 @@ const base = filePath.slice(lastSlashIndex + 1); | |
| console.log(`The base part of ${filePath} is ${base}`); | ||
|
|
||
| // Create a variable to store the dir part of the filePath variable | ||
| const dir = filePath.slice(0, lastSlashIndex); // Extract the directory part (everything before the last slash) | ||
|
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. Good — clear, descriptive variable, and the path includes both directory and extension so the task is realistic |
||
|
|
||
| // Create a variable to store the ext part of the variable | ||
| const lastDotIndex = base.lastIndexOf("."); // Find the index of the last dot in the base to get the extension | ||
| const ext = lastDotIndex !== -1 ? base.slice(lastDotIndex) : ""; // Extract the extension (including the dot), or empty string if none | ||
|
|
||
| const dir = ; | ||
| const ext = ; | ||
| console.log(`The dir part of ${filePath} is ${dir}`); | ||
| console.log(`The ext part of ${filePath} is ${ext}`); | ||
|
|
||
| // https://www.google.com/search?q=slice+mdn | ||
| // https://www.google.com/search?q=slice+mdn | ||
| 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? | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| //const age = 33; // redeclaring with a let or var solves this error | ||
| /* variables declared with `const` in JavaScript cannot be reassigned. | ||
| However, if the `const` variable is an object or array, | ||
| its contents can still be changed (the reference is immutable, not the value). | ||
| */ | ||
| let age = 33; | ||
| age = age + 1; | ||
|
|
||
| console.log(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}`); | ||
| /* | ||
| This happens because in JavaScript, variables declared with `let` or `const` are not accessible before their declaration | ||
| due to a behavior called the **temporal dead zone**. | ||
| If you try to use such a variable before it’s initialized, you get a `ReferenceError`. | ||
| To avoid this, always declare and assign your variables before using them. | ||
|
|
||
| */ | ||
| const cityOfBirth = "Bolton"; | ||
| console.log(`I was born in ${cityOfBirth}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,25 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| // 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 | ||
|
|
||
| /* This code throws an error because `cardNumber` is a **number**, and numbers do not have a `.slice()` method—`.slice()` is a method for strings and arrays. | ||
|
|
||
| **How to fix:** | ||
| Convert `cardNumber` to a string before using `.slice()`: | ||
|
|
||
| ````javascript | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| ```` | ||
|
|
||
| Now, `last4Digits` will correctly contain the last 4 digits as a string.*/ | ||
|
|
||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(last4Digits); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| // const 12HourClockTime = "20:53"; | ||
| // const 24hourClockTime = "08:53"; | ||
|
|
||
| // variables in js can't begin with numbers | ||
| const twelveHourClockTime = "20:53"; | ||
| const twenty24hourClockTime = "08:53"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // function decimalTopercent(num){ | ||
| // let percent = num * 100; | ||
| // return percent | ||
| // } | ||
| // console.log(decimalTopercent(0.5) + "%"); | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| const result = convertToPercentage(0.5); | ||
| const result1 = convertToPercentage(0.231); | ||
| console.log(result); | ||
| console.log(result1); |
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.
very detailed! @Abayie