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
1 change: 1 addition & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// updates the value of the variable count by adding 1
14 changes: 7 additions & 7 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Predict and explain first...
// =============> write your prediction here
// Missing backticks for template string template strings need to use backticks `, but here ${} is used without them

// call the function capitalise with a string input
// call the function capitalise with a string input (error)
// interpret the error message and figure out why an error is occurring
// ${} must be inside backticks and str is being redeclared (it is already the function parameter)

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

// =============> write your explanation here
// =============> write your new code here
// =============> This function takes a string and returns a new string with the first letter in capital
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
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
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"
6 changes: 6 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ function formatTimeDisplay(seconds) {
// 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"
24 changes: 24 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Test of existing fuction
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

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