Skip to content

Commit b8b3278

Browse files
committed
complete coursework fixing error and debug
1 parent 3372770 commit b8b3278

File tree

11 files changed

+134
-36
lines changed

11 files changed

+134
-36
lines changed

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

Lines changed: 7 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+
// =============> my prediction is that this code is not functioning because of that it declared parameter in the function.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,9 @@ function capitalise(str) {
99
return str;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
// =============> write your explanation here : the error message is indicating that str already declared in the parameter and we can't redeclare it.
13+
/* =============> write your new code here
14+
function capitalise(str) {
15+
const capitalizeFirstLetter =`${str[0].toUpperCase()}${str.slice(1)}`;
16+
return capitalizeFirstLetter;
17+
} */

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> write your prediction here : /* my prediction is that parameter was declared inside the function
5+
// and also local variable can not be logged globally. */
56

67
// Try playing computer with the example to work out what is going on
78

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

1516
console.log(decimalNumber);
1617

17-
// =============> write your explanation here
18+
// =============> write your explanation here : /* in this code the computer first read the function and
19+
// and store the variables and try to execute the function in case of the
20+
// above code the computer first take the parameter and do the next step
21+
// it reads the const decimalNUmber that is error so it will execute
22+
// identifier error. */
1823

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

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11

22
// Predict and explain first BEFORE you run any code...
3-
3+
// the function parameter is assigned to constant number the result will be variable error.
44
// this function should square any number but instead we're going to get an error
55

6-
// =============> write your prediction of the error here
6+
// =============> write your prediction of the error here : variable error.
77

88
function square(3) {
99
return num * num;
1010
}
1111

1212
// =============> write the error message here
13+
//syntaxError: Unexpected number
1314

1415
// =============> explain this error message here
16+
// the error message is explaining that the parameter is assigned to constant number rather than variable.
1517

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

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

2024

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

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

3-
// =============> write your prediction here
3+
// =============> write your prediction here : this code didn't declare the two global variables and the result will be undefined.
44

55
function multiply(a, b) {
66
console.log(a * b);
77
}
88

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

11-
// =============> write your explanation here
12-
11+
// =============> write your explanation here : the code have two number variables but they are not declared global in this case the
12+
// global console.log have undefined value
1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// =============> write your new code here let a = 10;
15+
// let b = 32;
16+
// function multiply(a, b) {
17+
// (a * b);
18+
// }
19+
20+
// console.log(`The result of multiplying 10 and 32 is ${(a * b)}`);

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

Lines changed: 10 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 two number variables are not declared and return is not defined.
33

44
function sum(a, b) {
55
return;
@@ -8,6 +8,13 @@ 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: two number values 10 and 32 are not defined global and the return is not assigned to any
12+
// return function.
1213
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
14+
// =============> write your new code here : let a = 10;
15+
// let b = 32;
16+
// function sum(a, b) {
17+
// a + b;
18+
// }
19+
20+
// console.log(`The sum of 10 and 32 is ${a + b}`);

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

Lines changed: 16 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: my prediction is that the code will log value of 3 because it have constant variable.
55

66
const num = 103;
77

@@ -14,11 +14,23 @@ 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 : all the console.log values are 3.
18+
1819
// Explain why the output is the way it is
19-
// =============> write your explanation here
20+
// =============> write your explanation here:it is because the global variable is constant number 103.
21+
2022
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
23+
// =============> write your new code here : let firstNum = 42;
24+
// let secondeNum = 105;
25+
// let thirdNum = 806;
26+
27+
// function getLastDigit(num) {
28+
// return num.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)}`);
2234

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

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
// Given someone's weight in kg and height in metres
1414
// Then when we call this function with the weight and height
1515
// It should return their Body Mass Index to 1 decimal place
16-
16+
let weight = 70;
17+
let height = 1.73;
1718
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
19+
// return the BMI of someone based off their weight and height
20+
const squaringHeight = height * height;
21+
const resultBMI = weight / squaringHeight;
22+
return resultBMI.toFixed(1);
23+
}
24+
console.log(calculateBMI(weight, height));

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

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

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

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

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ function formatTimeDisplay(seconds) {
1010

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

1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// =============> write your answer here : 3 times
2121

2222
// Call formatTimeDisplay with an input of 61, now answer the following:
2323

2424
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
25+
// =============> write your answer here :0
2626

2727
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
28+
// =============> write your answer here:00
2929

3030
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
31+
// =============> write your answer here: pad(00):pad(01):pad(01) because it will take the value of totalHours,remainingMinutes,remainingSeconds.
3232

3333
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
34+
// =============> write your answer here: the return value will be 00:01:01 because it is the final argument passed to pad() in the return statement.

0 commit comments

Comments
 (0)