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
13 changes: 9 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
// 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
// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}

console.log(capitalise("hello"));
18 changes: 12 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@

// 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: 13 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// 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: Unexpected number

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
// The function parameter is written as 3, which is a number literal.
// Function parameters must be variable names (identifiers).
// JavaScript does not allow numbers as parameter names,
// so it throws a SyntaxError before running the program.

// Finally, correct the code to fix the problem

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

console.log(square(3));

101 changes: 101 additions & 0 deletions Sprint-2/1-key-errors/answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Sprint 2 - 1 Key Errors (Answers)

## 0.js
- Predicted error:
- Actual error:
- Why it happens:
- Minimal fix:
- Docs:

## 1.js
- Predicted error:
- Actual error:
- Why it happens:
- Minimal fix:
- Docs:

## 2.js
- Predicted error:
- Actual error:
- Why it happens:
- Minimal fix:



## 0.js

- Predicted error: SyntaxError (variable redeclaration)

- Actual error: SyntaxError: Identifier 'str' has already been declared

- Why it happens:
The function parameter `str` is already declared. Inside the function, `let str = ...` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration of variables using `let`.

- Minimal fix:
Remove the `let` and return the new string directly:

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

- Docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let



## 1.js
- Predicted error: SyntaxError (variable redeclaration)

- Actual error: SyntaxError: Identifier 'decimalNumber' has already been declared

- Why it happens:
The function parameter `decimalNumber` is already declared. Inside the function, `const decimalNumber = 0.5;` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration using `const`.

There is also a second issue:
`console.log(decimalNumber);` is outside the function, and `decimalNumber` is not defined in the global scope. This would cause a ReferenceError.

- Concepts tested:
Variable scope, function parameters, redeclaration rules, global vs local variables.

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

console.log(convertToPercentage(0.5));




## 2.js

- Predicted error: SyntaxError (invalid function parameter)

- Actual error: SyntaxError: Unexpected number

- Why it happens:
The function is declared as `function square(3)`.
Function parameters must be variable names (identifiers), not literal values.
The number `3` is not a valid parameter name, so JavaScript throws a syntax error.

- Concept tested:
Function declaration syntax and valid parameter identifiers.

- Minimal fix:

```js
function square(number) {
return number * number;
}

console.log(square(3));









23 changes: 19 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// Predict and explain first...

// =============> write your prediction here
// The function will print 320 from inside multiply,
// but the final console.log will show "undefined"
// because multiply does not return a value.

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

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

// =============> write your explanation here
// The function multiply only logs the result using console.log,
// but it does not return the value.
// Since it does not return anything, JavaScript returns undefined by default.
// Therefore, the template string inserts 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)}`);


24 changes: 19 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// The function will return undefined because the return statement
// is followed by a line break. JavaScript inserts a semicolon automatically,
// so the function exits before a + b is executed.

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
// The return statement is on its own line.
// JavaScript automatically inserts a semicolon after return.
// This means the function returns undefined immediately,
// and the expression 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)}`);
33 changes: 24 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The function will always return 3 because it uses the global variable num (103).
// So all outputs will show 3 regardless of the input.

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
// 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 does not take a parameter, so it ignores the values passed in.
// It always uses the global variable num (which is 103).
// Therefore, it always returns the last digit of 103, which is 3.

// 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
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)}`);
18 changes: 16 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
// Then when we call this function with the weight and height
// 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
// }


function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

// Test cases
console.log(calculateBMI(70, 1.73)); // Output: 23.4
console.log(calculateBMI(80, 1.8)); // Output: 24.7




10 changes: 10 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,13 @@
// 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();
}

// Test cases
console.log(toUpperSnakeCase("hello there")); // HELLO_THERE
console.log(toUpperSnakeCase("lord of the rings")); // LORD_OF_THE_RINGS

28 changes: 28 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,31 @@
// 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}`;
}

// Test cases
console.log(toPounds("399p")); // £3.99
console.log(toPounds("5p")); // £0.05
console.log(toPounds("1200p")); // £12.00

Loading