Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
1d8957c
Add hello_world.js file
moses77-boop Sep 14, 2026
cb5fd1c
Add hello_world.js file with initial console log
moses77-boop Sep 14, 2026
3dcb7b7
Add fact.js file
moses77-boop Sep 14, 2026
14998ae
Add fun fact about sprints to fact.js
moses77-boop Sep 14, 2026
4b9c01a
Rename file fact.js to facts.js
moses77-boop Sep 14, 2026
386a826
Add greeting.js file
moses77-boop Sep 14, 2026
ed354a3
Add greeting message to greeting.js
moses77-boop Sep 14, 2026
02c297f
Add console log for greeting message in greeting.js
moses77-boop Sep 14, 2026
d3c778f
Update greeting message to include name in greeting.js
moses77-boop Sep 14, 2026
0411379
Change greeting variable from const to let and update its value to "G…
moses77-boop Sep 14, 2026
c6ad8eb
Add boolean.js file
moses77-boop Sep 14, 2026
d2880fb
Add boolean.js with initial console log comparisons
moses77-boop Sep 14, 2026
2bddb02
Add additional comparison console logs in boolean.js
moses77-boop Sep 14, 2026
3f06814
Add passwordChecker.js file
moses77-boop Sep 15, 2026
1b3d99b
Add password validation logic in passwordChecker.js
moses77-boop Sep 15, 2026
56326fb
Add else statement to password validation logic
moses77-boop Sep 15, 2026
cae8927
Update userInput for password validation test
moses77-boop Sep 15, 2026
ad792cd
Add adminPassword variable in passwordChecker.js
moses77-boop Sep 15, 2026
31d8426
Add else if condition to password validation logic in passwordChecker.js
moses77-boop Sep 15, 2026
0f097a3
Add exercise.js file
moses77-boop Sep 15, 2026
1d56388
Remove unused exercise.js file
moses77-boop Sep 15, 2026
220a714
Add response variable declaration in passwordChecker.js
moses77-boop Sep 15, 2026
762e97f
Update else condition to assign response message for incorrect passwo…
moses77-boop Sep 15, 2026
39b87c3
Log response message for incorrect password in passwordChecker.js
moses77-boop Sep 15, 2026
3a1178c
Change response declaration from const to let in passwordChecker.js
moses77-boop Sep 16, 2026
9558f43
Add prediction comment explaining SyntaxError in 0.js
moses77-boop Sep 21, 2026
667b02a
Call the capitalise function with a test string
moses77-boop Sep 21, 2026
0dcdd7f
Add comment about SyntaxError in 0.js
moses77-boop Sep 21, 2026
f76aa55
Add explanation for SyntaxError in capitalise function
moses77-boop Sep 21, 2026
5433734
Refactor capitalise function to avoid SyntaxError by renaming variable
moses77-boop Sep 21, 2026
24548db
Add refactored capitalise function to remove variable declaration con…
moses77-boop Sep 21, 2026
c094e79
Add prediction comment explaining potential SyntaxError in convertToP…
moses77-boop Sep 22, 2026
1552743
Add explanation for potential SyntaxError in convertToPercentage func…
moses77-boop Sep 22, 2026
9f517e7
Add explanatory comment for parameter shadowing in convertToPercentag…
moses77-boop Sep 22, 2026
36c8ab9
Add comments to clarify variable declaration conflict in convertToPer…
moses77-boop Sep 22, 2026
ff1d8b8
Add comments to clarify scope of 'decimalNumber' variable in convertT…
moses77-boop Sep 22, 2026
8f96ad7
Add clarification comment on variable scope for function parameters
moses77-boop Sep 22, 2026
31d1742
Comment out code in convertToPercentage function to prevent SyntaxErr…
moses77-boop Sep 22, 2026
e2a617b
Implement convertToPercentage function to calculate percentage from d…
moses77-boop Sep 22, 2026
8e24d75
Add comment to clarify parameter usage in square function
moses77-boop Sep 22, 2026
f2a0d61
Add comment to indicate expected SyntaxError in square function
moses77-boop Sep 22, 2026
ca723f9
Add expected SyntaxError message for square function parameter
moses77-boop Sep 22, 2026
409baf9
Add explanation for SyntaxError in square function parameter
moses77-boop Sep 22, 2026
c53c5ed
Comment out square function to correct SyntaxError
moses77-boop Sep 22, 2026
c50dd38
Implement square function to correctly return the square of a number
moses77-boop Sep 22, 2026
b4452ff
Add prediction for multiply function output
moses77-boop Sep 22, 2026
329939a
Update prediction and explanation for multiply function output
moses77-boop Sep 22, 2026
33f9efb
Refactor multiply function to return the product and update console l…
moses77-boop Sep 22, 2026
b89e5b7
Add prediction comment for sum function output
moses77-boop Sep 22, 2026
2d81e8b
Add explanation for sum function bug and unreachable code
moses77-boop Sep 22, 2026
7fc8853
Comment out the sum function and console log for debugging purposes
moses77-boop Sep 22, 2026
fee9c33
Implement sum function to correctly return the sum of two numbers and…
moses77-boop Sep 22, 2026
1768716
Add prediction comment for getLastDigit function output
moses77-boop Sep 22, 2026
d51b30d
Add output for getLastDigit function
moses77-boop Sep 22, 2026
d2547c9
Add explanation for getLastDigit function parameter issue
moses77-boop Sep 22, 2026
7d17723
Comment out getLastDigit function and related console logs for debugging
moses77-boop Sep 22, 2026
2fd1da4
Implement getLastDigit function to return the last digit of a number …
moses77-boop Sep 22, 2026
91327e6
Add comment for irrelevant variable declaration in getLastDigit function
moses77-boop Sep 22, 2026
feee6ba
Implement calculateBMI function to compute BMI based on weight and he…
moses77-boop Sep 22, 2026
acaa7de
Implement error handling clause for height parameter
moses77-boop Sep 22, 2026
efb0135
Fix console log to include height parameter in calculateBMI function
moses77-boop Sep 22, 2026
8e26e40
Implement toUpperSnakeCase function to convert strings to UPPER_SNAKE…
moses77-boop Sep 23, 2026
7ea0781
Add console log to demonstrate toUpperSnakeCase function output
moses77-boop Sep 23, 2026
e3228c5
Implement toPounds function to convert pence strings to formatted pounds
moses77-boop Sep 23, 2026
93a453c
Add additional test cases for toPounds function
moses77-boop Sep 23, 2026
4c05247
Add logging to track pad function calls in formatTimeDisplay
moses77-boop Sep 23, 2026
a5a7c63
Add explanation for pad function calls in formatTimeDisplay
moses77-boop Sep 23, 2026
0fe7ef9
Commented-out code and logging related to pad function calls in time-…
moses77-boop Sep 23, 2026
ac9bd5d
Implement function to track first number passed to pad function
moses77-boop Sep 23, 2026
2f14c92
Add explanation for first call to pad
moses77-boop Sep 23, 2026
b148d65
Commented-out code for firstNum assignment in pad function
moses77-boop Sep 23, 2026
fb5b6b5
Implement function to track first pad return value
moses77-boop Sep 23, 2026
b6de5f3
Add explanation for first call to pad function return value
moses77-boop Sep 23, 2026
e642a44
Comment out unused variables and code in pad function
moses77-boop Sep 23, 2026
36e4464
Implement pad function to track last num value
moses77-boop Sep 23, 2026
9953c8b
Add explanation for last call to pad function
moses77-boop Sep 23, 2026
ad65ef3
Comment out lastNum variable and its assignment in pad function
moses77-boop Sep 23, 2026
4f756c0
Implement function to track last pad return value
moses77-boop Sep 23, 2026
c5f7033
Add explanation for return value of last pad call
moses77-boop Sep 23, 2026
bfbaf1b
Comment out formatAs12HourClock function and its related assertions
moses77-boop Sep 24, 2026
6fc17ad
Implement formatAs12HourClock function with input validation and time…
moses77-boop Sep 24, 2026
38ee172
Add check functions for formatAs12HourClock to validate outputs and e…
moses77-boop Sep 24, 2026
c72aac2
Refactor formatAs12HourClock function and add comprehensive test case…
moses77-boop Sep 24, 2026
ebe4d77
Delete prep/boolean.js
moses77-boop Sep 24, 2026
be45ad6
Delete prep/facts.js
moses77-boop Sep 24, 2026
42c8e32
Delete prep directory
moses77-boop Sep 24, 2026
a818573
Refactor capitalise function to remove unnecessary variable assignment
moses77-boop Sep 24, 2026
117c886
Refactor convertToPercentage function to simplify return statement
moses77-boop Sep 24, 2026
ce0df58
Rename parameter in square function for clarity
moses77-boop Sep 24, 2026
ca134f6
Fix sum function return statement formatting for clarity
moses77-boop Sep 24, 2026
b5f6fa6
Refactor calculateBMI function to directly return BMI value
moses77-boop Sep 25, 2026
8613d97
Add BMI calculation with structuring
moses77-boop Sep 25, 2026
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
26 changes: 21 additions & 5 deletions Sprint-3/1-key-errors/0.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job fixing the scope error.

Quick question: since the template string already produces the exact result you need, could you return it directly without reassigning the parameter, and how might that improve code simplicity?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for feedback! I think I might have got carried away. I fixed it.
It would improve code simplicity as it cuts out an unnecessary step.

Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction
// A SyntaxError is thrown with message: Identifier 'str' has already been declared

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// Error message interpreted ====> The error message simply means the name 'str' was declared twice in the same scope

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }
// capitalise("moses");
// =============> write your explanation here
// The error here is because the parameter 'str' and the variable 'let str' share the same name in he same scope, thus the SyntaxError.
// To fix this error, I could either give the variable a different name or reassign the parameter 'str' without 'let', since no new declaration is made

// =============> write your new code here
// function capitalise(str) {
// let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
// return capitalised;
// }
// console.log(capitalise("moses"));
// |
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("moses"));
26 changes: 19 additions & 7 deletions Sprint-3/1-key-errors/1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job spotting the scope conflict and fixing the code.

Quick question: since decimalNumber * 100 is already a short expression, could you return it directly inside the template string without declaring a separate percentage variable?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I just enjoy adding more details to my code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// Predict and explain first...
// My prediction is the program would throw a SyntaxError, because 'decimalNumber' has already been declared as a parameter and also has been declared as a new variable in the same scope

// Why will an error occur when this program runs?
// =============> write your prediction here
//An error will occur because 'const decimalNumber = 0.5' clashes with the parameter of the same name.

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
// function convertToPercentage(decimalNumber) {
// // The parameter 'decimalNumber' is created as a variable inside the function's scope.
// const decimalNumber = 0.5;
// // tries to declare new variable called 'decimalNumber' in the same scope, where one already exists as a parameter
// // cannot declare same name twice in one scope with 'const'
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
// return percentage;
// }
// // As a result of the conflict nothing runs.
// console.log(decimalNumber);
// // This line is outside the function and would not parse, as 'decimalNumber' only exists inside 'convertToPercentage' as a parameter.

// =============> write your explanation here
// Parameters and variables declared inside a function cannot be accessed outside it.

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

}
console.log(convertToPercentage(0.5));
16 changes: 12 additions & 4 deletions Sprint-3/1-key-errors/2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job identifying why passing a number literal in a function definition causes a SyntaxError and fixing it with a proper parameter name.

Quick question: when naming parameters like n, how could choosing a more descriptive name (like number) help anyone reading your code understand its purpose even faster?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@

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

// The parameter is written as a value instead of a variable name
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// The program will throw a SyntaxError

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

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

// =============> explain this error message here
// In the 'function square(3)' JavaScript parser expected a valid parameter name it could use as a variable
// Since '3' is a number literal, not a valid identifier, the parser cannot understand it and throws a syntax error.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(number){
return number * number;
}
console.log(square(77));


20 changes: 16 additions & 4 deletions Sprint-3/2-mandatory-debug/0.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job tracing the execution order and explaining why functions without a return statement default to returning undefined.

Quick question: since the function now uses return a * b, why is returning the value rather than using console.log inside the function considered a best practice in JavaScript?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
Using return is considered as best practice because a function that only uses 'console.log' would not be able to parse output in an expression, but a function that returns a value stays flexible, I could choose not to log it.

Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// Predict and explain first...

// =============> write your prediction here
// My prediction is '320' would be printed first followed by "The result of multiplying 10 and 32 is undefined"
// This occurs because 'multiply()' does not return anything

function multiply(a, b) {
console.log(a * b);
}
// function multiply(a, b) {
// console.log(a * b);
// }

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The template literal calls 'multiply(10, 32) to build the string
// Inside multiply, 'console.log(a * b)' runs immediately, printing '320'
// This happens before the outer 'console.log' line finishes, because JavaScript has to evaluate 'multiply(10, 32)' first to know what to put in the template
// 'multiply's' function body only contains a 'console.log' statement without a 'return' keyword
// A function with no explicit return statement returns 'undefined' by default.

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

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
20 changes: 15 additions & 5 deletions Sprint-3/2-mandatory-debug/1.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job spotting how automatic semicolon insertion affects the return statement and fixing it.

Quick question: since the expression a + b is short and simple, why might keeping it on the same line as return (like return a + b;) be preferred over putting it on a new line inside parentheses?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I enjoy detailing my code. I'll fix it.

Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// The code seems like it would log 'The sum of 10 and 32 is 42' since function 'sum(10, 32)' seems to add two numbers

function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// function sum(a, b) {
// return;
// a + b;
// }

// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// When the code was run it logged 'The sum of 10 and 32 is undefined'
// The bug is in line 7
// 'return' is automatically assigned a semi-colon if it is followed by a line break, making line 8 unreachable

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b){
return a + b;
}
console.log (`The sum of 10 and 32 is ${sum(10, 32)}`);
30 changes: 23 additions & 7 deletions Sprint-3/2-mandatory-debug/2.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job identifying how the global constant variable shadowed the arguments and correctly refactoring the function to accept a parameter.

Quick question: why is passing values directly as parameters considered safer and more flexible than relying on outer variables like num?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
Passing values directly into a function as parameters is generally considered safer and more flexible than relying on outer variables like num because when a value is passed as a parameter, every call to the function uses it's own specific value. This was why the original code failed, because the function ignored the parameter that was passed to it and instead always referred to the same outer variable, num.

Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

// Predict the output of the following code:
// =============> Write your prediction here
// I predict the output would print the last digits of '42', '105', and '806' because the function name is 'getLastDigit' and it's been called by the input values as arguments

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// function getLastDigit() {
// return num.toString().slice(-1);
// }

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
// 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 bug is in line 9, where 'getLastDigit()' is declared with no parameters
// Instead the function body reaches out to 'const num = 103' and disregards the values passed because the function does not declare any parameter

// Finally, correct the code to fix the problem
// =============> write your new code here
// const num = 103 ---- becomes irrelevant
function getLastDigit(n){
return n.toString().slice(-1)
}
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
14 changes: 13 additions & 1 deletion Sprint-3/3-mandatory-implement/1-bmi.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job writing the calculateBMI function, handling edge cases for height, and using .toFixed(1) to return a clean string.

Quick question: since .toFixed() converts the number into a string, how might that affect any downstream code if another function tries to perform further mathematical calculations directly on the result of calculateBMI?

@moses77-boop moses77-boop Sep 25, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
From research using turns into a string, rather than real numbers, so if further mathematical calculations are peformed directly on that result in some cases, it produces the wrong answer by joining the values together as text instead of adding them numerically and in other cases it fails strict equality comparison since a string and a number are never treated as equal.
I have made changes to the code so it can handle further mathematical calculations.

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
if (!height || height <= 0){ // to catch cases of undefined and null
throw new Error('Height must be greater than 0');
}
const heightSquared = height ** 2;
return weight / heightSquared;
}
function formatBMI(bmi){
return bmi.toFixed(1); // rounds 'bmi' to 1 decimal place and converts to a string
// return the BMI of someone based off their weight and height
}
}

const bmiFurMath = calculateBMI(70, 1.73) + 7;
const displayBMI = formatBMI(bmiFurMath);
console.log(`The Body Mass Index is: ${displayBMI}`);
8 changes: 8 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job writing a robust function that handles extra spaces and validates the input type.

Quick question: since this is a fundamentals exercise, do you think using a regular expression like /\s+/ is necessary, or could a simpler method like .replaceAll() achieve the same result?

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(input){
if (typeof input !== "string"){
throw new Error("Input must be a string")
}
return input.trim().split(/\s+/).join("_").toUpperCase();
}
console.log (toUpperSnakeCase("lord of the rings season 1 episode 1"));
15 changes: 15 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job transforming the snippet into a reusable function and effectively handling different input lengths using string methods like padStart and padEnd.

Quick question: since your logic relies on removing the trailing 'p', how might your function behave if an input accidentally omits the 'p' or includes extra whitespace?

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@
// 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(toPounds("77770p"));
console.log(toPounds("2570p"));
console.log(toPounds("399p"));
console.log(toPounds("77p"));
console.log(toPounds("0p"));
34 changes: 34 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Great job tracing the execution step by step and using temporary tracking variables to verify your answers.

Quick question: while using global variables helped you trace the code here, why can relying on global variables for tracking state be risky in larger applications?

Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
// let padCalls = 0;
// let padCallCount = 0;
// let firstNum;
// let lastNum;
let lastReturn;

function pad(num) {
// padCalls++;
// padCallCount++;
// if (padCallCount === 1){
// firstNum = num;
// }
// lastNum = num;
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
}
// if (padCallCount === 1){
// firstReturn = numString;
// }
lastReturn = numString;
return numString;
}

Expand All @@ -14,6 +30,13 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
// for (const s of [8784, 77, 77777]){
// padCalls = 0;
// const result = formatTimeDisplay(s);
// console.log(`formatTimeDisplay(${s})==> ${result}| pad called ${padCalls} times `);
// }
formatTimeDisplay(61);
console.log("The return value of pad when it is called for the last time is:", lastReturn);

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions
Expand All @@ -22,17 +45,28 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// The 'pad' function is called "three times" within the 'formatTimeDisplay' function
// The count will always be three because the 'return' statement contains three 'pad()' calls

// 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
// for the first call to pad, num is assigned the value of 'totalHours', which is '0'
// Since the template literal is evaluated from left to right, calling 'formatTimeDisplay(61)', the calls occur in this order:
// totalHours, remainingMinutes, and remainingSeconds

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// For the first call to 'pad', 'num' is '0'
// The pad function will return '00' because it pads single-digit numbers with a leading zero

// 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 last call is 'pad(remainingSeconds)' which is '61 % 60'
// since 61 divided by 60 leaves a remainder of 1, it means the value '1' is assigned to 'num' in the last call of pad

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// The return value of pad when called for the last time is '01'
// since '1.toString()' is '1', the 'while' loop runs once and adds a '0' to the front, giving '01'
Loading