Skip to content

London | May-2025 | Fatima Z Belkedari | Module Structuring and Testing Data | Sprint 2 #610

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 20 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
17 changes: 13 additions & 4 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
// The instruction wants to define a function with the input (str)

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// the error is in the use of let str. (str) is already an input and you can't use it again.


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

// =============> write your explanation here
//In order to fix the problem we need to declare let with capitalise instead of str.
// Because if we use the parameter (str), it creates a naming conflict in the same scope.


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

function capitlise(str) {
let capitalise =`${str[0].toUpperCase()}${str.slice(1)}`;
return capitalise;
}
console.log(capitlise("good"));
20 changes: 18 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Predict and explain first...

//I think it will create a syntax error.Since decimal number is inside the funtion while running the function.
// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here synax error such us decimal number is not defined

// 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 @@ -15,6 +16,21 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// we declared a function convertToPercentage assigned with decimalNumber
//as a parameter then we use const to declare a new variable using the
// parameter's name which is wrong.So here again decimalNumber was either a parameter
// or declared in wrong way inside the function. We notice that console.log is placed outside the function
// and in order to return its value, it will show reference error. You may ask why? Well, simply, because
// there is no such global thing defined outside the function.

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

15 changes: 12 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

// Predict and explain first BEFORE you run any code...
//I think we should define number first before returning it.

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> write your prediction of the error here.
//number undefined.


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

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

//Unexpected number
// =============> explain this error message here

// It is not allowed to use numbers in a variable parameter.
// Finally, correct the code to fix the problem

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


function square(number) {
const result = number * number;
return result;
}

console.log(square(3));
10 changes: 9 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...

// It will print the result as undefined.
// =============> write your prediction here
//undefined because there is no return of the value.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +10,13 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// when the code run, it will show "the result of multiplying 10 and 32 is undefined because
// // what console.log does is that it prints the result of a*b but it doesn't return any 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)}`);
9 changes: 8 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

// the result would be: the sum of 10 and 32 is undefined.
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +9,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// the function is defined with the name "sum" there is no return for anything. And if we want to print the result
// it would be: the sum of 10 and 32 is undefined and what we are actually looking for is 42.
// 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)}`);
25 changes: 20 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Predict and explain first...

// Predict the output of the following code:
//I think the result would be 3 for 42, 105 and 806 because cost num is declared outside the function
// in the global scope. And this means any input would take the result of the global scope.
// Predict the output of the following code:3
// =============> Write your prediction here
// the result would be 3 for 42, 105 and 806. Because cost num =103 is defined outside the function.

const num = 103;

function getLastDigit() {
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)}`);

// 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 the output here: the last digit of 42, 105 and 806 is 3
// Explain why the output is the way it is. Because when we declared the variable const num =103 it is outside the function(global variable)
// so everytime we call the function, it will use the 103 as a reference.
// =============> write your explanation here
// The use of global variable outside the function doesn't accept any arguments. It always refers back to 103 and
// return the last digit which is 3
// Finally, correct the code to fix the problem
// =============> write your new code here

function getTheLastDigit(number){
return number.toString().slice(-1);

}
console.log(`The last digit of 42 is ${getTheLastDigit(42)}`);
console.log(`The last digit of 105 is ${getTheLastDigit(105)}`);
console.log(`The last digit of 806 is ${getTheLastDigit(806)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
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 @@ -15,5 +15,8 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const square =weight/ (height*height);
return square.toFixed(1);
// return the BMI of someone based off their weight and height
}
}
console.log(calculateBMI(55, 1.65));
9 changes: 9 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,12 @@
// 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(input){
Copy link

Choose a reason for hiding this comment

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

Does this function name follow the usual camelCase convention?

Copy link
Author

Choose a reason for hiding this comment

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

No, it does not. "camelCase" starts with a small lowercase letter and each new word starts with capital letter.

return input
.trim()
.toUpperCase()
.replace(/\s+/g, '_');
}
console.log(ToUpperSnakeCase("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) {

const numericString = penceString.substring(0, penceString.length - 1);

const padded = numericString.padStart(3, "0");

const pounds = padded.slice(0, -2);
const pence = padded.slice(-2);

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

console.log(toPounds("399p"));
console.log(toPounds("7p"));
console.log(toPounds("50p"));
console.log(toPounds("120p"));
console.log(toPounds("559P"));
10 changes: 5 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,18 @@ 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 pad will be called. For HH , MM and SS.

// 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 first time pad is called is with totalhours and it 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

// 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: it is 1 for the remainigseconds. So we used the 61%60 =1 So remaining seconds 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.
14 changes: 12 additions & 2 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

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 `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}
return `${time} am`;
return `${String(hours).padStart(2, "0")}:${minutes} am`;
}

const currentOutput = formatAs12HourClock("08:00");
Expand All @@ -23,3 +30,6 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.assert(formatAs12HourClock("13:00") === "01:00 pm");
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("17:30")=== "05:30 pm");