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
12 changes: 9 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// I predict that the error is occurring because there is a variable name conflict. The parameter 'str' is being redeclared inside the function, which is not allowed in JavaScript. This will cause a syntax error because we cannot declare a variable with the same name as a parameter within the same scope.

// call the function capitalise with a string input
// capitalise("hello");

// 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
// =============> write your new code here
// The error occurs because we are trying to declare a new variable 'str' inside the function, which is already declared as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same scope. To fix this error, we can simply remove the 'let' keyword and assign the new value to the existing parameter 'str' instead of trying to redeclare it.

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
22 changes: 18 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
// Predict and explain first...

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

// I predict that the error is occurring because there is a variable name conflict.
// The parameter 'decimalNumber' is being redeclared inside the function, which is not allowed in JavaScript.
// This will cause a syntax error because we cannot declare a variable with the same name as a parameter within the same scope.

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

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// decimalNumber is a parameter of the function convertToPercentage.
// Inside the function, we are trying to declare a new variable with the same
// name 'decimalNumber' using the 'const' keyword.
// This creates a conflict because we cannot have two variables with the same name in the same scope.
// To fix this error, we can simply remove the 'const' keyword and assign the
// new value to the existing parameter 'decimalNumber' instead of trying to redeclare it.

// 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));
20 changes: 13 additions & 7 deletions Sprint-2/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
// I predict that error will occur because the parameter 'num' is not defined in the function.

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

// =============> write the error message here
// return num * num;
// } */

// =============> write the error message here
// Uncaught SyntaxError: Unexpected number
// =============> explain this error message here
// The error occurs because we are trying to use a number '3' as a parameter in the function declaration, which is not valid syntax in JavaScript.
// Parameters should be variable names, not literal values. To fix this error, we can replace '3' with a valid variable name like 'num'.
// =============> explain this error message here
// To fix this error, we can change the parameter from '3' to a valid variable name like 'num'. This way, we can pass any number as an argument when calling the function, and it will correctly return the square of that number.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
7 changes: 6 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

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

// I predict that the error will occur because the function 'multiply' does not return any value, so when we try to use it inside the template literal, it will return 'undefined'.
// This will result in the output being "The result of multiplying 10 and 32 is undefined" instead of the expected product of 10 and 32.
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
// The error occurs because the function 'multiply' does not have a return statement, so it returns 'undefined' by default. When we try to use the result of 'multiply(10, 32)' inside the template literal, it evaluates to 'undefined', which is not the expected output. To fix this error, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}
10 changes: 9 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...
// =============> write your prediction here

// I predict that the error will occur because the function 'sum' does not return any value, so when we try to use it inside the template literal, it will return 'undefined'.
// This will result in the output being "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +10,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The error occurs because the function 'sum' has a return statement that does not return any value, which means it returns 'undefined' by default.
// When we try to use the result of 'sum(10, 32)' inside the template literal, it evaluates to 'undefined', which is not the expected output.
// To fix this error, we need to change the return statement in the 'sum' function to return the sum of 'a' and 'b'.

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b;
}
16 changes: 15 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

// I predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +15,24 @@ 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 output is "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3". This matches my prediction because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
// Explain why the output is the way it is
// =============> write your explanation here
// The output is the way it is because the function 'getLastDigit' is not using the parameter 'number' that is passed to it.
// Instead, it is using the variable 'num' which is set to 103.
// Since 'num' is not changing based on the input, the function will always return the last digit of 103, which is 3, regardless of the input passed to the function. To fix this issue, we need to change the function to use the parameter 'number' instead of the variable 'num'.
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(number) {
return number.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
// GetLastDigit seems not to work properly because the variable num is never passed into the function.
// The fix is to call getLastDigit(num) so the function uses that value.
3 changes: 2 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// 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
const bmi = weight / (height * height);
return Math.round(bmi * 10) / 10;
}
3 changes: 3 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,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(str) {
return str.toUpperCase().split(" ").join("_");
}
25 changes: 25 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,29 @@
// You will need to take this code and turn it into a reusable block of code.
// You will need to declare a function called toPounds with an appropriately named parameter.


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

// You should call this function a number of times to check it works for different inputs
console.log(toPounds("399p")); // £3.99
console.log(toPounds("45p")); // £0.45
console.log(toPounds("5p")); // £0.05
console.log(toPounds("1234p"));// £12.34
7 changes: 5 additions & 2 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ function formatTimeDisplay(seconds) {

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

// Pad will be called 3 times when formatTimeDisplay is called, once for each of the total hours, remaining minutes, and remaining seconds.
// 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

// When pad is called for the first time, the value assigned to num is 0, which is the total hours calculated from the input of 61 seconds. This is because 61 seconds is equal to 1 minute and 1 second, which means there are 0 total hours in that time duration.
// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when called for the first time is "00", because 0 padded to 2 digits 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
// When pad is called for the last time, the value assigned to num is 1, which is the remaining seconds calculated from the input of 61 seconds. This is because 61 seconds is equal to 1 minute and 1 second, so there is 1 second remaining after accounting for the full minutes.

// 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 when pad is called for the last time in this program is "01", because 1 padded to 2 digits is "01".
Loading