Skip to content

Commit eb5ff4a

Browse files
debugged and fixed buggy codes.
1 parent d249dd2 commit eb5ff4a

11 files changed

Lines changed: 156 additions & 40 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> I presume the str is repeatedly declared, first as a parameter and again as a variable.
3+
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
@@ -9,5 +10,12 @@ function capitalise(str) {
910
return str;
1011
}
1112

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.
14+
/* ==========> function capitalise(str) {
15+
let result = `${str[0].toUpperCase()}${str.slice(1)};
16+
return result;
17+
}
18+
OR
19+
function capitalise(str) {
20+
return `${str[0].toUpperCase()}${str.slice(1)}`;
21+
}

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

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

33
// 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.
55

66
// Try playing computer with the example to work out what is going on
77

@@ -14,7 +14,15 @@ function convertToPercentage(decimalNumber) {
1414

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
17+
// =============> it says decimalNumber has aleady been declared for the above reason.
1818

1919
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
20+
/* ===========> function convertToPercentage(decimalNumber) {
21+
let decimalNum = 0.5;
22+
const percentage = `${decimalNum * 100}%`;
23+
24+
return percentage;
25+
}
26+
const result = convertToPercentage();
27+
console.log(result);
28+
*/

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

43
// this function should square any number but instead we're going to get an error
54

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.
76

87
function square(3) {
98
return num * num;
109
}
1110

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.
1312

14-
// =============> explain this error message here
13+
// =============> The error is already described above.
1514

1615
// Finally, correct the code to fix the problem
1716

18-
// =============> write your new code here
19-
20-
17+
/* ===========> function square(num) {
18+
return num * num;
19+
}
20+
*/

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ function multiply(a, b) {
88

99
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

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.
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
/* =============> function multiply(a, b) {
15+
return a * b;
16+
}
17+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> As the function is not retuning anything, we're going to see undefined on the console.
33

44
function sum(a, b) {
5-
return;
6-
a + b;
5+
return a + b;
76
}
87

98
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
109

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.
1211
// 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)}`);
16+
*/

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> the function is going to return error as the /1 is not valid expression or syntax
55

66
const num = 103;
77

8-
function getLastDigit() {
8+
function getLastDigit(num) {
99
return num.toString().slice(-1);
1010
}
1111

@@ -14,11 +14,16 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// 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+
*/
1820
// 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.
2022
// 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+
*/
2227

2328
// This program should tell the user the last digit of each number.
2429
// 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
@@ -15,5 +15,8 @@
1515
// It should return their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
18+
const bmi = weight / (height * height);
19+
return Math.round(bmi * 10) / 10;
20+
}
21+
22+
console.log(`The BMI of Yonatan is ${calculateBMI(65, 1.65)}`);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
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 snakeCaseString(str) {
18+
return str.split(" ").join("_").toUpperCase();
19+
}
20+
console.log(snakeCaseString("lord of the rings"));

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,27 @@
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+
const penceString = "399p";
8+
9+
function toPounds(penceString) {
10+
const penceStringWithoutTrailingP = penceString.substring(
11+
0,
12+
penceString.length - 1
13+
);
14+
15+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
16+
const pounds = paddedPenceNumberString.substring(
17+
0,
18+
paddedPenceNumberString.length - 2
19+
);
20+
21+
const pence = paddedPenceNumberString
22+
.substring(paddedPenceNumberString.length - 2)
23+
.padEnd(2, "0");
24+
return ${pounds}.${pence}`;
25+
}
26+
27+
console.log(toPounds("39p"));
28+
console.log(toPounds("100p"));
29+
console.log(toPounds("4000p"));
30+
console.log(toPounds("9p"));

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@ function pad(num) {
55
}
66
return numString;
77
}
8+
console.log(pad());
89

910
function formatTimeDisplay(seconds) {
1011
const remainingSeconds = seconds % 60;
1112
const totalMinutes = (seconds - remainingSeconds) / 60;
1213
const remainingMinutes = totalMinutes % 60;
1314
const totalHours = (totalMinutes - remainingMinutes) / 60;
14-
1515
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
17+
console.log(formatTimeDisplay(61));
1718

19+
//
1820
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1921
// to help you answer these questions
2022

2123
// Questions
2224

2325
// 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
2527

2628
// Call formatTimeDisplay with an input of 61, now answer the following:
2729

2830
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
31+
// =============> 0
3032

3133
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
34+
// =============> 00
3335

3436
// 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.
3638

3739
// 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

Comments
 (0)