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
16 changes: 16 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: The code will throw a SyntaxError because the variable 'str' has already been declared as a parameter of the function.

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

// Original code with error:

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

// =============> write your explanation here
// Explanation: The error occurs because the variable 'str' is being redeclared within the function using 'let',
// which is not allowed since 'str' is already declared as a parameter of the function.
// To fix this, we can either rename the inner variable or simply assign the new value to the existing parameter without redeclaring it.

// =============> write your new code here
// Fixed code:
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("hello")); // Output: "Hello"
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// Prediction: The error will occur because the variable 'decimalNumber' is being redeclared
// inside the function using 'const', which is an illegal operation.
// Variables declared with 'const' cannot be redeclared in the same scope.

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

/*
Original code:
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +18,18 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

// =============> write your explanation here
// Explanation: The error occurs because 'decimalNumber' is declared as a parameter of the function.
// When we try to declare it again inside the function with 'const', it causes a syntax error.
// To fix this, we should either use the parameter directly or rename the inner variable.

// 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)); // Output: 50%
17 changes: 17 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// Prediction: It will throw a syntax error because 3 is not a valid parameter name.

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

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

// =============> explain this error message here
// MDN Reference: https://developer.mozilla.org/en-US/docs/Glossary/Identifier

// JavaScript does not allow numbers to be used as variable names or parameter names.
// Function parameters must follow the rules for valid identifiers.
// According to MDN, an identifier may not start with a digit and must follow JavaScript's naming rules.

// Finally, correct the code to fix the problem

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

console.log(square(3)); // 9

// Test cases:

// console.log(square(10)); // 100
// console.log(square(5)); // 25
21 changes: 21 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
// Predict and explain first...

// =============> write your prediction here
// Prediction: The function will not multiply the numbers.
// Instead, it will print the sum of a and b (42) and the final console.log will ouput:
// The result of multiplying 10 and 32 is undefined

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

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

// =============> Error message
// 320
// The result of multiplying 10 and 32 is undefined

// =============> write your explanation here
// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

// The function multiply() uses console.log() to print the result but it does not return anything.
// In JavaScript, if a function does not explicitly return a value, it returns undefined.
// When we try to use multiplay(10, 32) inside a console.log, it returns undefined.
// To fix this, we must use the return statement so the function gives back a value that can be used.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}

// The result of multiplying 10 and 32 is 320
console.log(`The result of multiplying 10 and 32 is ` + multiply(10, 32));
19 changes: 19 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: The code should output - "The sum of 10 and 32 is 42"
// But the code will return undefined instead of 42.

/* Original code
function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
*/

// =============> Error message
// The sum of 10 and 32 is undefined

// =============> write your explanation here
// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

// The function uses `return;` without an expression.
// According to MDN, if a return statement has no value, the function returns undefined.
// So when `sum(10, 32)` is called, it returns undefined.
// To fix this, the return statement must include the expression `a + b`.

// 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)}`);
24 changes: 24 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// Predict the output of the following code:
// =============> Write your prediction here
// Prediction: The function will always return "3" because it uses the hardcoded value 103.

/* Original code:
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +14,35 @@ function getLastDigit() {
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
// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#function_parameters

// The function does not use the input number passed in.
// It always returns the last digit of the variable `num`, which is 103.
// To fix this, the function should accept a parameter and use that instead of the hardcoded value.

// Finally, correct the code to fix the problem
// =============> write your new code here
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)}`);

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

// The original function was not working as it wasn't taking any parameters and always used the variable `num = 103`.
// So no matter what number you passed in, the function would ignore it and always returned the hardcoded value, "103"
// instead, which was "3", in this case.
6 changes: 5 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1); // toFixed(1) rounds to 1 decimal place.
}

// Test case
console.log(calculateBMI(70, 1.73)) // Output: 23.4
19 changes: 19 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,22 @@
// 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

// MDN References I used:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim

function toUpperSnakeCase(input) {
return input
// Removes extra spaces
.trim()
// Converts everything to caps
.toUpperCase()
// Replaces all spaces with underscores
// g means global, so it applies to the whole string
// / / is a regular expression matching a space
.replace(/ /g, "_");
}

console.log(toUpperSnakeCase("hello there")); // HELLO_THERE
23 changes: 23 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,26 @@
// 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

// Converts a string representing pence (e.g. "399")
// into pounds.pence format (e.g. "3.99")
// MDN References used:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

// Converts a pence string (e.g. "399")
function toPounds(penceString) {
// Ensure the string is atleast 3 characters long by padding with leading zeros if needed
// e.g. "5" → "005", "99" → "099", "399" stays "399"
const padded = penceString.padStart(3, "0");
// Extract everything except the last two digits → this becomes the pounds
// e.g. "399" → "3"
const pounds = padded.substring(0, padded.length - 2);
// Extract the last two digits → this becomes the pence
// e.g. "399" → "99"
const pence = padded.substring(padded.length - 2);
// Combines pounds and pence into the final money format
return `${pounds}.${pence}`;
}

console.log(toPounds("399")); // 3.99
25 changes: 25 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,42 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// pad(totalHours)
// pad(remainingMinutes)
// pad(remainingSeconds)
// Answer: 3

// 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
// formatTimeDisplay:
// seconds = 61
// remainingSeconds = 61 % 60 = 1
// totalMinutes = (61 - 1) / 60 = 1
// remainingMinutes = 1 % 60 = 1
// totalHours = (1 - 1) / 60 = 0
// Answer: pad(num) = pad(0) = 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// First call: pad(totalHours)
// totalHours = 0
// pad(0) → "00"
// Answer: "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
// Last call: pad(remainingSeconds)
// remainingSeconds = 1
// pad(1) → num = 1
// Answer: num = 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
// Last call: pad(remainingSeconds)
// remainingSeconds = 1
// toString() → "1"
// "1".padStart(2, "0") → "01"
// pad(1) → "01"
// Answer = "01"