Skip to content

Commit e4c7014

Browse files
committed
Sprint2
1 parent 8f3d6cf commit e4c7014

File tree

10 files changed

+110
-33
lines changed

10 files changed

+110
-33
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> my explanation: I see here declaration of str variable, which already exist as an argument. It will cause an error.
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
6-
6+
//===============>SyntaxError: Identifier 'str' has already been declared, its occured while execution of code.
7+
/*
78
function capitalise(str) {
89
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
910
return str;
1011
}
11-
12-
// =============> write your explanation here
12+
*/
13+
// =============> write your explanation here. My explanation: We have to change the name of variable and return it in order to fix an error.
1314
// =============> write your new code here
15+
function capitalise(str) {
16+
let strChanged = `${str[0].toUpperCase()}${str.slice(1)}`;
17+
return strChanged;
18+
}
19+
console.log(capitalise("some string"));

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
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: Value decimalNumber is a parameter to the convertToPercentage. We don't need to declare it at line 10.
5+
// =============> Also at line 16, we have to call function coverToPercentage with an argument.
56

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

8-
function convertToPercentage(decimalNumber) {
9+
/*function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
1314
}
1415
1516
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
17+
*/
18+
// =============> write your explanation here. My explanation: We need to modify this function. I'm going to remove line where decimalNumber is declared and execute the function properly.
1819

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

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33

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
7-
6+
// =============> write your prediction of the error here My prediction: There is an error with parameter.
7+
/*
88
function square(3) {
99
return num * num;
1010
}
11+
console.log(square)
12+
*/
13+
// =============> write the error message here: SyntaxError: Unexpected number
1114

12-
// =============> write the error message here
13-
14-
// =============> explain this error message here
15+
// =============> explain this error message here: An error because of invalid parameter.
1516

1617
// Finally, correct the code to fix the problem
1718

1819
// =============> write your new code here
20+
function square(num) {
21+
return num * num;
22+
}
23+
console.log(square(3))
1924

2025

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

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

3-
// =============> write your prediction here
4-
3+
// =============> write your prediction here: This function return nothing.
4+
/*
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)}`);
10-
11-
// =============> write your explanation here
10+
*/
11+
// =============> write your explanation here: We need to modify this function.
12+
// =============> Create a new variable where we have to do the following calculation, after that return this new variable.
1213

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

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
3-
2+
// =============> write your prediction here: wrong calculation and return nothing.
3+
/*
44
function sum(a, b) {
55
return;
66
a + b;
77
}
88
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
11-
// =============> write your explanation here
10+
*/
11+
// =============> write your explanation here: We need to create a variable where we have to store our calculation and return it.
1212
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
13+
// =============> write your new code
14+
function sum(a, b) {
15+
resultOfAdding = a + b;
16+
return resultOfAdding
17+
}
18+
19+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
5-
4+
// =============> Write your prediction here: Function getLastDigit return num.
5+
/*
66
const num = 103;
77
88
function getLastDigit() {
99
return num.toString().slice(-1);
10+
1011
}
1112
1213
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1314
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1415
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
16+
*/
1617
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18+
// =============> write the output here: output is last digit of variable num.
1819
// Explain why the output is the way it is
19-
// =============> write your explanation here
20+
// =============> write your explanation here: Because num is global and function's parameter is absent .
2021
// Finally, correct the code to fix the problem
2122
// =============> write your new code here
23+
const num = 103;
24+
25+
function getLastDigit(num) {
26+
let digit = num.toString().slice(-1);
27+
return digit
28+
}
29+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
30+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
31+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2232

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
resultBmi = weight/Math.pow(height,2);
20+
return resultBmi.toFixed(1);
21+
}
22+
console.log(calculateBMI(70, 1.73));

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(text){
19+
let textUpper = text.toUpperCase();
20+
return textUpper.replace(" ", "_");
21+
}
22+
console.log(upperSnakeCase("hello here"));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
/*
12
// In Sprint-1, there is a program written in interpret/to-pounds.js
23
34
// You will need to take this code and turn it into a reusable block of code.
45
// You will need to declare a function called toPounds with an appropriately named parameter.
56
67
// You should call this function a number of times to check it works for different inputs
8+
*/
9+
10+
function toPounds(penceString){
11+
let penceStringWithoutTrailingP;
12+
if (penceString.charAt(penceString.length - 1) == "p"){
13+
penceStringWithoutTrailingP = penceString.substring( 0, penceString.length - 1);
14+
} else {
15+
penceStringWithoutTrailingP = penceString;
16+
}
17+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
18+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
19+
20+
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
21+
22+
console.log(${pounds}.${pence}`);
23+
}
24+
toPounds("200");
25+
toPounds("200p");
26+
toPounds("20");
27+
toPounds("2");

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) {
1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// =============> // three 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+
// =============> 61%60=1/61-1/60=1/60%60=0, the value assigned to num when pad called for the first time is 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: remainingSeconds 61%60=1.
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: "01" because of .padStart(2, "0")
35+
console.log(pad(0));
36+
console.log(pad(1));
37+
console.log(pad(123));
38+
console.log(formatTimeDisplay(61));
39+

0 commit comments

Comments
 (0)