Skip to content
Closed
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
22 changes: 16 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: SyntaxError because `str` is re-declared with `let` inside the function
// where it already exists as a parameter. You cannot use `let` to re-declare a variable
// that already exists in the same scope.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

// Original broken code:
// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

// Explanation: The parameter `str` already exists in the function scope.
// Using `let str` tries to re-declare it, which is a SyntaxError.
// Instead of reassigning `str` (which represents the original input),
// we use a new const `capitalisedStr` to make it clear this is a different value.

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

// =============> write your explanation here
// =============> write your new code here
26 changes: 17 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Prediction: Two errors — (1) re-declaring decimalNumber with const inside the function
// when it is already a parameter causes a SyntaxError.
// (2) console.log(decimalNumber) outside the function will throw ReferenceError because
// decimalNumber is not defined in the outer scope.

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

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

// Explanation: The parameter decimalNumber already exists in the function scope;
// const re-declaration is illegal. Also, decimalNumber is not accessible outside the function.
// Fix: remove the const re-declaration inside the function, and pass a value when calling it.

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

return percentage;
}

console.log(decimalNumber);

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

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(0.5));
25 changes: 12 additions & 13 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

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

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

// =============> write the error message here
// Prediction: SyntaxError — a function parameter must be a valid identifier, not a number literal.

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

// Finally, correct the code to fix the problem

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

// Error message: SyntaxError: Unexpected number
// Explanation: `3` is a number literal and cannot be used as a parameter name.
// Parameters must be valid identifiers (variable names).
// Fix: use a named parameter like `num`.

function square(num) {
return num * num;
}
19 changes: 12 additions & 7 deletions 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...
// Prediction: The template literal will print "undefined" for the function result because
// multiply uses console.log internally but does not return a value (returns undefined).

// =============> write your prediction here
// Original broken code:
// function multiply(a, b) {
// console.log(a * b);
// }
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// Explanation: Functions without a return statement return undefined.
// The outer console.log then interpolates undefined into the string.
// Fix: replace console.log inside multiply with a return statement.

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

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

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

// Finally, correct the code to fix the problem
// =============> write your new code here
21 changes: 14 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: The sum will print "undefined" because return; exits the function
// immediately without a value, so a + b is never evaluated.

// Original broken code:
// function sum(a, b) {
// return;
// a + b;
// }
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// Explanation: return; with no expression returns undefined.
// The a + b on the next line is unreachable dead code and is never executed.
// Fix: put a + b on the same line as return.

function sum(a, b) {
return;
a + b;
return a + b;
}

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
31 changes: 18 additions & 13 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// Prediction: All three logs will print "3" (the last digit of the module-level const num = 103)
// because getLastDigit ignores its parameter and always uses the outer num.
// Expected output:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

const num = 103;
// Original broken code:
// const num = 103;
// function getLastDigit() {
// return num.toString().slice(-1);
// }

function getLastDigit() {
// Actual output matched prediction — all printed "3".

// Explanation: The function has no parameter, so any argument passed in is discarded.
// It always reads the outer `num` variable which is 103.
// Fix: add a parameter to getLastDigit and use it instead of the outer variable.

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

// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// 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
2 changes: 1 addition & 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,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));
}
20 changes: 5 additions & 15 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// A set of words can be grouped together in different cases.
function toUpperSnakeCase(str) {
return str.replaceAll(" ", "_").toUpperCase();
}

// For example, "hello there" in snake case would be written "hello_there"
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.

// Implement a function that:

// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

// 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
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
16 changes: 11 additions & 5 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// In Sprint-1, there is a program written in interpret/to-pounds.js
function toPounds(penceString) {
const withoutP = penceString.substring(0, penceString.length - 1);
const padded = withoutP.padStart(3, "0");
const pounds = padded.substring(0, padded.length - 2);
const pence = padded.substring(padded.length - 2).padEnd(2, "0");
return `£${pounds}.${pence}`;
}

// 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.

// 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("9p")); // £0.09
console.log(toPounds("50p")); // £0.50
console.log(toPounds("1000p")); // £10.00
13 changes: 7 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad is called 3 times per call to formatTimeDisplay (once for totalHours, once for remainingMinutes, once for remainingSeconds).

// Call formatTimeDisplay with an input of 61, now answer the following:
// (remainingSeconds = 1, totalMinutes = 1, remainingMinutes = 1, totalHours = 0)

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> num = 0 (totalHours), because totalHours is the first argument in the template literal.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// c) What is the return value of pad when it is called for the first time?
// =============> "00" — "0".length < 2, so "0" is prepended, giving "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
// =============> num = 1 (remainingSeconds). It is the last argument passed in the template literal, so pad is called with it last.

// 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" — "1".length < 2, so "0" is prepended, giving "01".
Loading