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
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

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

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

*/
// =============> write your explanation here
/*Undeclared variables are automatically declared when first used. As a parameter in the 'capitalise' function,
'str' is already declared. trying to declare it again with 'let' will cause a syntax error.
*/
// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello world")); // This line is inserted only to test the code. It returns "Hello world"
18 changes: 15 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// =============> write your prediction here

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

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

console.log(decimalNumber);

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

/*
1. Undeclared variables are automatically declared when first used. As a parameter in the 'convertToPercentage' function,
'decimalNumber' is (automatically) already declared. trying to declare it again with 'const' causes a syntax error.
2. Also, the 'console.log(decimalNumber);' line is outside the function, so 'decimalNumber' is not defined in that scope.
3 decimalNumber = 0.5; overwrites the argument supplied, and so to make the function work as intended, this line should be removed, so the function does not ignore its input argument.
*/
// 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.25)); // This line is inserted only to test the code. It returns "25%"
18 changes: 12 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

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

*/
// =============> write the error message here

// SyntaxError: Unexpected number
// =============> explain this error message here
/*
1-The parameter name '3' is not a valid identifier. Function parameters names must (be valid variable names & not literal values), i.e. start with a letter, underscore (_), or dollar sign ($), and cannot be a number.
2-Also, the function uses 'num' which is not defined anywhere. It should use the parameter name instead.
3-Finally, the function is not called anywhere, so it won't produce any output.
*/

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3)); // This line is inserted only to test the function. It returns 9
21 changes: 17 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// Predict and explain first...

/*
The function 'multiply' does not return any value, it only logs the product of 'a' and 'b' to the console, within itself.
Therefore, when we try to use its result in a template literal, it will be 'undefined'.
In the below, i expect the TWO outputs to be, 320 and The result of multiplying 10 and 32 is undefined.
*/
// =============> write your prediction here

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

/*
1. I will change the function to return the product of 'a' and 'b' instead of logging it.
This way, when we call 'multiply(10, 32)', it will return 320,
which can then be used in the template literal.
*/
// 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)}`);
20 changes: 18 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here

// =============> write your prediction here
/*
The function 'sum' does not return the sum of 'a' and 'b' because there is a semicolon immediately after the 'return' statement.
This causes the function to return 'undefined' instead of the intended sum.
*/
/*
function sum(a, b) {
return;
a + b;
}

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

*/
// =============> write your explanation here
/*
1. In JavaScript, when a 'return' statement is followed by a newline, it is treated as if there is a semicolon immediately after 'return'.
2. This means that the function exits at the 'return' statement and does not execute the next line, which contains the actual sum operation.
3. As a result, the function returns 'undefined' instead of the sum of 'a' and 'b'.
4. To fix this, we need to place the expression to be returned on the same line as the 'return' statement.
*/
// 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)}`);
34 changes: 32 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// Predict and explain first...
/*
The function 'getLastDigit' is not working properly because it does not take any parameters, yet it is being called with arguments (42, 105, 806).
As a result, it always returns the last digit of the constant 'num', which is 103, instead of the last digit of the numbers passed to it.
*/
// =============> 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
*/
// Predict the output of the following code:
// =============> Write your prediction here

/*
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +23,32 @@ 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 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
/* 1. The function 'getLastDigit' does not accept any parameters, so it does not use the values (42, 105, 806) passed to it when called.
2. Instead, it always operates on the constant 'num', which is set to 103.
3. The function converts 'num' to a string and retrieves the last character using 'slice(-1)', which is '3'.
4. Therefore, regardless of the input values, the function always returns '3', leading to the same output for all calls.
*/
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

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

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)}`);
9 changes: 7 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// return the BMI of someone based off their weight and height
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
}
console.log(calculateBMI(70, 1.73)); // This line is inserted only to test the function. It returns 23.4
console.log(calculateBMI(80, 1.8)); // This line is inserted only to test the function. It returns 24.7
console.log(calculateBMI(90, 1.75)); // This line is inserted only to test the function. It returns 29.4
6 changes: 6 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,9 @@
// 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) {
// return the string in UPPER_SNAKE_CASE
return str.toUpperCase().replace(/ /g, "_");
}
console.log(toUpperSnakeCase("hello there")); // This line is inserted only to test the function. It returns "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // This line is inserted only to test the function. It returns "LORD_OF_THE_RINGS"
25 changes: 25 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,28 @@
// 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 toPounds(penceString) {
// Convert a string representing an amount in pence (e.g., "123p") to pounds and pence format (e.g., "£1.23")

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}`;
}
console.log(toPounds("5555p")); // This line is inserted only to test the function. It returns "£55.55"
console.log(toPounds("399p")); // This line is inserted only to test the function. It returns "£0.05"
console.log(toPounds("50p")); // This line is inserted only to test the function. It returns "£0.50"
console.log(toPounds("500p")); // This line is inserted only to test the function. It returns "£5.00"
19 changes: 15 additions & 4 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(61)); // This line is inserted only to test the function. It returns "00:01:01"

// Here is a function that takes a number of seconds and returns a string formatted as "HH:MM:SS"
// You will need to play computer with this example - use the Python Visualiser 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

/*
The value assigned to num when pad is called for the last time is 1.
The last call to pad in the template literal is pad(remainingSeconds). Since remainingSeconds = 61 % 60 = 1, the value passed to num is 1. This will return "01" after padding, resulting in the final output "00:01:01"
*/
// 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
/*
The return value of the last call to pad is "01".
Explanation: When pad is called with remainingSeconds (which equals 1), the parameter num receives the value 1. The function then converts it to a string "1",
pads it to 2 characters with leading zeros to get "01", and returns this string. This returned value "01"
is then inserted into the template literal as the last part of the time display, producing the final output "00:01:01".
*/
46 changes: 46 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,49 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.log(formatAs12HourClock("12:00")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("12:01")); // This line is inserted only to test the function. It returns "12:00 pm"
console.log(formatAs12HourClock("13:13")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("00:01")); // This line is inserted only to test the function. It returns "12:00 pm"
console.log(formatAs12HourClock("01:01")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("11:59")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("24:00")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("14:14")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("20:20")); // This line is inserted only to test the function. It returns "12:00 am"
console.log(formatAs12HourClock("00:00")); // This line is inserted only to test the function. It returns "12:00 am"

/*
Test Groups Created:
1. Midnight (00:xx) - Edge case for start of day
2. Morning hours (01-11) - Standard AM times
3. Noon (12:xx) - Edge case for 12 PM
4. Afternoon/Evening (13-23) - Standard PM times requiring conversion
5. Minutes preservation - Ensures minutes aren't lost

Bugs Found:
1. Midnight bug: "00:00" returns "00:00 am" instead of "12:00 am"
2. Noon bug: "12:00" returns "12:00 am" instead of "12:00 pm"
3. Minutes lost: All times lose their minutes and show :00 (e.g., "13:13" becomes "1:00 pm" instead of "1:13 pm")
4. Leading zeros: Morning times keep the input format including leading zeros
*/

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

if (hours === 0) {
// Midnight: 00:xx becomes 12:xx am
return `12:${minutes} am`;
} else if (hours < 12) {
// Morning: 1-11 stays same with am
return `${hours}:${minutes} am`;
} else if (hours === 12) {
// Noon: 12:xx stays 12:xx pm
return `12:${minutes} pm`;
} else {
// Afternoon/Evening: 13-23 subtract 12 and add pm
return `${hours - 12}:${minutes} pm`;
}
}
*/
Loading