Skip to content

Commit a2491cc

Browse files
committed
Complete JavaScript practice exercises
1 parent ed78bdd commit a2491cc

6 files changed

Lines changed: 72 additions & 18 deletions

File tree

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

Lines changed: 9 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+
// The issue is that return is on one line and a + b is on the next.
3+
// Therefore, a + b is never returned.
34

45
function sum(a, b) {
56
return;
@@ -8,6 +9,11 @@ function sum(a, b) {
89

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

11-
// =============> write your explanation here
12+
// After running the code, the sum of 10 and 32 is undefined.
13+
// JavaScript inserts a semicolon after return when a + b is placed on the next line.
14+
// This causes the function to return before calculating the sum.
15+
1216
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
17+
// function sum(a, b) {
18+
// return a + b;
19+
// }

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

Lines changed: 18 additions & 5 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+
// I believe that in each case, the output will be the last digit of 103, which is 3.
55

66
const num = 103;
77

@@ -14,11 +14,24 @@ 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+
// The last digit of 42 is 3
18+
// The last digit of 105 is 3
19+
// The last digit of 806 is 3
20+
1821
// Explain why the output is the way it is
19-
// =============> write your explanation here
22+
// The function getLastDigit() does not accept a parameter.
23+
// Consequently, it always uses the global variable num, whose value is 103.
24+
2025
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
26+
// const num = 103;
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.
24-
// Explain why getLastDigit is not working properly - correct the problem
36+
// getLastDigit() is not working properly because it always uses the global num variable (103).
37+
// I added num as a parameter so that the function can accept the value passed as an argument.

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+
let bmi = weight / (height * height);
19+
return bmi.toFixed(1);
20+
}
21+
22+
console.log(calculateBMI(70, 1.73));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@
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 toUpperSnakeCase(input) {
19+
return input.toUpperCase().replaceAll(" ", "_");
20+
}
21+
22+
console.log(toUpperSnakeCase("hello there"));
23+
console.log(toUpperSnakeCase("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+
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+
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+
25+
return ${pounds}.${pence}`;
26+
}
27+
28+
console.log(toPounds("399p"));
29+
console.log(toPounds("5p"));
30+
console.log(toPounds("1200p"));

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) {
2121
// Questions
2222

2323
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
24+
// pad() is called 3 times because it is used once for hours, once for minutes, and once for seconds.
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+
// Because the first call to pad() is pad(totalHours), and formatTimeDisplay(61) calculates totalHours as 0, the value passed to pad() as num is 0.
3030

31-
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
31+
// c) What is the return value of pad when called for the first time?
32+
// When pad() is called for the first time, num is 0 because totalHours is 0.
33+
// The function converts it to a string and adds a leading zero, so the return value is "00".
3334

34-
// 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+
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
36+
// Because the last call to pad() is pad(remainingSeconds) and formatTimeDisplay(61) calculates remainingSeconds as 1 (61 % 60 = 1), the value passed into pad() as num is 1.
3637

37-
// 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+
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
39+
// The return value of pad() when it is called for the last time is "01" because num is 1, and the function pads numbers by adding a leading zero.

0 commit comments

Comments
 (0)