Skip to content

Commit 377fec1

Browse files
Finish section 1 and 2 for sprint 2 course work.
1 parent 3372770 commit 377fec1

File tree

6 files changed

+110
-9
lines changed

6 files changed

+110
-9
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
/*
5+
My prediction is that this function will take the first letter of a string and capital it.
6+
*/
7+
48
// call the function capitalise with a string input
59
// interpret the error message and figure out why an error is occurring
610

7-
function capitalise(str) {
11+
/*function capitalise(str) {
812
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
10-
}
14+
}*/
1115

1216
// =============> write your explanation here
17+
18+
/*
19+
The main reason why the capitalise function have a not define error because the the str variable have been declare in the parameter
20+
inside the function, the str variable is re-declare again that will break the rule of how variable work.
21+
*/
22+
1323
// =============> write your new code here
24+
25+
function capitalise(str) {
26+
let firstCapitaLetter = `${str[0].toUpperCase()}${str.slice(1)}`;
27+
return firstCapitaLetter;
28+
};
29+
console.log(capitalise("hello"));

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// When the JS executed the code, it will give 2 possible error 1 is decimalNumber is define inside the function and decimalNumber is undefined when using console.log
56

67
// Try playing computer with the example to work out what is going on
7-
8+
/*
89
function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +14,26 @@ function convertToPercentage(decimalNumber) {
1314
}
1415
1516
console.log(decimalNumber);
16-
17+
*/
1718
// =============> write your explanation here
1819

20+
/*
21+
For function convertToPercentage there are 1 error:
22+
inside the function the decimalNumber parameter is being re-declare and given a fix value, this will give this function a
23+
decimalNumber are ready defined.
24+
25+
Next is when we console.log the function instead of calling the function name and value we used the function parameter variable name
26+
instead. This will give the error decimalNumber is undefined.
27+
*/
28+
1929
// Finally, correct the code to fix the problem
2030
// =============> write your new code here
31+
32+
33+
function convertToPercentage(decimalNumber) {
34+
const percentage = `${decimalNumber * 100}%`;
35+
36+
return percentage;
37+
}
38+
39+
console.log(convertToPercentage(0.25));

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55

66
// =============> write your prediction of the error here
77

8+
// For function square the variable inside the parameter will have a error, because number cant be used when name a variable and num variable is undefine.
9+
/*
810
function square(3) {
911
return num * num;
1012
}
11-
13+
*/
1214
// =============> write the error message here
15+
// SyntaxError: Unexpected number
1316

1417
// =============> explain this error message here
18+
//Because number cant be used when name a variable.
1519

1620
// Finally, correct the code to fix the problem
1721

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

20-
24+
function square(num) {
25+
return num * num;
26+
};
27+
console.log(square(3));

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

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

33
// =============> write your prediction here
4+
/*
5+
The function multiply when is log into the terminal. we will receive a print out expression from
6+
first console.log inside the function and undefined value from the string template inside the section console.log.
7+
*/
48

9+
/*
510
function multiply(a, b) {
611
console.log(a * b);
712
}
813
914
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
15+
*/
1016

1117
// =============> write your explanation here
18+
/*
19+
First the function multiply have 2 parameter identifier a and b, the main purpose of this function is to multiply any argument that put
20+
into the identifier a and b. Inside the the function is we have the function call console.log and nested inside is the expression from multiply of a and b.
21+
However this will lead to error, since we need to return the expression(value) back to the function so it can be re used when call in a string template or other
22+
function.
1223
24+
*/
1325
// Finally, correct the code to fix the problem
1426
// =============> write your new code here
27+
28+
function multiply(a, b) {
29+
return a * b;
30+
}
31+
32+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// The function sum when is call inside the string template. The print out will be "The sum of 10 and 32 is undefined".
4+
/*
45
function sum(a, b) {
56
return;
67
a + b;
78
}
89
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11+
*/
1112
// =============> write your explanation here
13+
14+
/*
15+
For the function sum the writing format is correct but however instead of return the expression from the combine value
16+
of a and b, right now there is no expression return back to the function. So when the function is used inside the string template,
17+
it give the value of undefined instead of actual value of a and b combine together.
18+
*/
19+
1220
// Finally, correct the code to fix the problem
1321
// =============> write your new code here
22+
23+
function sum(a, b) {
24+
return a + b;
25+
}
26+
27+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// Predict the output of the following code:
44
// =============> Write your prediction here
55

6+
/* The function getLastDigit should get the last digit number from any number argument pass into the function, but however it only take
7+
value from variable num and only give out the value of 3.
8+
*/
9+
10+
/*
611
const num = 103;
712
813
function getLastDigit() {
@@ -12,13 +17,35 @@ function getLastDigit() {
1217
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1318
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1419
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
20+
*/
1621
// Now run the code and compare the output to your prediction
1722
// =============> write the output here
23+
/*
24+
The last digit of 42 is 3
25+
The last digit of 105 is 3
26+
The last digit of 806 is 3
27+
*
28+
/
29+
1830
// Explain why the output is the way it is
1931
// =============> write your explanation here
32+
/*
33+
Because the function getLastDigit when use, instead of picking last digit number of any random number argument pass into the function
34+
it only pick last digit number from the variable num value of 103. So when the user try to input other random number into
35+
the function is only print out the number 3 as the result.
36+
*/
2037
// Finally, correct the code to fix the problem
2138
// =============> write your new code here
2239

40+
const num = 103;
41+
42+
function getLastDigit(digit) {
43+
return digit.toString().slice(-1);
44+
}
45+
console.log(`The last digit of 103 is ${getLastDigit(num)}`);
46+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
47+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
48+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
49+
2350
// This program should tell the user the last digit of each number.
2451
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)