Skip to content

Commit 872131a

Browse files
committed
solution for sprint 2
1 parent d873260 commit 872131a

File tree

11 files changed

+127
-18
lines changed

11 files changed

+127
-18
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
4+
// The code will capitalize the first word of the str which gives "Str"
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
68

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
9+
// The error occurs in line 13 and it says "SyntaxError: Identifier 'str' has already been declared"
10+
// The error is informing us that the string str has already been declared in line 12 so we can not declare it again.
11+
12+
//function capitalise(str) {
13+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
14+
// return str;
15+
//}
1116

1217
// =============> write your explanation here
18+
19+
// The code is written to capitalize the first letter of str which is expected to give the result Str.
20+
1321
// =============> write your new code here
22+
23+
function capitalise(str) {
24+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
25+
return str;
26+
}
27+
console.log(capitalise("str"))

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// This code is tend to be a code to convert decimal number in to %. I predict there will be an error because the call for the function has a bug.
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9+
/*function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
13-
}
14+
}/
1415
15-
console.log(decimalNumber);
16+
console.log(decimalNumber);*/
1617

1718
// =============> write your explanation here
19+
// A syntaxError has been observed because the decimalNumber is already declared.
20+
// if we delete line 10 anther error happen this time about calling the function in which decimalNumber is not defined in the global scope.
1821

1922
// Finally, correct the code to fix the problem
2023
// =============> write your new code here
24+
function convertToPercentage(decimalNumber) {
25+
const percentage = `${decimalNumber * 100}%`;
26+
27+
return percentage;
28+
}
29+
30+
console.log(convertToPercentage("0.5"));

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
/* There is a problem in the function parameter. we can not use number in parameter.
8+
even though line 12 by it self has no coding problem but the num is not declared before.*/
79

10+
/*
811
function square(3) {
912
return num * num;
1013
}
14+
*/
1115

1216
// =============> write the error message here
13-
17+
/* unexpected number
1418
// =============> explain this error message here
19+
we can not use number in a parameter so 3 is not used as identifier */
1520

1621
// Finally, correct the code to fix the problem
1722

1823
// =============> write your new code here
24+
function square(num) {
25+
return num * num;
26+
}
27+
console.log(square("3"))
1928

2029

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

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

33
// =============> write your prediction here
4+
/*the function doesn't return any thing so there will be an error. */
45

5-
function multiply(a, b) {
6+
/*function multiply(a, b) {
67
console.log(a * b);
78
}
89
9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/
1011

1112
// =============> write your explanation here
13+
/* As predicted the function does not return any thing. But the first call gives 320. and the second undefine. */
1214

1315
// Finally, correct the code to fix the problem
1416
// =============> write your new code here
17+
function multiply(a, b) {
18+
return a * b;
19+
}
20+
21+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
/* The code will give us the sum of 10 and 32 which will be " The sum of 10 and 32 is 42" */
34

4-
function sum(a, b) {
5+
/*function sum(a, b) {
56
return;
67
a + b;
78
}
89
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/
1011

1112
// =============> write your explanation here
13+
/* the call of the function gives undefine result. the reason for this is the semicolon after return which shouldn't be placed there.
1214
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
15+
// =============> write your new code here */
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
/* I predict that the three call are giving
6+
The last digit of 42 is 2
7+
The last digit of 105 is 5
8+
The last digit of 806 is 6 */
59

6-
const num = 103;
10+
/*const num = 103;
711
812
function getLastDigit() {
913
return num.toString().slice(-1);
@@ -12,13 +16,25 @@ function getLastDigit() {
1216
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1317
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1418
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
19+
*/
1620
// Now run the code and compare the output to your prediction
1721
// =============> write the output here
22+
/* The last digit of 42 is 3
23+
The last digit of 105 is 3
24+
The last digit of 806 is 3 */
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
/* because the num is determined at fires and 103 is given to the num. as a result we see the same result.*/
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
2230

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)}`);
38+
2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// Then when we call this function with the weight and height
1515
// It should return their Body Mass Index to 1 decimal place
1616

17-
function calculateBMI(weight, height) {
17+
function calculateBMI(weight, height) {
18+
const bmi= weight/ (height*height)
19+
return bmi.toFixed(1);
1820
// return the BMI of someone based off their weight and height
19-
}
21+
}
22+
console.log(calculateBMI(86,1.70));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
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+
function toUpperCase(text) {
18+
return text.toUpperCase();
19+
}
20+
21+
console.log(toUpperCase("hello there"));
22+
console.log(toUpperCase("lord of the ring"));

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,14 @@
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+
function toPounds(pennies) {
8+
const pounds = pennies / 100;
9+
return pounds.toFixed(2);
10+
}
11+
12+
// calling the function to make sure it works
13+
console.log(toPounds(32391));
14+
console.log(toPounds(3239));
15+
console.log(toPounds(323));
16+
console.log(toPounds(32));
17+
console.log(toPounds(3));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13+
console.log(formatTimeDisplay(61));
1314

1415
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1516
// to help you answer these questions
@@ -18,17 +19,22 @@ function formatTimeDisplay(seconds) {
1819

1920
// a) When formatTimeDisplay is called how many times will pad be called?
2021
// =============> write your answer here
22+
/* pad will be called 3 times. one for totalHours, one for remainingMinutes and one for remainingSeconds.*/
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?
2527
// =============> write your answer here
28+
/* when I run the code the result is 00:01:01 there for the value assigned to num is "0".
2629
2730
// c) What is the return value of pad is called for the first time?
2831
// =============> write your answer here
32+
/* the return value is "00". this comes 0 changed to string and then padstart(2,0) changes it to "00" */
2933

3034
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3135
// =============> write your answer here
36+
/* The value assigned to num when the pad is called for the last time is "1". */
3237

3338
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3439
// =============> write your answer here
40+
/* The return value is 1. The code 1.toString() changes the num 1 to string "1" then the code .padStart(2, "0"); changes "1" to "01" */

0 commit comments

Comments
 (0)