Skip to content

NW-ITP |Mohammed Alzaki | Module-Structuring-and-Testing-Data| Sprint2 | week4 #390

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 11 commits into
base: main
Choose a base branch
from
16 changes: 13 additions & 3 deletions Sprint-2/1-key-errors/0.js
Copy link

Choose a reason for hiding this comment

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

This works fine

Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here

//The variable str has been declared twice the first one as the function
// and the second time inside the function => let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
// An error occured => SyntaxError: Identifier 'str' has already been declared
//because in javascript we cant declare a variable more than once within scope .

// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalise("cool"))
20 changes: 17 additions & 3 deletions Sprint-2/1-key-errors/1.js
Copy link

Choose a reason for hiding this comment

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

this works fine

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

//// The variable decimalNmber has been declared twice once as a parameter for convertToPercentage
// function and the second time 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(decimalNumber); */

// =============> write your explanation here
//The function convertToPercentage(decimalNumber) accepts a parameter decimalNumber
//Inside the function, ywe have decimalNumber redeclared using const decimalNumber = 0.5;, which creates a conflict
// Finally, correct the code to fix the problem
//we should remove the redeclaration of decimalNumber inside the function. Instead, use the parameter directly.and then
//pass a decimal number as input => 0.5 in the function call

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

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

const result = convertToPercentage(0.5);
console.log(result); // Output => "50%"
14 changes: 11 additions & 3 deletions Sprint-2/1-key-errors/2.js
Copy link

Choose a reason for hiding this comment

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

Code works fine

Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// The number three was passed straight to the function witch is why it's throwing an error

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

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

// =============> explain this error message here
//The function was expecting a parameter and got a number instead and a number
// can not be a valid parameter name
//We pass 3 as an argument when calling square(3), and the function returns 9 (since 3 squared is 9).

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3))
15 changes: 12 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Copy link

Choose a reason for hiding this comment

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

Code works fine

Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Predict and explain first...

// =============> write your prediction here
//In this function console.log() is used instead of return

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

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

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

//By using console.log() this multiply function dose not return anything and it just printing the output
//of => a * b to the console + undefined => since console.log does not return
// Finally, correct the code to fix the problem
// 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)}`);
15 changes: 13 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Copy link

Choose a reason for hiding this comment

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

Code works fine

Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
//Here we have return; and => a + b ; in seprate lines closed with semicolon.

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

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

// =============> write your explanation here
//AS we know that return end and stops the execution of the function witch mean that => a + b
//will not be executed and line 8 is useless since the function stops at line 7.

// 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: 25 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Copy link

Choose a reason for hiding this comment

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

Code works fine

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Predict and explain first...
// The constant variable num is set to = 103 ,and num should be passed as a parameter to getLastDigit function

// Predict the output of the following code:
// =============> Write your prediction here
/* The last digit will be the same for all values that will be passed to it
since it coming from the constant.*/

const num = 103;
/* const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -12,13 +15,33 @@ function getLastDigit() {
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
/* 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 output is the same because it coming from the execution of => num.toString().slice(-1);
//=> 3 over and over again

// 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
// To correct the code we needed to get rid of => const num = 103
//and pass num as a parameter to the function and now it giving the proper output
/* The last digit of 42 is 2
The last digit of 105 is 5
The last digit of 806 is 6 */
8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Copy link

Choose a reason for hiding this comment

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

code works fine

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
let squared = height * height;
let bmi = weight/squared ;
let bmiRounded = Math.round(bmi * 10) / 10;
return bmiRounded ;
// return the BMI of someone based off their weight and height
}
}

console.log(calculateBMI(70,1.73))
7 changes: 7 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Copy link

Choose a reason for hiding this comment

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

code works fine

Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
// 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(str) {
return str.replace(/\s+/g, '_').toUpperCase();
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
22 changes: 22 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Copy link

Choose a reason for hiding this comment

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

code works fine - my IDE showed one minor syntax consideration ; should be at the end. Also briefly summarise the code process in about 3 lines.

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@
// 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.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"))
10 changes: 10 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Copy link

Choose a reason for hiding this comment

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

this looks fine

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
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
Expand All @@ -18,17 +19,26 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
/* The pad function is called three times inside the formatTimeDisplay function:
Once for totalHours => to pad the hour value.
Once for remainingMinutes => to pad the minutes value.
Once for remainingSeconds =>to pad the seconds value. */

// 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 last time pad is called is for remainingSeconds, which is calculated as remainingSeconds = 61 % 60 = 1 when formatTimeDisplay(61) is called.

// 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
//The last call to pad is for remainingSeconds, which is 1. The padStart(2, "0") will pad this number to make it two digits.
//And return value =>"01"