Skip to content

WESTMIDLANDS| ITP-MAY 2025| ROJA ALAGURAJAN| Module Structuring and Testing Data|sprint 2 #612

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 5 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
11 changes: 10 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// This code will cause a SyntaxError because the variable `str` is being declared twice within the same function scope.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +10,13 @@ function capitalise(str) {
}

// =============> write your explanation here
// Explanation:
// The function parameter is named `str`. Inside the function, the same name `str` is redeclared with `let`.
// JavaScript does not allow redeclaring a variable in the same scope with `let` or `const`, so this causes a SyntaxError.
// The error message would say something like: "Identifier 'str' has already been declared".

// =============> write your new code here
function capitalise(str) {
const result = `${str[0].toUpperCase()}${str.slice(1)}`;
return result;
}
22 changes: 17 additions & 5 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Predict and explain first...

// Predict and explain first.:- SyntaxError because the variable 'decimalNumber' is being declared twice
// Why will an error occur when this program runs?
// =============> write your prediction here

// The first decimalNumber is a function parameter and the second decimalNumber is declared using const inside the function body.
//JavaScript does not allow a const or let declaration to reuse the name of a parameter in the same scope.

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

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

return percentage;
Expand All @@ -15,6 +16,17 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
/*The error happens because the variable name 'decimalNumber' is being used twice in the same function scope:
once as a parameter, and once as a const variable.
note:- JavaScript does not allow redeclaring a parameter name using const or let.
To fix the error, so, we should either rename the internal variable, or just use the parameter directly.*/

// 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.5));
18 changes: 10 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

// Predict and explain first BEFORE you run any code...
// Predict and explain first BEFORE you run any code:-
// its SyntaxError: Unexpected number because its invalid parameter

// 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
// =============> write the error message :-SyntaxError: Unexpected number

// =============> explain this error message here
// =============> explain this error message here:- its SyntaxError: Unexpected number because the parameter '3' is a number literal, but function parameters must be valid variable names.
// this function should square any number but instead we're going to get an error

// Finally, correct the code to fix the problem

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

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

console.log(square(3))
17 changes: 12 additions & 5 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

// =============> write your prediction here
// Predict and explain first:--\
// The multiply function logs the product of a and b but does not return anything.So the console.log will print:
//The result of multiplying 10 and 32 is undefined

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
// he function multiply(a, b) uses console.log to output the product but does not have a return statement.

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



function multiply(a, b) {
return a * b; // Return the product instead of logging it
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
16 changes: 12 additions & 4 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// Predict and explain first...
// The function sum(a, b) includes a return statement followed by a line with 'a + b'.
// error:-The sum of a and b is undefined

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

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

// =============> write your explanation here
// The return statement ends before 'a + b' due to JavaScript's automatic semicolon insertion.
// As a result, the function returns undefined and 'a + b' is never executed.

// 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)}`);
27 changes: 23 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
//I predict that the output of this code will not be as expected because the variable num has been declared with a value and it cannot be changed because it is a constant variable.
// There is also no parameter in the getLastDigit function. So even though the console.log statements are trying to call the getLastDigit function with different arguments,
// the function will not return any of the sliced version of these values.

const num = 103;

Expand All @@ -14,11 +17,27 @@ 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 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 fact that 3 is the result of each console.log statement is because the getLastDigit function is not actually using the arguments of 42, 105, and 806.
Instead, because the num variable was assigned with 103 */

// 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
/*Originally the getLastDigit function didn't have a parameter to accept an argument,
so it will always use the constant num variable defined at the top of the script to evaluate the last digit.
*/
11 changes: 9 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
function calculateBMI(weightKg, heightM) {
// return the BMI of someone based off their weight and height
}
square=(heightM*heightM);
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is square being declared?

const bmi = weightKg / square;
// return the BMI to 1 decimal
return bmi.toFixed(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of value do you expect the function to return? A number or a string?
Does your function return the type of value you expect it to return?


}
console.log(`The BMI of a person with a weight of 70kg and a height of 1.73m is ${calculateBMI(70, 1.73 )}`);
// logs "The BMI of a person with a weight of 70kg and a height of 1.73m is 23.4" to the console.
11 changes: 11 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,14 @@
// 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

/* I create a function named strToUppersnakeCase that replaces spaces with underscores,
and then call the function in the console to print the result */

function strToUppersnakeCase(str) {
return str.toUpperCase().replace(/\s+/g, "_");
}

// Calling the function and printing the result in the console
console.log(strToUppersnakeCase("hello there"));
console.log(strToUppersnakeCase("lord of the rings"));
42 changes: 42 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,45 @@
// 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

/*const penceString = "399p";// Initializes the string and extracts all characters except the trailing 'p'
const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); // Extracts the pounds part by taking all digits except the last two
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); // Extracts the pence portion of the price by taking the last two digits and ensures it has exactly two digits.
console.log(`£${pounds}.${pence}`); */

function toPounds(pence) {
if (typeof pence !== "string" || !pence.endsWith("p")) {
return "Invalid input. The input must be a string ending with 'p'.";
}

const penceStringWithoutTrailingP = pence.substring(0, pence.length - 1);
const penceNumber = Number(penceStringWithoutTrailingP);

if (Number.isInteger(penceNumber) && penceNumber >= 0) {
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(
3,
"0"
);
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pennies = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

return ${pounds}.${pennies}`;
} else {
return "Invalid input. The pence amount must be in number format and must be whole numbers that are not less than 0";
}
}

console.log(toPounds("5498p")); //result is £54.98
console.log(toPounds("63p")); //result is £0.63
console.log(toPounds("5498")); //result is "Invalid input. The input must be a string ending with 'p'."
console.log(toPounds("-99p")); //result is "Invalid input. The pence amount must be in number format and must be whole numbers that are not less than 0"
console.log(toPounds("pp")); //result is "Invalid input. The pence amount must be in number format and must be whole numbers that are not less than 0"
console.log(toPounds("12.7p")); //Invalid input. The pence amount must be in number format and must be whole numbers that are not less than 0
21 changes: 16 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,29 @@ function formatTimeDisplay(seconds) {
// Questions

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



// Call formatTimeDisplay with an input of 61, now answer the following:
//console.log(formatTimeDisplay(61)); //result is "00:01:01"

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// 0
//(The value of totalHours)



// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// "00"
//(0.toString().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
// 1
//The value assigned to num when pad is called for the last time is 1. This is because the function takes an input of 61
// and the remainingSeconds variable takes the remainder when 61 is divided by 60 .or in other words the number of seconds left after dividing 61 seconds into full minutes

// 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"
//(1.toString().padStart(2, "0") → "01")
Loading