Skip to content

London | May 2025 | Samuel Tarawally | Sprint 2 | Coursework #668

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
46d1dd2
ignores local vscode configurations
Tarawally Jul 14, 2025
4438f2d
docs(errors-0): Predict and explain error in capitalise function
Tarawally Jul 16, 2025
22e00ee
docs(errors-0): Predict and explain error in capitalise function
Tarawally Jul 16, 2025
c1fb283
docs(key-errors-1): Clarify explanations regarding variable redeclara…
Tarawally Jul 16, 2025
7284a1f
docs(key-errors): cCarify error explanation in square function example.
Tarawally Jul 16, 2025
a0442d0
fix(0.js): Correct multiply function to return value
Tarawally Jul 17, 2025
fd5e2b5
fix(0.js): Correct multiply function to return value
Tarawally Jul 17, 2025
a215a26
fix(0.js): Correct multiply function to return value
Tarawally Jul 17, 2025
3ec1c22
fix(1.js): correct sum function to return proper result and add expla…
Tarawally Jul 17, 2025
28d8815
fix(mandatory-debug): update getLastDigit to use parameter instead of…
Tarawally Jul 17, 2025
1d04ae1
fix(errors): correct invalid function parameter name in square function
Tarawally Jul 17, 2025
9296d88
fix(errors): refactor error explanation and correct code for percenta…
Tarawally Jul 17, 2025
fd53bfc
fix(errors): resolve variable redeclaration error in capitalise function
Tarawally Jul 17, 2025
3540425
feat(implement): calculate BMI rounded to one decimal place
Tarawally Jul 17, 2025
c38389a
feat(implement): upper snake case conversion function
Tarawally Jul 17, 2025
263d8c9
feat(implement): refactor pence conversion script into toPounds function
Tarawally Jul 17, 2025
2348e1e
docs(interpret): Answer questions in time-format.js
Tarawally Jul 18, 2025
86c9dd3
Merge branch 'CodeYourFuture:main' into sprint-2
Tarawally Jul 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
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/
8 changes: 8 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// A syntax error should occur because the function parameter is named `str`, but it is also being redeclared inside the function.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +11,11 @@ function capitalise(str) {
}

// =============> write your explanation here
// The code outputs the error `SyntaxError: Identifier 'str' has already been declared`.
// JavaScript does not allow you to redeclare a variable or parameter with the same name in the same scope.
// In this case, the parameter `str` is being redeclared as a `let` variable inside the function, which is not allowed.
// =============> write your new code here
function capitalise(str) {
const capitalisedString = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedString;
}
12 changes: 12 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// An error such as `decimalNumber is not defined` will occur because `decimalNumber` is not defined in the global scope.

// Try playing computer with the example to work out what is going on

Expand All @@ -15,6 +16,17 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// The first error occurs when attempting to run `console.log(decimalNumber)`, resulting in a `SyntaxError: Identifier 'decimalNumber' has already been declared`.
// This happens because `decimalNumber` is declared twice within the same scope: once as a parameter of the function and again as a constant inside the function.
// The second error is that `decimalNumber` is not defined in the global scope.
// Therefore, trying to log it outside the function will result in a `ReferenceError: decimalNumber is not defined`.
// Instead, the function should use the parameter `decimalNumber` directly without redeclaring it.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

console.log(convertToPercentage(0.5));
12 changes: 9 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

// Predict and explain first BEFORE you run any code...

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// A SyntaxError will occur because you cannot use a number such as `3` as a function parameter name.
// Names for parameters must be valid identifiers, for example, starting with a letter or underscore.

function square(3) {
return num * num;
}

// =============> write the error message here
// `SyntaxError: Unexpected number`

// =============> explain this error message here
// Function parameters are placeholders for the values that will be passed into the function.
// These placeholders must follow the same naming rules as variables, and a variable name cannot be a number.
// In this instance, the code `function square(3)` is attempting to declare a parameter with the name `3`, which is not a valid identifier, hence the `SyntaxError`.

// Finally, correct the code to fix the problem

// =============> write your new code here


function squareFixed(num) {
return num * num;
}
11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Predict and explain first...

// =============> write your prediction here
// The console will output two lines.
// 1. `320` (from inside the multiply function)
// 2. `The result of multiplying 10 and 32 is undefined`
// This is because the `multiply` function logs the result but does not return it, as functions that do not explicitly return a value return `undefined` by default.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +13,13 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The bug occurs because the `multiply` function uses `console.log()` to display the result instead of using the `return` keyword.
// When the function call is placed inside the template literal, it resolves to its return value, which is `undefined`.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiplyFixed(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiplyFixed(10, 32)}`);
11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// The output should be `The sum of 10 and 32 is undefined` given that the `return;` statement is on a line by itself before the actual calculation.
// This should cause the function to exit and return `undefined`.

function sum(a, b) {
return;
Expand All @@ -9,5 +11,14 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The `return` keyword immediately stops the execution of a function and returns a value.
// Here, `return;` is called without a value, so the function exits and returns `undefined`.
// As a result, the line `a + b;` is never executed.

// Finally, correct the code to fix the problem
// =============> write your new code here
function sumFixed(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sumFixed(10, 32)}`);
18 changes: 16 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output will be the same, `3`, for all three calls.
// This is because the function ignores its input parameter and always operates on the global constant `num`.

const num = 103;

Expand All @@ -15,10 +17,22 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

// Explain why the output is the way it is
// =============> write your explanation here
// The function `getLastDigit` is defined without any parameters, so it cannot use the numbers (42, 105, 806) being passed to it.
// Instead the function accesses the global constant `num` (which is 103) every time it is called.
// Therefore, it always resolves the last digit of 103, which is '3'.

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigitFixed(inputNumber) {
return inputNumber.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
console.log(`The last digit of 42 is ${getLastDigitFixed(42)}`);
console.log(`The last digit of 105 is ${getLastDigitFixed(105)}`);
console.log(`The last digit of 806 is ${getLastDigitFixed(806)}`);
7 changes: 5 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
}

console.log(`BMI for 82.4kg, 1.82m: ${calculateBMI(82.4, 1.82)}`);
8 changes: 8 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

function toUpperSnakeCase(inputString) {
return inputString.toUpperCase().replaceAll(" ", "_");
}

console.log(
`"a wizard of earthsea" becomes: ${toUpperSnakeCase("a wizard of earthsea")}`
);
21 changes: 21 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs

function toPounds(penceString) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

return `£${pounds}.${pence}`;
}

console.log(`"399p" converts to: ${toPounds("399p")}`);
8 changes: 8 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// The `pad` function will be called 3 times. This is because the return statement in `formatTimeDisplay` calls `pad` for `totalHours`, `remainingMinutes`, and `remainingSeconds`.

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// The value assigned to `num` will be `0`.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value will be the string `"00"`.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// The value assigned to `num` will be `1`.
// With an input of 61, `remainingSeconds` is calculated as `61 % 60`, which is `1`.
// The last call to `pad` in the `return` statement uses `remainingSeconds` as its argument.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// The return value will be the string `"01"`.
// The `pad` function takes the number `1`, converts it to the string `"1"`, and then uses `padStart(2, "0")` to add a leading zero, resulting in `"01"`.