Skip to content

West Midland | ITP-May-2025 | Saleh Yousef | Module-Structuring-and-Testing-Data | sprint 2 #589

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 8 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
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 7cc7f5
13 changes: 11 additions & 2 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 it will captilase the first and second letter of the string because the is calling for 0 , 1 slice.

// 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)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("summer"));
// why will an error occur when this program runs?
// because the variable str is being declared twice, once as a parameter and then again inside the function.
// =============> write your explanation here
// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("summer"));
15 changes: 11 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here - it won't work because the variable decimalNumber is being declared twice, once as a parameter and then again inside the function.


// 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);
console.log(convertToPercentage(0.8));

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

// previously the code was not working because the variable decimalNumber was being declared twice, once as a parameter and then again inside the function. This caused a conflict and resulted in an error.
// 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.8));
10 changes: 6 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here

// We will get in error beacuse teh variable num is not defined.
function square(3) {
return num * num;
}

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

// =============> explain this error message here
// =============> explain this error message here - the number should not be there, it should be a variable name.

// Finally, correct the code to fix the problem

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

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

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

// =============> write your prediction here
// =============> write your prediction here - the multiply will not return anything because the function does not have a return statement.

function multiply(a, b) {
console.log(a * b);
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
Expand All @@ -12,3 +12,8 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// 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)}`);
9 changes: 7 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here - the function will not work because we don't have a return statement in the function.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,11 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> write your explanation here = I'vee added a return statement to the function to ensure it returns the sum of the two numbers.
// 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)}`);
22 changes: 18 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// Predict and explain first... - const num = 103; is declared outside the function, so it will not be used in the getLastDigit function.

// Predict the output of the following code:
// Predict the output of the following code: - error.
// =============> Write your prediction here

const num = 103;
Expand All @@ -14,11 +14,25 @@ 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
// =============> 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
// =============> write your explanation here - because const cant be used with other numbers, it is only used with the first number declared. and the function is not taking any parameters, so it will always return the last digit of the first number declared.
// 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
// our function getLastDigit does not accept any parameters, so it always uses the global variable num (which is 103).
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)}`);
7 changes: 5 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,8 @@
// 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
}
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
// return the BMI of someone based off their weight and height
}
console.log(calculateBMI(80, 1.81));
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 toUpperCaseSnake(sentence){
sentence = sentence.split(' ').join('_').toUpperCase();
return sentence;
}
console.log(toUpperCaseSnake("Do what you can, with what you have, where you are."));
15 changes: 15 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,18 @@
// 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) {
const penceStringWithoutTrailingP = penceString.slice(0, -1);
const paddedPence = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPence.slice(0, -2);
const pence = paddedPence.slice(-2).padEnd(2, "0");

return `£${pounds}.${pence}`;
}



console.log(toPounds("399p"));
console.log(toPounds("99p"));

12 changes: 7 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> write your answer here - 3 times, once for each of totalHours, remainingMinutes, and remainingSeconds.

// 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
// =============> write your answer here - the value assigned to pad is 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here - "00" — because pad(0) => "0".padStart(2, "0") => "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
// =============> write your answer here - 1 — because the last call to pad is for remainingSeconds, which 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
// =============> write your answer here. - "01" — because pad(1) => "1".padStart(2, "0") => "01".
38 changes: 21 additions & 17 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
// 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`;
const [hh, mm] = time.split(":");
let hours = Number(hh);
const minutes = mm;
let suffix = "am";

if (hours === 0) {
hours = 12;
} else if (hours === 12) {
suffix = "pm";
} else if (hours > 12) {
hours -= 12;
suffix = "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 paddedHours = hours.toString().padStart(2, "0");
return `${paddedHours}:${minutes} ${suffix}`;
}

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.assert(formatAs12HourClock("00:00") === "12:00 am", "Midnight");
console.assert(formatAs12HourClock("01:30") === "01:30 am", "1:30 AM");
console.assert(formatAs12HourClock("13:15") === "01:15 pm", "1:15 PM");
console.assert(formatAs12HourClock("23:59") === "11:59 pm", "11:59 PM");
console.assert(formatAs12HourClock("09:05") === "09:05 am", "9:05 AM");