Skip to content

LONDON | ITP_MAY | Mirabel Adom | Module-Structuring-and-Testing-Data | Sprint-2 #655

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 1 commit 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
12 changes: 8 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Predict and explain first...
// =============> write your prediction here
// The code will produce an error.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
//The error message indicates that 'str' has already been declared: function capitalise(str) and let str

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

// =============> write your explanation here
// =============> write your new code here
// =============> The error message indicates that 'str' has already been declared.
// =============>
function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}
23 changes: 14 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> An error code will occur because 'decimalNumber' has already been declared.

// 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 original code didn't work because the variable name was reused and the value was printed 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));
20 changes: 10 additions & 10 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

// 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 produce an error because the function parameters must be variable names, instead of a value - in this case the error has occured because of 3.

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
// =============> The error message suggests that JavaScript requires a variable name rather than a number.

// Finally, correct the code to fix the problem

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


// =============>
function square(num) {
return num * num;
}
8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...

// =============> write your prediction here
//The code does not include a return statement which means that the output is undefined.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -11,4 +11,8 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// =============> write your explanation here

// 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)}`);
11 changes: 8 additions & 3 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
// The code is incorrect because there is a semi-colon in line 5 and prevent line 6 from running: return;

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
//The code below ensures that the function is defined. This can be achieved by removing the semi-colon in line 5 of the previous code.

// 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: 21 additions & 6 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 and explain first
// The three calls will all return the value 3

// Predict the output of the following code:
// =============> Write your prediction here
The last digit of 42 is 3
The last digit of 105 is 3
The last digit of 806 is 3

const num = 103;

Expand All @@ -14,11 +17,23 @@ 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
// Finally, correct the code to fix the problem
// =============> write your new code here
// the function always uses the constant num, which is set to 103, so it always returns '3'

// Finally, correct the code to fix the problem
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
function getLastDigit(n) {
return n.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)}`);

// the function always uses the constant num which is set to 10; it ignores other arguments and consistently returns "3", the last digit of 103

8 changes: 6 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,9 @@
// 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
}
// return the BMI of someone based off their weight and height
}
const heightSquared = height * height;
return `${(weight / heightSquared).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(calculateBMI(85, 1.80));
5 changes: 5 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,8 @@
// 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 convertToUpperSnakeCase(input) {
return input.toUpperCase().split(" ").join("_");
}
//console.log(convertToUpperSnakeCase("may the force be with you"));
44 changes: 44 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,47 @@
// 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";

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

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

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

console.log(`£${pounds}.${pence}`);
Comment on lines +10 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these code still needed?


//Refactored code
function toPounds(penceString) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

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

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

// console.log(toPounds("399p")); // £3.99
// console.log(toPounds("99p")); // £0.99
// console.log(toPounds("5p")); // £0.05
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
// The pad function will be called three times.

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

// c) What is the return value of pad is called for the first time?
// =============> 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
// The value assigned to num is 1
//pad(totalHours); // totalHours = 0

// 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
//In the pad function, since num is 1, the return value will be "01" (formatted to two digits).
// The return value is "01".