You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// I think that the first letter of the string will be upper case and then the .slice method will extract a part of a string and return the extracted part, being --> apitalise.
3
4
4
5
// call the function capitalise with a string input
5
6
// interpret the error message and figure out why an error is occurring
7
+
// function capitalise is already a variable and let str is decalring that variable again but str already exists.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+24-8Lines changed: 24 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,39 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
// The function does not take a parameter.
6
+
//const num = 103;
5
7
6
-
constnum=103;
8
+
//function getLastDigit() {
9
+
//return num.toString().slice(-1);
10
+
//}
7
11
8
-
functiongetLastDigit(){
9
-
returnnum.toString().slice(-1);
10
-
}
11
-
12
-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
12
+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
15
16
16
// Now run the code and compare the output to your prediction
17
17
// =============> write the output here
18
+
// The last digit of 42 is 3
19
+
// The last digit of 105 is 3
20
+
// The last digit of 806 is 3
18
21
// Explain why the output is the way it is
19
22
// =============> write your explanation here
23
+
// This happens because the functionn does not accept a parameter, it always uses the global variable num and the arguments passed in (42, 105, 806) are ignored.
20
24
// Finally, correct the code to fix the problem
21
25
// =============> write your new code here
22
26
27
+
functiongetLastDigit(num){
28
+
returnnum.toString().slice(-1);
29
+
}
30
+
31
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34
+
23
35
// This program should tell the user the last digit of each number.
24
36
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments