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
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// SyntaxError: Cannot redeclare parameter 'str' inside function body

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

// =============> write your explanation here
// SyntaxError: Identifier 'str' has already been declared - parameter cannot be redeclared with let/const/var
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter

// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
21 changes: 13 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// SyntaxError: Cannot redeclare parameter 'decimalNumber' inside function body

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

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

return percentage;
}

console.log(decimalNumber);
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;
// return percentage;
// }
// console.log(decimalNumber);

// =============> write your explanation here
// SyntaxError: Identifier 'decimalNumber' has already been declared - parameter cannot be redeclared
// Also, console.log(decimalNumber) would cause ReferenceError - variable only exists inside function scope
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}
17 changes: 10 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@

// 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
// SyntaxError: Function parameters must be valid identifiers, not number literals

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

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

// =============> explain this error message here
// Parameters must be valid identifiers (start with letter/underscore/$), not numbers
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_token

// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// Output: "320" then "The result of multiplying 10 and 32 is undefined"
// Function logs but doesn't return, so template literal receives undefined

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

// =============> write your explanation here
// Bug: console.log() prints but doesn't return a value - functions without return give undefined
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}
8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// Output: "The sum of 10 and 32 is undefined"
// Return statement has no value, so returns undefined. Expression "a + b" is unreachable code

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

// =============> write your explanation here
// Bug: return; exits immediately without a value (returns undefined). Code after return never executes
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

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

// Predict the output of the following code:
// =============> Write your prediction here
// All outputs will be "3" because function has no parameter and always uses global num (103)

const num = 103;

Expand All @@ -15,10 +16,20 @@ 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
// Bug: Function has no parameter, so arguments (42, 105, 806) are ignored. Always uses global num (103)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(number) {
return number.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
4 changes: 2 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,5 @@
// 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 Number((weight / (height * height)).toFixed(1));
}
4 changes: 4 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,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.replaceAll(" ", "_").toUpperCase();
}
16 changes: 16 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,19 @@
// 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)
.padEnd(2, "0");
return `£${pounds}.${pence}`;
}
5 changes: 5 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times: pad(totalHours), pad(remainingMinutes), pad(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
// 0 (totalHours for 61 seconds = 0 hours)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// "00" (0.toString().padStart(2, "0"))

// 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 (remainingSeconds for 61 seconds: 61 % 60 = 1)

// 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
// "01" (1.toString().padStart(2, "0"))
Loading