Skip to content
Open
19 changes: 14 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: We'll get an error message because "str" paramater has been declared already,
//using it again inside the function will clash due to redaclaration.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here: Identifier 'str' has already been declared,
//redaclaration in the same scope throws up the error message
// =============> write your new code here:
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
let newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return newStr;
}

// =============> write your explanation here
// =============> write your new code here
//Above code works because we have used another variable "newStr", avoiding redeclaration.
Copy link

@ykamal ykamal Mar 3, 2026

Choose a reason for hiding this comment

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

This is good. But do you think there could be another way to do this? One that doesn't require you to declare any variables at all?

Copy link
Author

Choose a reason for hiding this comment

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

function capitalise(str) {
return str[0].toUpperCase() + str.slice(1);
}

Could this work? Just by removing the unnecessary variable?

Copy link

Choose a reason for hiding this comment

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

Yes. Less memory, simpler. Well done


43 changes: 35 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here: potential error, decimalNumber has already been declared, can't redaclare it again with a new variable in the same scope.
//another error could be, console.log will try to access a local variable from the global scope.

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

// step one: function convertToPercentage(decimalNumber)
// function created with a parameter named (decimalNumber)
// step two: const decimalNumber = 0.5;
// error occurs because decimalNumber has been declared already in the fucntion, cannot declare another one with the same name , in the same scope.
// program stops.
// we have fixed the first error. Code runs again.
// step three: const percentage = `${decimalNumber * 100}%`;
// new variable called percentage created, then the JS code is inserted, where decimalNumber is multiplied by 100.
// no errors here
// step four: return percentage;
// this line sends the value out of the function then stops the function. No errors so far.
// Step five: console.log(decimalNumber);
// error occurs again, because the value created in step four can't be stored in console.log, due to decimalNumber being a parameter inside the function. Outside the function
// there is no variable named decimalNumber.
// we have fixed console.log with (convertToPercentage(0.5), now the result will print properly.



//function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

// return percentage;
//}

//console.log(decimalNumber);

// =============> write your explanation here: my error predictions turned out to be true and i have played computer with js code to further illustrate my reasoning.

// Finally, correct the code to fix the problem
// =============> write your new code here:

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
Copy link

Choose a reason for hiding this comment

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

This is good. But do you think there could be another way to do this? One that doesn't require you to declare any variables at all?

Copy link
Author

Choose a reason for hiding this comment

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

function convertToPercentage(decimalNumber) {
return ${decimalNumber * 100}%;
}

console.log(convertToPercentage(0.5));

Copy link

Choose a reason for hiding this comment

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

Yes similar to before

Copy link

Choose a reason for hiding this comment

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

Make sure to retain the string templating

}

console.log(decimalNumber);

// =============> write your explanation here
console.log(convertToPercentage(0.5));

// Finally, correct the code to fix the problem
// =============> write your new code here
26 changes: 19 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@

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

// =============> write your prediction of the error here
// =============> write your prediction of the error here: error one, 3 cannot be a parameter name, parameters must be variable names, not values.
// Error two, num is not defined. There is no variable named num, it would need to match the name inside the function.

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

// =============> write the error message here:

//Uncaught SyntaxError: Unexpected number

// =============> write the error message here
//Uncaught SyntaxError: Illegal return statement

// =============> explain this error message here
// =============> explain this error message here:

// first error message came up because JS found a number instead of a variable for the function.
// second error message came up because in JS return has to be inside of a function.

// Finally, correct the code to fix the problem

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

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


21 changes: 15 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Predict and explain first...

// =============> write your prediction here
// =============> write your prediction here: there will be an error, due to console.log being used incorrectly. Console.log is for displaying but there is no return command to
//clarify what to send to console.log

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

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

// =============> write your explanation here: I have run the code on node.js and got 'undefined' as an error message. I shall add 'return' instead of the first 'console.log'
//so that the result can be displayed properly in the second console.log.

// Finally, correct the code to fix the problem
// =============> write your new code here:

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

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

// =============> write your explanation here

// Finally, correct the code to fix the problem
// =============> write your new code here
23 changes: 16 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: js is a sequential language, as in reads code line by line, so i predict nothing will happen after 'return' is processed and
//some sort of an error message will appear.

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

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

// =============> write your explanation here: 'undefined' error appeared. Fixed the code by adding the addition on the same line as return.


// Finally, correct the code to fix the problem
// =============> write your new code here:

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

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

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
34 changes: 24 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// Predict the output of the following code
// =============> Write your prediction here: i think, the console.log will only show '3' because the paramater hasn't been specified in the function.

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

function getLastDigit() {
//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)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here: I've run the code and got '3' in each console.log line.

// Explain why the output is the way it is
// =============> write your explanation here: It's because there is no parameter in the function to differentiate viarable 103 from 42, 105, 806.


// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(num) {
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)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

// it works now, but since cosnt num = 103 is unused, i have removed it.

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
10 changes: 8 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
function calculateBMI(weight, height) {
const bmi = weight / (height * height);
Copy link

Choose a reason for hiding this comment

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

There's a division here. Do you think we need to put in any specific safety guards?

Copy link
Author

Choose a reason for hiding this comment

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

We should guard against height being 0 so we could add the following guard:

if (height <= 0) {
return "Height must be greater than 0";
}

Copy link
Author

Choose a reason for hiding this comment

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

@ykamal I think i have fixed all three problems you have raised, could you please have another look?

Copy link

Choose a reason for hiding this comment

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

Less than 0 is still fine. It's 0 that's the problem.

Copy link
Author

Choose a reason for hiding this comment

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

function calculateBMI(weight, height) {
if (height === 0) {
throw new Error("Height cannot be 0");
}

const bmi = weight / (height * height);
return Number(bmi.toFixed(1));

}

@ykamal i have changed the guards for 0 specifically

Copy link

Choose a reason for hiding this comment

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

Great, thanks. Ah, it's a body weight thing, so less than zero would be a problem 😂😂 but code wise this is fine

return Number(bmi.toFixed(1));
}

console.log(calculateBMI(88, 1.76));

// return the BMI of someone based off their weight and height
}
}
7 changes: 7 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,10 @@
// 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(str) {
return str
.toUpperCase()
.replaceAll(" ", "_");
}
console.log(toUpperSnakeCase("hello there"));
17 changes: 17 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,20 @@
// 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}`;
}
23 changes: 9 additions & 14 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
function pad(num) {
return num.toString().padStart(2, "0");
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
console.log(formatTimeDisplay(61));
console.log(formatTimeDisplay(6671));
console.log(formatTimeDisplay(832));

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// 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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> write your answer here: pad will be called 3 times.

// 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
// =============> write your answer here: The value is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here: The return value is '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
// =============> write your answer here: The value is 1 because 61 % 60 leaves 1 second remaining.

// 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
// =============> write your answer here: The return value is '01' because return num.toString().padstart(2, '0') formats 1 into '01'.
Loading