Skip to content
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

WM4 | FATIMA_SAFANA | Module-Structuring-and-Testing-Data | WEEK5 #220

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

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

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

//The function will multiply a and b. it will return undefined.

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

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

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

// The function will return the sum of the two parameters a and b, which serves as placeholders for values.
// there was a syntax error

8 changes: 7 additions & 1 deletion Sprint-2/debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const num = 103;

function getLastDigit() {
function getLastDigit(num) {
return num.toString().slice(-1);
}

Expand All @@ -12,3 +12,9 @@ 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

// The function uses the method .tostring to convert the num to string
//then returns the value of index -1, that is the last value of the string.
//it wasn't working properly because the function has no parameter.


13 changes: 9 additions & 4 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Predict and explain first...

// call the function capitalise with a string input
// call the function capitalize 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;
function capitalize(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;

}

console.log(capitalize("dog"));

//SyntaxError: Identifier 'str' has already been declared
//The variable has been declared twice in the same scope.
12 changes: 8 additions & 4 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
// Try playing computer with the example to work out what is going on

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

Choose a reason for hiding this comment

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

What is percentage? Do you need it?


return percentage;
//return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(0.5));

//I predict function will return 50%
// I was wrong :
//
4 changes: 3 additions & 1 deletion Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

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

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

console.log(square(2));


65 changes: 61 additions & 4 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@

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

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}
const hoursPm = hours - 12
//padding with 0 to make sure the string length is 2
const adjustedHoursPm = String(hoursPm).padStart(2,0);
return `${adjustedHoursPm}:${min} pm`;

} else if (hours === 12){
return `12:${min} pm`;

}else if (hours === 00){
return `12:${min} am`;

}
//padding with 0 to make sure the string length is 2
const adjustedHoursAm = String(hours).padStart(2,0);
return `${adjustedHoursAm}:${min} am`;
};



const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
Expand All @@ -22,3 +38,44 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// test to check function formatAs12HourClock works for 12:00
const currentOutput3 = formatAs12HourClock("12:00");

const targetOutput3 = "12:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

// test to check function formatAs12HourClock works for 00:00
const currentOutput4 = formatAs12HourClock("00:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

// test to check function formatAs12HourClock works for time thats not on the hour during am time
const currentOutput5 = formatAs12HourClock("06:35");
const targetOutput5 = "06:35 am";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);

// test to check function formatAs12HourClock works for time thats not on the hour during pm time
const currentOutput6 = formatAs12HourClock("20:45");
const targetOutput6 = "08:45 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);

// test to check function formatAs12HourClock works for time thats not on the hour during pm time
const currentOutput7 = formatAs12HourClock("00:45");
const targetOutput7 = "12:45 am";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`
);
16 changes: 16 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function calBmi(weightkg, heightM) {
const bmi = weightkg / (heightM * heightM);
return Number(bmi.toFixed(1));
}

//console.log(12.46.toFixed(1));

// test1 to check the example test case
const currentOutput1 = calBmi(70, 1.73);
const targetOutput1 = 23.4;
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);

12 changes: 12 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution

function toUpperSnakeCase(str) {
const upperCase = str.toUpperCase()
return upperCase.replaceAll(" ", "_");
}

const sentence = 'fat dog'
console.log(toUpperSnakeCase(sentence));

const sentence1 = 'lord of the rings'
console.log(toUpperSnakeCase(sentence1));

46 changes: 46 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,49 @@
// 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 toPound(amountInPence){
// removes p
const penceStringWithoutTrailingP = amountInPence.substring(0, amountInPence.length - 1);
// makes sure the length is 3
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//returns the pounds from the string i.e the first number in the string
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// returns a the pence, i.e the substring(length-2) then pads it with 0 to make sure length is 2
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

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

// test to check function penceToPound 1digit input
const currentOutput = toPound("30p");
const targetOutput = "£0.30";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

// test to check function penceToPound 3digit input
const currentOutput1 = toPound("454p");
const targetOutput1 = "£4.54";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);

// test to check function penceToPound 1digit input
const currentOutput2 = toPound("4p");
const targetOutput2 = "£0.04";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// test to check function penceToPound 0 edge case
const currentOutput3 = toPound("0p");
const targetOutput3 = "£0.00";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

9 changes: 9 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
// Given a number,
// When I call this function with a number
// it returns the new price with VAT added on

function addVat(price){
const vat = 0.2;
const priceWithoutPoundSign = Number(price.substring(1));
return `£${(vat * priceWithoutPoundSign) + priceWithoutPoundSign}`;
}
Copy link

Choose a reason for hiding this comment

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

The spec is a bit vague, but I think it will probably make more sense for this function to deal with a number directly and return a number.

But what's matter is that you know how to implement a function. So no change is necessary.


console.log(addVat("£50"));

9 changes: 9 additions & 0 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ function pad(num) {
return num.toString().padStart(2, "0");
}

console.log(pad(2));

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
Expand All @@ -13,19 +15,26 @@ function formatTimeDisplay(seconds) {
)}`;
}

console.log(formatTimeDisplay(61));

// 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?
//3

// 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?
//0

// c) What is the return value of pad is called for the first time?
//00

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
//1 the value of 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
//01 the value of remainingSeconds is padded with length 2 and 0 making it 01