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
29 changes: 22 additions & 7 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// Predict and explain first...
// =============> write your prediction here
/// This code will error before it runs. 'str' has already been declared"
//
// This happens because:
// 1. 'str' is already declared as a function parameter
// 2. Inside the function, we try to declare 'str' again using 'let'

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

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// write your explanation here
//When you write: function capitalize(str)
// - 'str' is already declared as a parameter
//Then inside the function: let str = ...
// - This tries to declare 'str' again

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello")); // "Hello"


13 changes: 4 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// decimalNumber is already declared as a function parameter. and name variable the same

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

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here
console.log(convertToPercentage(0.5));

// Finally, correct the code to fix the problem
// =============> write your new code here
// creates a function named convertToPercentage
// The function returns the percentage string.SS
23 changes: 16 additions & 7 deletions Sprint-2/1-key-errors/2.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly revisit this file to make it clearer. Leave the comments on the original code.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@

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

// =============> write your prediction of the error here
// Error because the variable 'num' is not defined when we try to call the function.
// The function expects
// a parameter but we're not passing any argument when calling it.

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

// =============> write the error message here
// The function will return NaN  (Not a Number)
// this doesn't throw an error, but returns NaN  because:
// - num is undefined (no argument passed)
// - undefined * undefined = NaN 

// =============> explain this error message here
// When square() is called without an argument, the parameter 'num' has
// // the value 'undefined'. When you multiply undefined * undefined,
// // JavaScript returns NaN (Not a Number) instead of throwing an error. 

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num)
{
return num * num;
}
console.log(square(5)); // Output: 25 console.log(square(10)); 


22 changes: 21 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Predict and explain first...

// =============> write your prediction here
//The output is 320
//no define the reult of multiplying 10 and 32
// This is because the multiply function uses console.log() instead of return.
// The function will print 320 to the console, but then will show
// "undefined" because the function doesn't return a value.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -10,5 +14,21 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

//The multiply() function calculates a * b (which is 320) and prints it
// using console.log().
//console.log() doesn't return a value just output of console
// it only displays output to the console.
//
// there is no return statement, it returns 'undefined'.
// the function:
// 1. Prints "320" to the console
// 2. Returns undefined
// "The result of multiplying 10 and 32 is undefined"

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

function multiply(a, b) {
return a * b; // use return not console
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
20 changes: 20 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Predict and explain first...
// =============> write your prediction here

//The output will be:
// "The sum of 10 and 32 is not defined because the return statement without a value.
// The line "a + b;" after return will not executed
// return undefined. The code after return is unreachable.


function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +15,19 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// a + b; ← This line will not executes (unreachable code)
// So the function effectively becomes:
// function sum(a, b) {
// return undefined;
// a + b; // not execute
// }
// The return statement immediately exits the function and returns undefined.
// Any code after a return statement is "unreachable" and not execute.
// result is the sum of 10 and 32 is 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)}`);
31 changes: 31 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

// Predict the output of the following code:
// =============> Write your prediction here
//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
//
// All outputs show "3" because the function not taking the parameter
// passe value to 'num' (which is 103), so it returns the last digit of 103, which is 3.

const num = 103;

Expand All @@ -15,10 +22,34 @@ 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 getLastDigit() has TWO problems:
// The function definition doesn't have a parameter,
// it can't receive the values (42, 105, 806) added to it.
//inside the function, it uses the global constant
// 'num' (which is 103) instead of using the parameter
//getLastDigit(42) call function not taking 42
// num (103) converts to "103"
// -slice(-1) gets the last character "3"
// returns "3"
// Finally, correct the code to fix the problem
// =============> write your new code here

const num1 = 103;
function getLastDigit(number) { // Added parameter 'number'
return number.toString().slice(-1); // Use 'number' instead of 'num'
}
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 function has no parameter when call getLastDigit(42),
// the number 42 is passed but storage variable to save it.
// The function can't use it because it doesn't have a parameter to receive it.
5 changes: 4 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,7 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return Math.round((weight / (height * height)) * 10) / 10;
}

console.log(calculateBMI(70, 1.73));
16 changes: 16 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,19 @@
// 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) {
// Step 1: Replace all spaces with _
const withUnderscores = str.replace(/ /g, '_');

// Step 2: Convert to uppercase
const upperCase = withUnderscores.toUpperCase();

return upperCase;
}

// Test
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
console.log(toUpperSnakeCase("the quick brown fox")); // "THE_QUICK_BROWN_FOX"
console.log(toUpperSnakeCase("code your future")); // "CODE_YOUR_FUTURE"
29 changes: 29 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,32 @@
// 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 to convert pence string to pounds format
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 the function with inputs
console.log(toPounds("399p")); // "£3.99"
console.log(toPounds("50p")); // "£0.50"
console.log(toPounds("5p")); // "£0.05"
console.log(toPounds("1p")); // "£0.01"
console.log(toPounds("99p")); // "£0.99"
console.log(toPounds("100p")); // "£1.00"
console.log(toPounds("1250p")); // "£12.50"
console.log(toPounds("10000p")); // "£100.00"
console.log(toPounds("199p")); // "£1.99"
console.log(toPounds("2550p")); // "£25.50"
8 changes: 7 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// You will need to play computer with this example - use the Python Visual https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

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

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

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
//"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
// 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

// "01"
38 changes: 38 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't write tests for your fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,41 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Test of existing
// Test of existing function
console.log("=== TESTING BUGGY FUNCTION ===");
console.log(formatAs12HourClock("00:00"), "→ Expected: 12:00 am ❌"); // Bug: midnight
console.log(formatAs12HourClock("12:00"), "→ Expected: 12:00 pm ❌"); // Bug: noon
console.log(formatAs12HourClock("14:30"), "→ Expected: 02:30 pm ❌"); // Bug: minutes lost
console.log(formatAs12HourClock("08:15"), "→ Expected: 08:15 am ❌"); // Bug: minutes lost
console.log(formatAs12HourClock("23:59"), "→ Expected: 11:59 pm ❌"); // Bug: minutes lost


// Fixed function to get result

function formatAs12HourClockFixed(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

if (hours === 0) return `12:${minutes} am`; // Midnight
if (hours === 12) return `12:${minutes} pm`; // Noon
if (hours > 12) {
const h = (hours - 12).toString().padStart(2, "0");
return `${h}:${minutes} pm`;
}
return `${time.slice(0, 5)} am`;
}

console.log("=== Test for fixed function ===\n");

console.log(formatAs12HourClockFixed("00:00"), "→ Expected: 12:00 am");
console.log(formatAs12HourClockFixed("00:30"), "→ Expected: 12:30 am");
console.log(formatAs12HourClockFixed("01:00"), "→ Expected: 01:00 am");
console.log(formatAs12HourClockFixed("08:15"), "→ Expected: 08:15 am");
console.log(formatAs12HourClockFixed("11:59"), "→ Expected: 11:59 am");
console.log(formatAs12HourClockFixed("12:00"), "→ Expected: 12:00 pm");
console.log(formatAs12HourClockFixed("12:30"), "→ Expected: 12:30 pm");
console.log(formatAs12HourClockFixed("13:00"), "→ Expected: 01:00 pm");
console.log(formatAs12HourClockFixed("14:45"), "→ Expected: 02:45 pm");
console.log(formatAs12HourClockFixed("23:59"), "→ Expected: 11:59 pm");