Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here
// I guess the function is trying to capitalise the first letter of the string,
// capture the rest of the string from index 1 to the end of the string,
// and concatenate them together to return the capitalised string

// 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
// After calling the function capitalise with a string input,
// I got a syntax error that "str" has already been declared.
// This is because str has already been declared as a parameter of the function,
// when a new variable named str, it causes a conflict.

// =============> write your new code here
// I renamed the new variable to capitalisedStr to avoid the conflict

function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}
console.log(capitalise("hello")); // should return "Hello"
21 changes: 20 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// Predict and explain first...
// At the first glance, I believe the function would occur errors because the variable decimalNumber is declared twice.
// Besides, console.log is trying to log a variable that is not defined in global scope.

// Why will an error occur when this program runs?
// =============> write your prediction here
// The first error should be a syntax error that "decimalNumber" has already been declared.
// Since decimalNumber is declared as a parameter of the function,
// when a new variable also named decimalNumber is declared inside the function,
// it causes a conflict.

// Try playing computer with the example to work out what is going on
// The second error should be a reference error that "decimalNumber is not defined".

// Try playing computer with the example to work out what is going on
/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +21,19 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

// =============> write your explanation here
// After running the code, I got a syntax error that "decimalNumber" has already been declared.
// I also found out that the decimalNumber inside the function should be declared again.
// Besides, if we want to log the result of the function, we should call the function with an argument inside console.log().

// 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)); // should return "50%"
console.log(convertToPercentage(0.75)); // should return "75%"
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

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

// I guess the error is a reference error because the parameter is not defined correctly.
/*
function square(3) {
return num * num;
}

*/
// =============> write the error message here
// function square(3) {
// ^
//SyntaxError: Unexpected number
// In fact, the error is a syntax error because number 3 is unexpected here.

// =============> explain this error message here
// Number 3 is not a valid parameter name, which is misplacing and causes a syntax error.

// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here

// It seems the function multiply did not return any value.
// Therefore, it is like a console.log returning undefined, while it is inside another console.log.
// So the output should be "The result of multiplying 10 and 32 is undefined".
/*
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
// After running the code, I got a separate output of 320 and "The result of multiplying 10 and 32 is undefined".
// This is because the function multiply does not return any value, so it returns undefined by default.
// The console.log inside the function multiply prints 320 when the function is called.

// 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)}`); // should return "The result of multiplying 10 and 32 is 320"
12 changes: 11 additions & 1 deletion Sprint-2/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
// It looks like the function sum return nothing because a+b is written in the next line after return statement.
// Therefore, the output should be "The sum of 10 and 32 is undefined".

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

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

*/
// =============> write your explanation here
// After running the code, I got "The sum of 10 and 32 is undefined".
// The return statement ends the function execution and a+b is never evaluated.

// 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)}`); // should return "The sum of 10 and 32 is 42"
23 changes: 21 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The output should always be 3 because the parameter is not defined in the function,
// and the variable num is always 103 in the global scope.

const num = 103;

/*
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
/*
The last digit of 42 is 3
The last digit of 105 is 3
The last digit of 806 is 3
*/
// As expected, the output is always 3.

// Explain why the output is the way it is
// =============> write your explanation here
//The num is defined in the global scope as 103.
// And the function does not accept any parameters.
// So every time the function is called, it returns the last digit of 103, which is 3.

// 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)}`); // should return 2
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // should return 5
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // should return 6

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
8 changes: 6 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,9 @@
// 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
}
// return the BMI of someone based off their weight and height
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

console.log(calculateBMI(70, 1.73)); // should return 23.4
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(str) {
// return the string in UPPER_SNAKE_CASE
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there")); // should return "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // should return "LORD_OF_THE_RINGS"
61 changes: 61 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,64 @@
// 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

/* Code from Sprint-1 below for reference
const penceString = "399p";

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");

console.log(`£${pounds}.${pence}`);
*/

function toPounds(penceString) {
if (typeof penceString !== "string" || !penceString.endsWith("p")) {
console.log("Invalid input, please provide a penceString ending with 'p'");
return "Invalid input, please provide a penceString ending with 'p'";
}

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

if (Number.isNaN(Number(penceStringWithoutTrailingP))) {
console.log("Invalid input, please provide a numeric penceString");
return "Invalid input, please provide a numeric penceString";
}

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

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

console.log(`£${pounds}.${pence}`);
return `£${pounds}.${pence}`;
}

toPounds("399p");
toPounds("5p");
toPounds("89p");
toPounds("1234p");
toPounds("0p");
toPounds("70000p");
toPounds("abcp");
toPounds(500);
toPounds("abc");
19 changes: 19 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
let timesToBeCalled = 0;

function pad(num) {
timesToBeCalled += 1;
console.log(
` Pad has been called ${timesToBeCalled} times and num is ${num}`
);
console.log(` The return value will be ${num.toString().padStart(2, "0")}`);
return num.toString().padStart(2, "0");
}

Expand All @@ -11,24 +18,36 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));

// 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
// The pad function will be called 3 times when formatTimeDisplay is called.
// 1st: for totalHours
// 2nd: for remainingMinutes
// 3rd: for 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 is 0 when pad is called for the first time because 61 seconds is 0 hours, 1 minute, and 1 second.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad is "00" in string format because 0 is padded to 2 digits with leading zeros.

// 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 is 1 when pad is called for the last time.
// It is because the remaining seconds is 1 second after taken 60 seconds for 1 minute.

// 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 assigned to num is "01" in string format.
// It is because 1 second is padded to 2 digits with a leading zero.
Loading