Skip to content

West Midlands | ITP-May - 25| Jonathan Boahene | Sprint-2 | Module-Structuring-and-Testing-Data #664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
19 changes: 14 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here => This code will throw a syntax error
// because the parameter str is redeclared as a let variable inside the function, which is not allowed.

// 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 => The error occurs because we cannot declare
// a new variable with the same name as a function parameter (str) using let inside the function.
// This causes a "Identifier 'str' has already been declared" error according to mdn.

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("bethan"));
25 changes: 19 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// An error will occur because 'decimalNumber' is redeclared inside the function using 'const',
// which is not allowed. Also, 'decimalNumber' is not defined in the global scope,
// so console.log(decimalNumber) will throw a ReferenceError.

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

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

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here
// The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function
// using 'const'. Also, 'decimalNumber' is not defined outside the function, so
// console.log(decimalNumber) will throw a ReferenceError.

// 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.9)); // Output: 90%
15 changes: 10 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

// 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
// The code will throw a syntax error because you cannot use a number as a function parameter.

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

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

// =============> explain this error message here
// Function parameters must be variable names, not values. Using a number as a parameter is invalid syntax in JavaScript -mdn

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log(square(7));
21 changes: 15 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Predict and explain first...

// =============> write your prediction here
// function multiply(a, b) {
// console.log(a * b);
// }

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

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// The output will be:
// 320
// The result of multiplying 10 and 32 is undefined
// This is because multiply logs the result but does not return it, so the template literal gets undefined.

// =============> write your explanation here
// The function multiply only prints the result to the console and does not return anything.
// When we use multiply(10, 32) inside the template literal, it evaluates to undefined because the function has no return value.

// 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: 14 additions & 6 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// function sum(a, b) {
// return;
// a + b;
// }

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// The output will be: The sum of 10 and 32 is undefined
// This is because the function returns immediately with no value, so a + b is never executed.

// =============> write your explanation here
// The return statement ends the function before a + b is calculated, so the function returns 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: 22 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@

// 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
// Because getLastDigit always uses the global variable num (103) and ignores the argument.

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// 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
// // Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// The function getLastDigit does not accept any parameters and always uses the global variable num (103).
// So, regardless of the argument passed, it always returns the last digit of 103, which is "3".

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

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)}`);
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
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
}
// Calculate BMI: weight divided by height squared
const bmi = weight / Math.pow(height, 2);
// Return BMI rounded to 1 decimal place
return +bmi.toFixed(1);
}

console.log(calculateBMI(78, 1.72));
8 changes: 8 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,11 @@
// 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) {
// Replace spaces with underscores and convert to uppercase
return str.replace(/ /g, "_").toUpperCase();
}

console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
18 changes: 18 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,21 @@
// 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) {
// Remove trailing 'p' and pad with zeros to ensure at least 3 digits
const pence = penceString
.substring(0, penceString.length - 1)
.padStart(3, "0");
// Get pounds and pence parts
const pounds = pence.substring(0, pence.length - 2);
const pencePart = pence.substring(pence.length - 2).padEnd(2, "0");
// Return formatted string
return `£${pounds}.${pencePart}`;
}

console.log(toPounds("399p")); // £3.99
console.log(toPounds("9p")); // £0.09
console.log(toPounds("99p")); // £0.99
console.log(toPounds("100p")); // £1.00
console.log(toPounds("1234p")); // £12.34
5 changes: 5 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// pad will be called 3 times (once for hours, once for minutes, once for seconds).

// 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
// The first call is pad(totalHours). For input 61, totalHours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// pad(0) returns "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 last call is pad(remainingSeconds). For input 61, remainingSeconds is 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
// pad(1) returns "01".
69 changes: 58 additions & 11 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,71 @@
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.

// function formatAs12HourClock(time) {
// const hours = Number(time.slice(0, 2));
// if (hours > 12) {
// return `${hours - 12}:00 pm`;
// }
// return `${time} am`;
// }

// const currentOutput = formatAs12HourClock("08:00");
// const targetOutput = "08:00 am";
// console.assert(
// currentOutput === targetOutput,
// `current output: ${currentOutput}, target output: ${targetOutput}`
// );

// const currentOutput2 = formatAs12HourClock("23:00");
// const targetOutput2 = "11:00 pm";
// console.assert(
// currentOutput2 === targetOutput2,
// `current output: ${currentOutput2}, target output: ${targetOutput2}`
// );

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

if (hours === 0) {
return `12:${minutes} am`;
}
if (hours === 12) {
return `12:${minutes} pm`;
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${pad(hours - 12)}:${minutes} pm`;
}
return `${time} am`;
return `${pad(hours)}:${minutes} am`;
}

function pad(num) {
return num.toString().padStart(2, "0");
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
// Test cases for different groups of input data and edge cases
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
formatAs12HourClock("00:00") === "12:00 am",
"Test midnight failed"
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(formatAs12HourClock("01:00") === "01:00 am", "Test 1am failed");
console.assert(formatAs12HourClock("08:00") === "08:00 am", "Test 8am failed");
console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Test noon failed");
console.assert(formatAs12HourClock("13:00") === "01:00 pm", "Test 1pm failed");
console.assert(
formatAs12HourClock("11:59") === "11:59 am",
"Test 11:59am failed"
);
console.assert(
formatAs12HourClock("12:59") === "12:59 pm",
"Test 12:59pm failed"
);
console.assert(
formatAs12HourClock("15:30") === "03:30 pm",
"Test 3:30pm failed"
);
console.assert(formatAs12HourClock("23:00") === "11:00 pm", "Test 11pm failed");
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
formatAs12HourClock("23:59") === "11:59 pm",
"Test 11:59pm failed"
);
15 changes: 15 additions & 0 deletions prep/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// function decimalTopercent(num){
// let percent = num * 100;
// return percent
// }
// console.log(decimalTopercent(0.5) + "%");

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

const result = convertToPercentage(0.5);
const result1 = convertToPercentage(0.231);
console.log(result);
console.log(result1);