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
23 changes: 18 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
// Predict and explain first...
// =============> write your prediction here
// =============> SyntaxError: Identifier 'str' has already been declared

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

// =============> str has already been declared in the function. let can not redeclare a variable with the same name in the same scope. parameter, so we cannot declare it again with let. This will cause a syntax error. To fix this, we should remove the let keyword when assigning the new value to str.
// =============> write new code here

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello"));

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

//console.log(capitalise("hello"));
23 changes: 15 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here

// =============> write your prediction here decimalNumber has already been declared in the function. const can not redeclare a variable with the same name in the same scope. parameter, so we cannot declare it again with const.
// This will cause a syntax error. To fix this, we should remove the const keyword when assigning the new value to decimalNumber.
// console.log(decimalNumber); only exists inside the function.
// 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 decimalNumber = 0.5;
//const percentage = `${decimalNumber * 100}%`;

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

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

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

// 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));
17 changes: 9 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

// Predict and explain first BEFORE you run any code...
// SyntaxError: Unexpected token '3'

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

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

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

// =============> write the error message here
//function square(3) {
// return num * num;
//}

// =============> write the error message here SyntaxError: Unexpected token (1:16)
// =============> explain this error message here

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log(square(3));
21 changes: 14 additions & 7 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...
// Predict and explain first...didn't declare the a and b parameters in the function. This will cause a ReferenceError. To fix this, we should declare the parameters a and b in the function definition.

// =============> write your prediction here
// =============> write your prediction here function doesn't return anything, so the result of multiplying 10 and 32 is undefined.
// To fix this, we should add a return statement to the function to return the result of multiplying a and b.

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

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

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

// =============> write your explanation here
// =============> The result of multiplying 10 and 32 is undefined'write your explanation here

// 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)}`);
20 changes: 13 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// Predict and explain first... function doesn't return anything, so the result of multiplying 10 and 32 is undefined.
// To fix this, we should add a return statement to the function to return the result of multiplying a and b.
// =============> write your prediction here '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)}`);
//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
function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
34 changes: 23 additions & 11 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
// Predict and explain first...
// Predict and explain first...const num is declared outside the function, so it is not accessible inside the function. This will cause a ReferenceError.
// To fix this, we should pass the number as a parameter to the function and use that parameter inside the function.

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here 3,3,3

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
// =============> 2,5,6
// Explain why the output is the way it is
// =============> write your explanation here
// =============> the output is the way function is called with an argument, so the function is able to access the value of the argument passed to it and return the last digit of that number.
// The function is not using the variable num declared outside the function, so it does not cause a ReferenceError.
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

const num = 103;

function getLastDigit(n) {
return n.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)}`);
7 changes: 5 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,8 @@
// 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 (weight / (height * height)).toFixed(1);
}
console.log(calculateBMI(90, 1.7));
// return the BMI of someone based off their weight and height
// return the BMI of someone based off their weight and height
5 changes: 5 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,8 @@
// 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 convertToUpperSnakeCase(inputString) {
return inputString.toUpperCase();
}

console.log(convertToUpperSnakeCase("Hello there"));
24 changes: 24 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,27 @@
// 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
);

return `£${pounds}.${pence}`;
}

console.log(toPounds("99p")); // £0.99
console.log(toPounds("5p")); // £0.05
console.log(toPounds("123p")); // £1.23
14 changes: 9 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function pad(num) {
}

function formatTimeDisplay(seconds) {
formatTimeDisplay(61);
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
Expand All @@ -21,18 +22,21 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad will be called 3 times, once for each of the hours, minutes, and seconds values that are being formatted into a string.

// 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
// =============>Pad is called for the first time with the value of 0, which is the total hours calculated from the input of 61 seconds.
// The total hours is calculated by dividing the total minutes (1) by 60, which results in 0 hours. pad(totalHours)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 0 is the value assigned to num when pad is called for the first time, and the return value of pad is "00". This is because the while loop in the pad function adds a leading zero to the string representation of num until its length is at least 2. Since num is 0, it becomes "00" after one iteration of the loop.

// 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
// =============> pad(remainingSeconds) remainingSeconds =1, so num is +1. This is because the input to formatTimeDisplay is 61 seconds, which results in 1 second remaining after calculating the total minutes and hours. The pad function is called with this value to format it as a two-digit string for display.

// 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
// =============> numString = "1" so string has only one character "01" the return value of pad when it is called for the last time in this program is "01".
// This is because the while loop in the pad function adds a leading zero to the string representation of num until its length is at least 2.
// Since num is 1, it becomes "01" after one iteration of the loop.
Loading