Skip to content

Commit 415c69e

Browse files
committed
forma-time
1 parent 617ad94 commit 415c69e

10 files changed

Lines changed: 109 additions & 49 deletions

File tree

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// =============> An error will occur because console.log(decimalNumber) tries to access a variable that does not exist in the global scope.
55

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

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
11-
12-
return percentage;
13-
}
148

15-
console.log(decimalNumber);
169

17-
// =============> write your explanation here
10+
// =============> decimalNumber is already a parameter, and JavaScript does not allow redeclaring a parameter woth const.
1811

1912
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
13+
// =============>
14+
function convertToPercentage(decimalNumber) {
15+
const percentage = `${decimalNumber * 100}%`;
16+
return percentage;
17+
}
18+
19+
console.log(convertToPercentage(0.5));

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
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
6+
// =============> SyntaxError
77

8-
function square(3) {
9-
return num * num;
10-
}
118

12-
// =============> write the error message here
9+
// =============> Unexpected number
1310

14-
// =============> explain this error message here
11+
// =============> function parameters must be variable not values.
1512

1613
// Finally, correct the code to fix the problem
1714

18-
// =============> write your new code here
15+
// =============>
16+
function square(num) {
17+
return num * num;
18+
}
19+
console.log(square(3));
1920

2021

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

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

3-
// =============> write your prediction here
3+
// =============> SyntaxError undefined function
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
8-
9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
105

11-
// =============> write your explanation here
6+
// =============> We use console.log() only when we want to print something immediately. In this case we need to use return.
127

138
// Finally, correct the code to fix the problem
149
// =============> write your new code here
10+
function multiply(a, b) {
11+
return a * b;
12+
}
13+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> the sum is undefined
33

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
8-
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
104

11-
// =============> write your explanation here
5+
// =============> This is due to Automatic semicolon Insertion
126
// Finally, correct the code to fix the problem
137
// =============> write your new code here
8+
function sum(a, b) {
9+
return a + b;
10+
}
11+
12+
console.log(`The sum of 10 + 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> The output will always be 3 for all cases.
55

6+
7+
// Now run the code and compare the output to your prediction
8+
// =============> write the output here
9+
// The last digit of 42 is 3
10+
//The last digit of 105 is 3
11+
//The last digit of 806 is 3
12+
// Explain why the output is the way it is
13+
// =============> write your explanation here
14+
//The function does not have a parameter.
15+
//It does not use the values 42, 105, or 806.
16+
//It always uses the global variable:
17+
// Finally, correct the code to fix the problem
18+
// =============> write your new code here
619
const num = 103;
720

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
21+
function getLastDigit(value) {
22+
return value.toString().slice(-1);
1023
}
1124

1225
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1326
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1427
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1528

16-
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18-
// Explain why the output is the way it is
19-
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
2229

2330
// This program should tell the user the last digit of each number.
2431
// 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 squareHeight = height * height;
19+
const bmi = weight / squareHeight;
20+
return Number(bmi.toFixed(1));
21+
}
22+
console.log(calculateBMI(60, 1.70));

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+
function toUpperSnakeCase(str) {
18+
return str.toUpperCase().replaceAll(" ", "_");
19+
}
20+
21+
console.log(toUpperSnakeCase("Hello there")); // "HELLO_THERE"
22+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,45 @@
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+
// ===>Code
8+
// const penceString = "399p";
9+
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+
25+
// console.log(`£${pounds}.${pence}`);
26+
27+
28+
29+
30+
// ===>Reusable Code
31+
32+
33+
function toPounds(str){
34+
35+
const onlyNumbers = str.replace(/[^\d]/g, ''); // removes everything that is not a digit
36+
const decimals = (onlyNumbers/100).toFixed(2); // converts pence to pounds
37+
38+
return ${decimals}`;
39+
};
40+
41+
42+
43+
// ===> test
44+
45+
const examples = ["80p", "Fay", "130p", "£3.50", "10000"];
46+
for (const example of examples) {
47+
console.log(toPounds(example));
48+
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ 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+
// =============> pad function will be called 3 times when formatTimeDisplay is called.
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+
// =============> The value assigned to num when pad is called for the first time is 0.
26+
2627

2728
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
29+
/* =============> The return value of pad when it is called for the first time is "00".
30+
because num is 0, and when we convert it to a string and pad it to 2
31+
characters with leading zeros, it becomes "00".*/
2932

3033
// 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
34+
// =============> The value assigned to num when pad is called for the last time in this program is 1.
35+
3236

3337
// 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
38+
// =============>The return value assigned to num when pad is called for the last time in this program is "01".

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ const targetOutput2 = "11:00 pm";
2222
console.assert(
2323
currentOutput2 === targetOutput2,
2424
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);
25+
);

0 commit comments

Comments
 (0)