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
21 changes: 18 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// I predict this code will cause a SyntaxError because of variable redeclaration.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +9,20 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// My explanation; The error occurs because:

// str is declared as a function parameter
// Inside the function, we try to declare another variable with let str = ...
// This creates a naming conflict - you can't have two variables with the same name in the same scope when one is declared with let/const




// My code:

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

// This solution uses a different variable name (result) to store the capitalized string, avoiding the naming conflict while keeping the same logic.
21 changes: 20 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...


// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict this code will cause a SyntaxError because of constant redeclaration, and even if that were fixed, it would cause a ReferenceError when trying to access decimalNumber outside the function

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

Expand All @@ -16,5 +17,23 @@ console.log(decimalNumber);

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

// My explanation; There are two main problems:
// SyntaxError: The parameter decimalNumber and the constant const decimalNumber = 0.5; have the same name in the same scope, causing a redeclaration error.
// ReferenceError: Even if we fix the first issue, console.log(decimalNumber) tries to access a variable that only exists inside the function scope, not in the global scope.



// Finally, correct the code to fix the problem
// =============> write your new code here

// My code:

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

console.log(convertToPercentage(0.5));

// I removed the redundant redeclaration of decimalNumber inside the function
// I Changed console.log(decimalNumber) to actually call the function with convertToPercentage(0.5)
17 changes: 14 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// My predict is this code will cause a SyntaxError because of an invalid function parameter.

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

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

// =============> explain this error message here
// My explanation;

// The error occurs because:
// Function parameters must be variable names (identifiers), not literal values
// 3 is a number literal, not a valid parameter name
// The function is trying to use num in the calculation, but num was never declared as a parameter

// Finally, correct the code to fix the problem

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

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

// I replaced the invalid parameter 3 with a valid parameter name num
// This allows the function to accept a number input and correctly calculate its square
29 changes: 26 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
// Predict and explain first...

// =============> write your prediction here
// I predict that the output will be:
// 320
// The result of multiplying 10 and 32 is undefined



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
// My explanation; The issue is that:

// The multiply function uses console.log(a * b) which prints 320 to the console
// But the function doesn't return any value, so it implicitly returns undefined
// When we try to interpolate ${multiply(10, 32)} in the template literal, it becomes undefined
// So we get the unexpected output showing "The result... is undefined"

// Finally, correct the code to fix the problem
// =============> write your new code here
// My new code:

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

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

// Now the output will be:
// The result of multiplying 10 and 32 is 320

// The fix:

// I Changed console.log(a * b) to return a * b so the function actually returns the result
// The template literal can now properly display the calculated value
30 changes: 27 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I predict that the output will be:
// The sum of 10 and 32 is undefined

function sum(a, b) {
return;
Expand All @@ -8,6 +9,29 @@ function sum(a, b) {

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

// =============> write your explanation here
// My explanation; The issue is with the semicolon after return:

// When JavaScript sees return; with a semicolon, it immediately exits the function
// The line a + b; is never executed because it comes after the return statement
// Since return; doesn't return any value, the function returns undefined
// Therefore, the template literal shows "The sum of 10 and 32 is undefined"
// This is an example of automatic semicolon insertion causing unexpected behavior.


// Finally, correct the code to fix the problem
// =============> write your new code here
// My new code:


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

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

// Now the output will be:
// The sum of 10 and 32 is 42

// The fix:

// I removed the semicolon after return so the expression a + b is properly returned
// The function now correctly calculates and returns the sum
44 changes: 40 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

// 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
// The last digit of 806 is 3

const num = 103;

Expand All @@ -14,11 +17,44 @@ 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

// After running the code, the actual output is:
// 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

// My explanation:
// The problem is that the getLastDigit function ignores its parameter and always uses the global constant num which is set to 103:
// The function takes a parameter but doesn't use it
// Instead, it uses the global num variable (value: 103)
// 103.toString().slice(-1) always returns "3"
// So regardless of what number we pass to the function, it always returns "3"




// Finally, correct the code to fix the problem
// =============> write your new code here
// My new code:

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


// The output will now be:
// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 6

// The fix:
// I changed the function to use its parameter (number) instead of the global (num) variable
// The function now correctly returns the last digit of the number passed to it
31 changes: 30 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,33 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
}

// Here's my implementation of the BMI calculation function, according to the steps provided and my understanding:

function calculateBMI(weight, height) {
// Calculate height squared
const heightSquared = height * height;

// Calculate BMI (weight divided by height squared)
const bmi = weight / heightSquared;

// Return BMI rounded to 1 decimal place
return bmi.toFixed(1);
}

// Test with my values: weight = 70kg, height = 1.90m
const weight = 70;
const height = 1.90;
const bmiResult = calculateBMI(weight, height);

console.log(`For weight ${weight}kg and height ${height}m, the BMI is ${bmiResult}`);

// Calculation breakdown for your values:

// Height squared: 1.90 × 1.90 = 3.61
// BMI: 70 ÷ 3.61 = 19.39...
// Rounded to 1 decimal place: 19.4
// So the function will return "19.4" for weight = 70kg and height = 1.90m.
// The toFixed(1) method ensures the result is formatted to 1 decimal place as specified in the requirements.

20 changes: 20 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,23 @@
// 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


// Here's my implementation of the function to convert a string to UPPER_SNAKE_CASE:

function toUpperSnakeCase(inputString) {
// Replace spaces with underscores and convert to uppercase
return inputString.replace(/ /g, '_').toUpperCase();
}

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


// How it works:

// inputString.replace(/ /g, '_') - replaces all spaces with underscores
// The / /g is a regular expression that matches all spaces globally
// .toUpperCase() - converts the entire string to uppercase
19 changes: 19 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,22 @@
// 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



// Here's my implementation of the toPounds function:

function toPounds(kilograms) {
const conversionRate = 2.20462;
const pounds = kilograms * conversionRate;
return pounds;
}


// The `toPounds` function works in 3 simple steps:

// Takes input - the `kilograms` parameter receives a weight in kilograms
// Converts - multiplies kilograms by 2.20462 (the conversion rate to pounds)
// Returns result - gives back the calculated weight in pounds

// Example: `toPounds(10)` calculates `10 × 2.20462 = 22.0462` and returns this value.
19 changes: 14 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@ function formatTimeDisplay(seconds) {
// Questions

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

// 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
// My answer: 0 (totalHours = 0)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// My 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
// My answer: 1 (remainingSeconds = 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
// My answer: "01" (pad adds a leading zero to make it 2 digits)


// Explanation for input 61:

// totalHours = 0
// remainingMinutes = 1
// remainingSeconds = 1
// pad is called 3 times: pad(0), pad(1), pad(1)
// Returns "00:01:01"
Loading
Loading