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
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/0.js
+8Lines changed: 8 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
// Predict and explain first...
2
2
// =============> write your prediction here
3
+
// I predict that the code will throw an error because the variable `str` is being declared twice within the same scope, the first in the function perimeter.
4
+
// and the second declaration is inside the function body.
3
5
4
6
// call the function capitalise with a string input
5
7
// interpret the error message and figure out why an error is occurring
@@ -10,4 +12,10 @@ function capitalise(str) {
10
12
}
11
13
12
14
// =============> write your explanation here
15
+
// an error will occur because the variable`str` is being declared in the same scope,which is not allowed in javascript.
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+19Lines changed: 19 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,9 @@
2
2
3
3
// Why will an error occur when this program runs?
4
4
// =============> write your prediction here
5
+
// I predict that the code will throw an error because the variable `decimalNumber` being declared twice within the same scope,in the function perimeter.
6
+
// and the second declaration is inside the function body.
7
+
5
8
6
9
// Try playing computer with the example to work out what is going on
7
10
@@ -15,6 +18,22 @@ function convertToPercentage(decimalNumber) {
15
18
console.log(decimalNumber);
16
19
17
20
// =============> write your explanation here
21
+
// in javascript its not allowed to declare variables twice in the same scope because it creates an error such as `decimalnumber`.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/1.js
+16-1Lines changed: 16 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
// Predict and explain first...
2
2
// =============> write your prediction here
3
+
// I predict that there will be ann error because the return variable does not have values recieved by sum function .there are values that has not variable
3
4
4
5
functionsum(a,b){
5
6
return;
@@ -9,5 +10,19 @@ function sum(a, b) {
9
10
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
11
11
12
// =============> write your explanation here
12
-
// Finally, correct the code to fix the problem
13
+
14
+
// Finally, Correct the code to fix the problem. The function receives the values 10 and 32 as parameters a and b.
15
+
// However, because return is followed by a new line, JavaScript treats it as:
16
+
17
+
// This causes the function to return undefined immediately.The expression a + b is never execute.
18
+
// As a result , the template literal prints:
19
+
"The sum of 10 and 32 is undefined".
20
+
13
21
// =============> write your new code here
22
+
23
+
functionsum(a,b){
24
+
returna+b;
25
+
26
+
}
27
+
28
+
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+36Lines changed: 36 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,14 @@
1
1
// Predict and explain first...
2
+
// I predict that all 3 lines will print as last digit of the function does not use the valuable to passed into.
3
+
// instead , it always uses the global variable which is 103.
2
4
3
5
// Predict the output of the following code:
4
6
// =============> Write your prediction here
7
+
// The function getLastDigit() does not have a parameter , so it cannot use the numbers passed to it when it is called.
8
+
// when getLastDigit (42), getLastDigit (105),and getLastDigit(806)are executed ,the arguments are ignored because thr function the function does not accept
9
+
// any parameters instead,the function always uses the global variable num, which is 103.
10
+
// 103 converted to a string is "103",and slice(-1) returns the last character ,which is "3".
11
+
// therefore, the function always returns 3 regardless of the number passed in.
5
12
6
13
constnum=103;
7
14
@@ -15,10 +22,39 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
22
16
23
// Now run the code and compare the output to your prediction
17
24
// =============> write the output here
25
+
26
+
// The resource https://chatgpt.com/cdn/assets/root-lmbi4tq1.css# was preloaded using link preload but not used within a few seconds from the window's load event.
27
+
// Please make sure it has an appropriate `as` value and it is preloaded intentionally.
28
+
(index):1
29
+
18
30
// Explain why the output is the way it is
19
31
// =============> write your explanation here
32
+
33
+
// his message is a browser warning, not a JavaScript error.
34
+
// The browser downloaded (preloaded) the CSS file to make the page load faster,
35
+
// but the stylesheet was not used immediately after the page loaded.
36
+
// Because the preloaded resource was not used within a few seconds,
37
+
// Chrome displays a warning to indicate that the preload may be unnecessary
38
+
// or that the resource might have the wrong "as" attribute.
39
+
// The page can still work correctly even though this warning appears.
40
+
20
41
// Finally, correct the code to fix the problem
21
42
// =============> write your new code here
22
43
44
+
functiongetLastDigit(num){
45
+
returnnum.toString().slice(-1);
46
+
}
47
+
48
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
49
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
50
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
51
+
52
+
23
53
// This program should tell the user the last digit of each number.
24
54
// Explain why getLastDigit is not working properly - correct the problem
55
+
// The original version of getLastDigit was not working properly because it did not accept a parameter.
56
+
// It always used a global variable instead of the number passed into the function.
57
+
// by adding the parameter num,the function now recieves the number that is passed in when it is called.The function converts that number to a string
58
+
// and uses slice (-1) to return the last digit.as a result ,getLastDigit(105) returns 5,and getLastDigital(806) returns 6.
0 commit comments