Skip to content
Open
3 changes: 1 addition & 2 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const pounds = paddedPenceNumberString.substring(
);

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

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

Expand Down
12 changes: 12 additions & 0 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Predict and explain first...
// =============> write your prediction here

// Prediction: the method toUpperCase return the character at index 0 of str as uppercase.
// slice method will return the characters of str starting at index 1 excluding the first character.
// the template literal combines the converted upper case first character and the rest of the string.

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

Expand All @@ -10,4 +14,12 @@ function capitalise(str) {
}

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

// Explanation: 1. str declares twice.Hence it gives syntax error.
// Refactor: 2. instead of returning str return can directly send back the template literal expression.

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

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
14 changes: 14 additions & 0 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// Prediction:
// 1. decimalNumber is declared twice and it will give syntax error.

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

function convertToPercentage(decimalNumber) {
Expand All @@ -16,5 +19,16 @@ console.log(decimalNumber);

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

// 2. console.log will return undefined as decimalNumber local scope declaration inside the function.
// 3. It's percentage not decimalNumber needs printing.

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

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

console.log(convertToPercentage(0.5));

// The function convertToPercentage can be called passing with an argument of the input.
15 changes: 15 additions & 0 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@

// =============> write your prediction of the error here

// Prediction:
// 1. This will give reference error.
//


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

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

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

// The function expects a parameter name and it shouldn't start with a number.
// num should be the parameter and later 3 can be passed as an argument.

// Finally, correct the code to fix the problem

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

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

console(square(3));

10 changes: 10 additions & 0 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

// 'The result of multiplying 10 and 32 is 10 * 32' is printed.

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

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

// the function prints a * b. When presented with multiply(10, 32) it prints 10 * 32.

// 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)}`);
15 changes: 14 additions & 1 deletion Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

// it will return syntax error

function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +11,16 @@ function sum(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

// There is a semicolon after return.

// Finally, correct the code to fix the problem.
// Later I discovered having a line between return and a + b gives another undefined error message
// =============> write your new code here

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

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

25 changes: 25 additions & 0 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Predict the output of the following code:
// =============> Write your prediction here

// The last digit of 42 is 3 undefined
// The last digit of 105 is 3 undefined
// The last digit of 806 is 3 undefined

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +19,31 @@ 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

// Because the function is not given a parameter and it is being passed an argument the same time which it ignores.
// The function uses the global variable num instead and print 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)}`);
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

// The reason was the num variable wasn't declared as a parameter inside the function.
// At the same time the function is receiving arguments which the function ignores.
5 changes: 3 additions & 2 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place


function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1);
}
4 changes: 4 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,7 @@
// 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(" ", "_");
}
13 changes: 13 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,16 @@
// 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 toPound(priceInPence) {
const penceDigits = priceInPence
.substring(0, priceInPence.length - 1)
.padStart(3, "0");

const poundsPart = penceDigits.substring(0, penceDigits.length - 2);

const pencePart = penceDigits.substring(penceDigits.length - 2);

return `£${poundsPart}.${pencePart}`;
}
console.log(toPound("399p"));
1 change: 1 addition & 0 deletions Sprint-3/4-mandatory-interpret/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3-topounds.js
12 changes: 12 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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
Expand All @@ -23,16 +24,27 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

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

// The value that assigned to num is 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here

// The return value of pad 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

// The value assigned to num is 1. This is because 1 is the last character in the string 61

// 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 is "01". when pad is called for the last time it takes the last character of the string 61.
// After checking remainingSeconds length which is less than 2 it adds a "0" and returns "01"