Skip to content

Commit d7a7034

Browse files
committed
I worked on interpreting code,fixing error, implementing of code, debugging of code
1 parent b31a586 commit d7a7034

11 files changed

Lines changed: 118 additions & 37 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// My prediction is that the function will throw an error because the variable which also the parameter is redeclared again inside the function.
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

@@ -9,5 +9,10 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
// =============> write your explanation here: to explain, The parameter str already exists in that function's scope,
13+
// so trying to `let str = ...` again is redeclaring the same name in the same scope, and JS won't allow it.
14+
// =============> write your new code here: function capitalise(str) {
15+
// let capitaliseFristLatter = `${str[0].toUpperCase()}${str.slice(1)}`;
16+
17+
// return capitaliseFristLatter;
18+
// }

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5-
5+
// My prediction is that the variable `decimalNumber` is already declared and also the console.log can only log the variable decimalNumber.
66
// Try playing computer with the example to work out what is going on
77

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

1515
console.log(decimalNumber);
1616

17-
// =============> write your explanation here
17+
// =============> write your explanation here: the variable was already declared in the parameter scope and it won't redeclare inside the function again
18+
// and also the console.log was only working for the logging of the variable `decimalNumber` not the function.
1819

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
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+
// =============> write your prediction of the error here: it will throw an error because in the parameter(placeholder)
6+
// there is constant value which js does not recognize.
77

88
function square(3) {
9-
return num * num;
9+
return num * num;
1010
}
1111

12-
// =============> write the error message here
12+
// =============> write the error message here: the error message SyntaxError: Unexpected number
1313

14-
// =============> explain this error message here
14+
// =============> explain this error message here: syntax error, meaning it fails before the function even gets defined.
15+
// JS expects a parameter slot to contain a name (identifier) and gets confused
16+
// when it sees a raw number sitting there instead.
1517

1618
// Finally, correct the code to fix the problem
1719

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

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

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

3-
// =============> write your prediction here
3+
// =============> write your prediction here: My prediction is that in this function there is two console.log
4+
// the result of this two log will print one with the value only 320 and
5+
// the other one with the sentences of the log not the value.
46

57
function multiply(a, b) {
68
console.log(a * b);
79
}
810

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

11-
// =============> write your explanation here
13+
// =============> write your explanation here: because of the log inside the function the outside function can't access the result so the log will print
14+
// one with the value only and second console.log will print the sentences with explanation of the result and
15+
// undefined value because the the function doesn't access the operation.
1216

1317
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
18+
// =============> write your new code here: function multiply(a, b) {
19+
// return a * b;
20+
// }
21+
22+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> write your prediction here: the function have nothing to return so it will log the sentences with undefined value.
33

44
function sum(a, b) {
55
return;
@@ -8,6 +8,11 @@ function sum(a, b) {
88

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

11-
// =============> write your explanation here
11+
// =============> write your explanation here: firstly the process start accepting that it is a function and takes
12+
// the parameter to hold the place then it go through the function but by the 5th line
13+
// the function return and the value will come out undefined
1214
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
15+
// =============> write your new code here: function sum(a, b) {
16+
// return a + b;
17+
//
18+
// }

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> Write your prediction here: the code is already declared a variable that can not be changed so the consol.log always be the same 3.
55

66
const num = 103;
77

@@ -14,11 +14,15 @@ 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+
// =============> write the output here: 3
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// =============> write your explanation here: the code is already declared a variable that can not be changed in the
20+
// first place so when ever the process try to access another console.log it will take the same value as before
21+
// so the consol.log always be the same.
2022
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
23+
// =============> write your new code here: function getLastDigit(num) {
24+
// return num.toString().slice(-1);
25+
// }
2226

2327
// This program should tell the user the last digit of each number.
2428
// Explain why getLastDigit is not working properly - correct the problem

Sprint-2/3-mandatory-implement/1-bmi.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
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 squaredHeight = height * height;
19+
const originalBMI = (weight / squaredHeight).toString();
20+
let dotIndex = originalBMI.indexOf(".");
21+
let bmiWithOneDecimal = originalBMI.slice(0, dotIndex + 2);
22+
return bmiWithOneDecimal;
23+
}
24+
console.log(`The calculated BMI is ${calculateBMI(78.4, 1.71)}`);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
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+
18+
function toCapitalizedSnakeCase(str) {
19+
const toCapitalized = str.toUpperCase().replace(/ /g, "_");
20+
return toCapitalized;
21+
}

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

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

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@ function formatTimeDisplay(seconds) {
1414

1515
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
17-
17+
console.log(formatTimeDisplay(61));
1818
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1919
// to help you answer these questions
2020

2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// =============> write your answer here: 3 times.
2525

2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
29+
// =============> write your answer here:it is 0, because totalHours is 0.
3030

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
32+
// =============> write your answer here:it is 00, because the function pad will add another 0 if it is less than 2 index.
3333

3434
// 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
35+
// =============> write your answer here:it is 1, because the remainingSeconds value will become 1 after it get the remainder of the seconds.
3636

3737
// 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
38+
// =============> write your answer here:it will be 01,because after going through the function pad it will gain 0 from the front because it is less
39+
// than 2 index so the return will be 01.

0 commit comments

Comments
 (0)