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 presume the str is repeatedly declared, first as a parameter and again as a variable.
3
+
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
@@ -9,5 +10,12 @@ function capitalise(str) {
9
10
returnstr;
10
11
}
11
12
12
-
// =============> write your explanation here
13
-
// =============> write your new code here
13
+
// =============> str is already declared in the parameter and shouldn't be declared inside the function. The error says it's been declared already for this reason. Therefore we need to rename the str inside the function.
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+11-3Lines changed: 11 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
2
3
3
// Why will an error occur when this program runs?
4
-
// =============> write your prediction here
4
+
// =============> The error will appear because we are trying to log the variable decimalNumber in the function. We have to call the function at this point. Besides to that, the decimalNumber is again declared inside the function where it shouldn't be.
5
5
6
6
// Try playing computer with the example to work out what is going on
7
7
@@ -14,7 +14,15 @@ function convertToPercentage(decimalNumber) {
14
14
15
15
console.log(decimalNumber);
16
16
17
-
// =============> write your explanation here
17
+
// =============> it says decimalNumber has aleady been declared for the above reason.
18
18
19
19
// Finally, correct the code to fix the problem
20
-
// =============> write your new code here
20
+
/* ===========> function convertToPercentage(decimalNumber) {
// Predict and explain first BEFORE you run any code...
3
2
4
3
// this function should square any number but instead we're going to get an error
5
4
6
-
// =============> write your prediction of the error here
5
+
// =============> It is going to throw an error message as 3 is written in incorrect position. That position is set only for parameters, not arguments.
7
6
8
7
functionsquare(3){
9
8
returnnum*num;
10
9
}
11
10
12
-
// =============> write the error message here
11
+
// =============> in the node repl, it says unexpected number, as the function always expects a parameter to be declared in its definition.
13
12
14
-
// =============> explain this error message here
13
+
// =============> The error is already described above.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/0.js
+5-2Lines changed: 5 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,10 @@ function multiply(a, b) {
8
8
9
9
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
10
11
-
// =============> write your explanation here
11
+
// =============> The function is only printing values on the console but not returning the value to the caller. Henceforth, it outputs both 320 and undefined on the console. 320 is the result of the console.log(a * b) inside the function, whereas undefined comes from the outer console.log(...) as it tries to bring forth and print the result of the function multiply(a, b). But the function is not returning any value and hence undefined appears on the console.
12
12
13
13
// Finally, correct the code to fix the problem
14
-
// =============> write your new code here
14
+
/*=============>functionmultiply(a,b){
15
+
returna*b;
16
+
}
17
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
// =============> As the function is not retuning anything, we're going to see undefined on the console.
3
3
4
4
functionsum(a,b){
5
-
return;
6
-
a+b;
5
+
returna+b;
7
6
}
8
7
9
8
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
9
11
-
// =============> write your explanation here
10
+
// =============> The function uses parameters a and b. It calculates the addition of a and b. Supposedly it should return the result and then we can use the value of the function, but it is returning nothing at all as return is closed without being specified.
12
11
// Finally, correct the code to fix the problem
13
-
// =============> write your new code here
12
+
/* =============> function sum(a, b) {
13
+
return a + b;
14
+
}
15
+
console.log(`The sum of 10 and 32 is ${(10, 32)}`);
// =============> the function is going to return error as the /1 is not valid expression or syntax
5
5
6
6
constnum=103;
7
7
8
-
functiongetLastDigit(){
8
+
functiongetLastDigit(num){
9
9
returnnum.toString().slice(-1);
10
10
}
11
11
@@ -14,11 +14,16 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
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
-
// =============> write the output here
17
+
/* ===========> function getLastDigit() {
18
+
return num.toString().slice(-1);
19
+
*/
18
20
// Explain why the output is the way it is
19
-
// =============> write your explanation here
21
+
// =============> this is because the /1 is not defined and not valid in the system of javaScript languague, so it throws an error. Morever because the parameter for the function is not defined, the function takes the global variable scope to run the code inside it. So obviously, we need to put down a parameter which is num to have the function work for any number other than the global variable scope.
20
22
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
23
+
/* ============> function getLastDigit(num) {
24
+
return num.toString().slice(-1);
25
+
}
26
+
*/
22
27
23
28
// This program should tell the user the last digit of each number.
24
29
// Explain why getLastDigit is not working properly - correct the problem
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
19
21
// to help you answer these questions
20
22
21
23
// Questions
22
24
23
25
// a) When formatTimeDisplay is called how many times will pad be called?
24
-
// =============> write your answer here
26
+
// =============> three times in terms of hours, minutes and seconds
25
27
26
28
// Call formatTimeDisplay with an input of 61, now answer the following:
27
29
28
30
// b) What is the value assigned to num when pad is called for the first time?
29
-
// =============> write your answer here
31
+
// =============> 0
30
32
31
33
// c) What is the return value of pad is called for the first time?
32
-
// =============> write your answer here
34
+
// =============> 00
33
35
34
36
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35
-
// =============> write your answer here
37
+
// =============> the value assigned to num will be 1, and because the remaining seconds after 61 % 60 will be 1.
36
38
37
39
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38
-
// =============> write your answer here
40
+
// =============> the return value of pad when it is called for the last time is "01". Because inside pad(), 1 is less than 2, and will be padded to "0" and will become "01".
0 commit comments