Skip to content

Commit 64fd843

Browse files
committed
sprint 2 completed --fixed
1 parent 3372770 commit 64fd843

File tree

11 files changed

+195
-39
lines changed

11 files changed

+195
-39
lines changed

Sprint-2/1-key-errors/0.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I am not able to predict any error, everything seems fine to me.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,11 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
// =============> Yeah, str name for the variable is already being used as the parameter of the function,
13+
// that's why we can't declare it agian as being done in line 8.
14+
// =============> the correct code would be as follows:
15+
16+
/*function capitalise(str) {
17+
let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`;
18+
return capitaliseStr;
19+
}*/

Sprint-2/1-key-errors/1.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
3+
// =============> There would be a syntax error because the decimalNumber is declared again as const
4+
//and logically there is no need for initializing decimalNumber again with 0.5 value as in that case the method would return 50% always
65
// Try playing computer with the example to work out what is going on
76

87
function convertToPercentage(decimalNumber) {
@@ -14,7 +13,13 @@ function convertToPercentage(decimalNumber) {
1413

1514
console.log(decimalNumber);
1615

17-
// =============> write your explanation here
16+
// =============> Yes, the error is because the identifier decimalNumber has already been declared.
1817

1918
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
19+
// =============> the correct code would be as follows:-
20+
21+
/*function convertToPercentage(decimalNumber) {
22+
const percentage = `${decimalNumber * 100}%`;
23+
24+
return percentage;
25+
}*/

Sprint-2/1-key-errors/2.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33

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

6-
// =============> write your prediction of the error here
6+
// =============> In the parameters of function we can write the name of the variable but not the actual value.
7+
//we write actual value when we call the function.
78

89
function square(3) {
910
return num * num;
1011
}
1112

1213
// =============> write the error message here
14+
/*
15+
function square(3) {
16+
^
17+
18+
SyntaxError: Unexpected number
19+
*/
1320

14-
// =============> explain this error message here
21+
// =============> Yeah, the error is because that when writing the function it does not expect number value instead of the parameter name
1522

1623
// Finally, correct the code to fix the problem
1724

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

27+
/*
28+
function square(num) {
29+
return num * num;
30+
}
31+
*/
2032

Sprint-2/2-mandatory-debug/0.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
// Predict and explain first...
22

33
// =============> write your prediction here
4+
//I think that the problem is our function multiply does not return any value but write in console
5+
// However, we are calling the function inside another console.log and expecting the function to return some value.
46

57
function multiply(a, b) {
68
console.log(a * b);
79
}
810

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

11-
// =============> write your explanation here
13+
// =============> Yeah, when we run the code we get the following output which shows that second console is not able to get the value from the function as the function does not return any value.
14+
/*
15+
320
16+
The result of multiplying 10 and 32 is undefined
17+
*/
1218

1319
// Finally, correct the code to fix the problem
1420
// =============> write your new code here
21+
/*
22+
function multiply(a, b) {
23+
return a * b;
24+
}
25+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
26+
*/

Sprint-2/2-mandatory-debug/1.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// In the below function, after return there is a semi colon which syntactically would just return nothing and any line of code below that would not run
44
function sum(a, b) {
55
return;
66
a + b;
@@ -9,5 +9,12 @@ function sum(a, b) {
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
//The function is returning undefined
13+
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
/*
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
*/

Sprint-2/2-mandatory-debug/2.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
5-
4+
/* The last digit of 42 is 2
5+
The last digit of 105 is 5
6+
The last digit of 806 is 6
7+
*/
68
const num = 103;
79

8-
function getLastDigit() {
10+
function getLastDigit(num) {
911
return num.toString().slice(-1);
1012
}
1113

@@ -15,10 +17,23 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
/*
21+
The last digit of 42 is 3
22+
The last digit of 105 is 3
23+
The last digit of 806 is 3
24+
*/
1825
// Explain why the output is the way it is
19-
// =============> write your explanation here
26+
// =============> I just realized now that the function getLastDigit is just always just returning the last digit of cons num
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
/*
31+
function getLastDigit(num) {
32+
return num.toString().slice(-1);
33+
}
34+
35+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2238
23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
39+
*/

Sprint-2/3-mandatory-implement/1-bmi.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
// It should return their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
18+
return (weight / (height * height)).toFixed(1);
19+
}
20+
21+
console.log(calculateBMI(90, 1.83));

Sprint-2/3-mandatory-implement/2-cases.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
18+
/*
19+
1.Split the String by comma to get the separate words
20+
2.Join the separate words by _ to convert the string into snake case
21+
3. uppercase all the letters in the string using toUpperCase Method in Javascript
22+
*/
23+
function upperSnakeCase(myString) {
24+
return myString.split(" ").join("_").toUpperCase();
25+
}
26+
27+
console.log(upperSnakeCase("My name is Mehroz"));

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
function toPounds(penceString) {
9+
const penceStringWithoutTrailingP = penceString.substring(
10+
0,
11+
penceString.length - 1
12+
);
13+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
14+
const pounds = paddedPenceNumberString.substring(
15+
0,
16+
paddedPenceNumberString.length - 2
17+
);
18+
19+
const pence = paddedPenceNumberString.substring(
20+
paddedPenceNumberString.length - 2
21+
);
22+
//.padEnd(2, "0");
23+
return ${pounds}.${pence}`;
24+
}
25+
26+
console.log("400p in pounds is :" + toPounds("400p"));
27+
console.log("420p in pounds is :" + toPounds("420p"));
28+
console.log("0p in pounds is :" + toPounds("0p"));
29+
console.log("1p in pounds is :" + toPounds("1p"));
30+
console.log("248p in pounds is :" + toPounds("248p"));

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ function pad(num) {
33
}
44

55
function formatTimeDisplay(seconds) {
6-
const remainingSeconds = seconds % 60;
7-
const totalMinutes = (seconds - remainingSeconds) / 60;
8-
const remainingMinutes = totalMinutes % 60;
9-
const totalHours = (totalMinutes - remainingMinutes) / 60;
6+
const remainingSeconds = seconds % 60; // 1
7+
const totalMinutes = (seconds - remainingSeconds) / 60; //1
8+
const remainingMinutes = totalMinutes % 60; // 1
9+
const totalHours = (totalMinutes - remainingMinutes) / 60; //0
10+
11+
console.log(totalHours + " " + remainingMinutes + " " + remainingSeconds);
1012

1113
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1214
}
@@ -17,18 +19,18 @@ function formatTimeDisplay(seconds) {
1719
// Questions
1820

1921
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
22+
// =============> 3
2123

2224
// Call formatTimeDisplay with an input of 61, now answer the following:
2325

2426
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
27+
// =============> 0
2628

2729
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
30+
// =============> 00
2931

3032
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
33+
// =============> Value assigned to num is '1' i.e. the value fo remaining seconds. As when in the program we did seconds % 60 we got the remaining seconds as 1
3234

3335
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
36+
// =============> the value assigned is '01' because when the function pad is called on the value 1, the padStart method inside that function applies the pad length of 2 using 0.

0 commit comments

Comments
 (0)