Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
38c5f48
Predict and explain errors. Fix by removing let so not to make new va…
MahirShah300 Sep 23, 2026
b862d6d
Predict and explain errors. Fix by removing line and making variable …
MahirShah300 Sep 23, 2026
d311b62
Predict and explain error. Fix by giving adding variable name to para…
MahirShah300 Sep 23, 2026
d9257c2
Create function to calculate BMI
MahirShah300 Sep 24, 2026
8dcfe07
Create function to return upper snake case string
MahirShah300 Sep 24, 2026
9ff0a0b
Take previous code and made into function toPounds
MahirShah300 Sep 24, 2026
ebffeae
Predict and explaind bug. Fix by changing function to return a * b
MahirShah300 Sep 25, 2026
818cc32
Predict and explain bug. Fix by moving a + b before ; in return line …
MahirShah300 Sep 25, 2026
b0260ef
Predict and explain bug. Fix by removing num declaration and adding n…
MahirShah300 Sep 25, 2026
f54452f
Answer questions
MahirShah300 Sep 28, 2026
38825cb
Fix bugs, extend code to catch different inputs. Write tests
MahirShah300 Sep 28, 2026
ab74a01
Fix formatting
MahirShah300 Sep 28, 2026
d27c7d8
Add variable capitaliseStr instead of changing original string
MahirShah300 Oct 1, 2026
508e466
Remove default parameter from square function
MahirShah300 Oct 1, 2026
92cdff7
Made fix to catch cases of empty strings, non-string inputs, check fo…
MahirShah300 Oct 1, 2026
ad83dc0
Fix toPounds function to return string template
MahirShah300 Oct 2, 2026
d643752
Add validation check for whitespace in input string
MahirShah300 Oct 2, 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
17 changes: 13 additions & 4 deletions Sprint-3/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
// the function has a parameter str, but then tries to make a new
// variable also called str. so maybe cause error because str already exists

// 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;
}
//function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
//}

// =============> write your explanation here
// SyntaxError: Identifier 'str' has already been declared
// Prediction was correct, str already exists
// =============> write your new code here

function capitalise(str) {
const capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitaliseStr;
}
34 changes: 28 additions & 6 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
//decimalNumber already exists as a parameter, so const decimalNumber is making
//a new variable with the same name. There is also a problem where decimalNumber is being
//assigned 0.5, which won't cause errors, but means that no matter what is passed to the function
//decimalNumber will always be 0.5

// 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) {
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
//}

console.log(decimalNumber);
//convertToPercentage(0.6)
//console.log(decimalNumber);

// =============> write your explanation here
//SyntaxError: Identifier 'decimalNumber' has already been declared
//prediction correct, decimalNumber already exists so causes error. Removing the line
//shows another error
//console.log(decimalNumber);
// ^
//ReferenceError: decimalNumber is not defined
//This is because of scope, decimalNumber is defined in the function, so it only has scope
//in the function

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

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

return percentage;
}

let decimalNumber = 0.6;
console.log(decimalNumber);
console.log(convertToPercentage(decimalNumber));
20 changes: 13 additions & 7 deletions Sprint-3/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
// In the function, 3 hasn't been assigned to num, so num is NaN and should return NaN.
//Also when defining a function, the parameter should have the variable name, not a value
//to pass in, unless you want to default value such as num = 3, which assigns 3 to num if
// no value is passed in

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

//syntaxError means there is a mistake in the way the code is written, so javascript can't
// interpret the code. in this case there is a number here when there shouldn't be.
// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
21 changes: 16 additions & 5 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Predict and explain first...

// =============> write your prediction here
//The multiply function doesn't return anything, only prints to console so the function call will have value undefined.
//So when line 12 is executed, first it will print "The result of multiplying 10 and 32 is undefined"
//Then on a new line it prints 320

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

//It first printed 320, then printed "The result of multiplying 10 and 32 is undefined" on the next line
//I think this is because it goes through the line 12, executes the multiply function call first, then finishes the
//console.log function call.
// 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)}`);
18 changes: 14 additions & 4 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
//when the return line is executed, the function exits. a + b is a separate line because it
//is after the semicolon, so a + b is never reached and not returned. So line 11 will print
//The The sum of 10 and 32 is undefined

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

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

// =============> write your explanation here
//It did as predicted, for the reason I expected
// 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)}`);
33 changes: 25 additions & 8 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
// Predict and explain first...

// Predict the output of the following code:
//The last digit of 42 is 3
//The last digit of 105 is 3
//The last digit of 806 is 3
// =============> Write your prediction here
//num is defined outside of the function, so has scope in the function getLastDigit.
//the function doesn't take any arguments and uses num, so will always return 3, no matter what
//is passed to the function because it will use num, which is set to 103

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
// As I predicted, for the reason above
// 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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
1 change: 1 addition & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
return (weight / height ** 2).toFixed(1);
}
3 changes: 3 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
// 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(" ", "_");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why was the replaceAll used ? does this print exactly what is expected or does it add some additional strings which were not part of input string ?

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.

replaceAll goes through a string and removes all cases of the first input, and replaces it with the second input. It prints what is expected, and the only thing added are _ in places of " ". If there's any unexpected whitespace " " after a string, they would replaced with _

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clear answer, thanks.

}
25 changes: 25 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,28 @@
// 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}`);
}

toPounds("10p");
toPounds("100p");
toPounds("232134p");
toPounds("238p");
toPounds("5234p");
10 changes: 10 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ function formatTimeDisplay(seconds) {

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

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

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// "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
// 1. Last time it is called is pad(remainingSeconds). remainingSeconds = seconds % 60
// with seconds = 61 this expression evaluates to 1, so when 1 is passed to pad, num is
// assigned 1.

// 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
// "01". Num is 1 which converted to the string "1" which is assigned to numString.
// The while loop runs while numString length less than 2, and adds "0"
// to the beginning string. So "1" becomes "01". the string length is now equal to two,
// so the while loop exits and numString is returned with value "01"
Loading
Loading