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
7 changes: 7 additions & 0 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ function capitalise(str) {
}

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

// The code will produce a SyntaxError because str is declared twice as a function parameter and again with let.
// =============> write your new code here

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

// An error will occur because decimalNumber is declared twice inside the function, and
// decimalNumber is also not accessible outside the function.

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

function convertToPercentage(decimalNumber) {
Expand All @@ -18,3 +21,10 @@ console.log(decimalNumber);

// 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));
9 changes: 8 additions & 1 deletion Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// A SyntaxError will occur because 3 cannot be used as a function parameter.

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

// =============> write the error message here
// SyntaxError: Invalid or unexpected token

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

// Function parameters must be variable names and not a number,
// hence this is a syntaxError

// Finally, correct the code to fix the problem

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

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

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

// =============> write your prediction here
// It will probably print 320 so it will multiply the 10 by 32 and then undefined because the multiply() is not returning anything

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

// =============> write your explanation here
// The function prints 320, but because there is no return statement, multiply(10, 32) evaluates to undefined.

// 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)}`);
7 changes: 7 additions & 0 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// The function will return undefined,

function sum(a, b) {
return;
Expand All @@ -9,5 +10,11 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The return; statement ends the function immediately. Therefore, a + b is never executed.
// 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)}`);
19 changes: 19 additions & 0 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

const num = 103;

Expand All @@ -15,10 +18,26 @@ 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 function doesn't accept a parameter. It always uses the global variable:
// Finally, correct the code to fix the problem
// =============> write your new code here

const num = 103;

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
3 changes: 3 additions & 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,7 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
const bmi = weight / (height * height);
return bmi.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(" ", "_");
}
10 changes: 10 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,13 @@
// 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(pence) {
const pounds = Math.floor(pence / 100);
const remainingPence = pence % 100;

return `£${pounds}.${remainingPence.toString().padStart(2, "0")}`;
}

console.log(toPounds(399));
console.log(toPounds(100));
console.log(toPounds(1250));
6 changes: 5 additions & 1 deletion Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 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
// 1

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

// 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 — The last call to pad is for remainingSeconds, which is 1 when seconds is 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
// "01" — pad(1) converts 1 to a string and adds "0" to the beginning.
Loading