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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
*.swp
**/.DS_Store
28 changes: 28 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
// Predict and explain first...
// =============> write your prediction here

/** Prediction:
* The code will throw an error because the parameter str and the variable str inside the function are both declared 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 function:
*
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
*/

// =============> write your explanation here
/** Explanation:
* The error occurs because:
*
* The function parameter str is already declared in the function scope
* Inside the function, we're trying to redeclare str using let which creates a new variable in the same scope
* JavaScript doesn't allow redeclaring a variable with let in the same scope
* The error message would be something like:
* SyntaxError: Identifier 'str' has already been declared
*/

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

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

// Test the function
console.log(capitalise("hello")); // "Hello"
console.log(capitalise("world")); // "World"

41 changes: 34 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
// Predict and explain first...

// Why will an error occur when this program runs?

// =============> write your prediction here
/**
* Prediction:
* An error will occur because the function parameter decimalNumber is being redeclared inside the function body using const. This creates a naming conflict - you can't have a parameter and a local variable with the same name.
* Additionally, the console.log(decimalNumber) at the end will cause a ReferenceError because decimalNumber is not defined in the global scope, it's only defined within the function's scope.
*/

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

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

// =============> write your explanation here
/**
* Explanation
* The code has two main problems:
* Redeclaration error: The function parameter decimalNumber is being redeclared with const decimalNumber = 0.5 inside the function. In JavaScript, it not possible to have a variable with the same name as a parameter in the same scope.
* Scope error: The console.log(decimalNumber) at the end is trying to access a variable that only exists inside the function's scope. Variables declared inside functions are not accessible from the outside.
* Logic error: Even if the scope issues were fixed, the function always returns "50%" regardless of the input because it overwrites the parameter with 0.5.
*/

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

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
// Example usage:
const decimalNumber = 0.5;
console.log(convertToPercentage(decimalNumber)); // Output: "50%"
console.log(convertToPercentage(0.80)); // Output: "80%"

// Finally, correct the code to fix the problem
// =============> write your new code here
33 changes: 27 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@

// 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;
}
/**
* Prediction:
* The function parameter is incorrectly defined as a numeric literal 3 instead of a parameter name. This will cause a syntax error because JavaScript function parameters must be valid identifiers (variable names), not numbers.
*/

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


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

// =============> explain this error message here
/** Explanation:
* In JavaScript, when defining a function, the parameters must be valid variable names (like num, x, value, etc.). Using a literal number like 3 as a parameter is invalid syntax because:
* Parameters act as placeholders for values that will be passed when the function is called
* Numbers can not be used as variable names in JavaScript
* The parser expects a valid identifier in the parameter declaration, not a numeric literal
*/

// Finally, correct the code to fix the problem

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

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

console.log(square(3));

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

// =============> write your prediction here
/**
* It display "320" and "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

/** Explanation:
*
* The following function takes two arguments a and b as input and display the multiplication result.
* But instead of using the function "console.log" it shoudl use the function "return (a * b)".
* That's the reason it displays "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)}`);

16 changes: 15 additions & 1 deletion 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
/**
* It returns an "undefined"
*/

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

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

// =============> write your explanation here
/** Explanation:
* The arguments "a + b" should be on the same line as the return so the sum returns that value instead of "undefined"
*/

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

62 changes: 55 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,70 @@
// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
/**
* The last digit of 42 input is '3'
* The last digit of 105 input is '3'
* The last digit of 806 input is '3'
*/

function getLastDigit() {
return num.toString().slice(-1);
}
/**
* Original function:
*
* const num = 103;
*
* 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 input is '3'
* The last digit of 105 input is '3'
* The last digit of 806 input is '3'
*/

// Explain why the output is the way it is
// =============> write your explanation here

/**
* Explanation:
*
* The function getLastDigit() is not working properly because:
* It's ignoring the parameter: The function is defined to take a parameter, but when calling the function it's using the global variable num (which is set to 103) instead of the parameter passed to it.
* There is no parameters in function definition.
* The function is defined as function getLastDigit() without any parameters, so when we call getLastDigit(42), the 42 is ignored.
* Fixed value: The function always returns the last digit of 103 (which is "3"), regardless of what number is passed to it.
* That's why all three console logs show "3" - they're all getting the last digit of 103, not the numbers 42, 105, and 806 passed.
*/

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

const num = 103;

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

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

/**
* The key fix was adding a parameter number to the function definition and using that parameter inside the function instead of the global num variable. Now each call to the function getLastDigit() works with the specific number passed to it.
*/

20 changes: 18 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,22 @@
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

/**
* Original function:
* 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
}
// Calculate BMI: weight divided by height squared
const bmi = weight / (height * height);

// Return the result rounded to 1 decimal place using the built-in Math library
return Math.round(bmi * 10) / 10;
}

// Test the function
console.log(calculateBMI(70, 1.73)); // Should output: 23.4
console.log(calculateBMI(80, 1.80)); // Test with another example and returns 24.7

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

function convertToUpperSnakeCase(inputString) {
// Split the string into words using spaces as separators
const words = inputString.split(' ');

// Convert each word to uppercase using map() function
const upperWords = words.map(word => word.toUpperCase());

// Join the uppercase words with underscores
const upperSnakeString = upperWords.join('_');

return upperSnakeString;
}

// Test the function
console.log(convertToUpperSnakeCase("hello there")); // HELLO_THERE
console.log(convertToUpperSnakeCase("lord of the rings")); // LORD_OF_THE_RINGS
console.log(convertToUpperSnakeCase("UPPER SNAKE CASE")); // UPPER_SNAKE_CASE

38 changes: 38 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,41 @@
// 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


// REUSABLE FUNCTION SOLUTION:

function toPounds(penceString) {
// Remove the trailing 'p' character
const penceStringWithoutTrailingPound = penceString.substring(
0,
penceString.length - 1
);

// Pad with leading zeros to ensure at least 3 characters
const paddedPenceNumberString = penceStringWithoutTrailingPound.padStart(3, "0");

// Extract pounds (all except last 2 digits)
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// Extract pence (last 2 digits) and ensure it's 2 digits
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// Return the formatted string
return `£${pounds}.${pence}`;
}

// TEST THE FUNCTION:

console.log(toPounds("399p")); // £3.99
console.log(toPounds("99p")); // £0.99
console.log(toPounds("5p")); // £0.05
console.log(toPounds("1000p")); // £10.00
console.log(toPounds("12345p")); // £123.45
console.log(toPounds("0p")); // £0.00

Loading