Skip to content

Commit e3541bd

Browse files
committed
complete 1-count exercise
1 parent 8f3d6cf commit e3541bd

File tree

10 files changed

+141
-35
lines changed

10 files changed

+141
-35
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// It will give an error because the variable `str`, is declared twice inside the same function,
4+
// It is already a parameter, and then it is redeclared using `let`.
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
68

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
9+
//function capitalise(str) {
10+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
// return str;
12+
// }
1113

1214
// =============> write your explanation here
15+
// The error happens because 'str' is written two times, first, it is the function parameter.
16+
// Then, it is declared again using 'let', javaScript does not allow declaring the same variable twice, inside the same function.
1317
// =============> write your new code here
18+
19+
function capitalise(str) {
20+
return `${str[0].toUpperCase()}${str.slice(1)}`;
21+
}
22+
console.log(capitalise("Code"));

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
// Predict and explain first...
2+
// The code will give an error because `decimalNumber` is declared twice,
3+
// once as the function parameter, and again with `const` inside the function.
24

35
// Why will an error occur when this program runs?
46
// =============> write your prediction here
5-
7+
// The error occurs because `decimalNumber` has already been declared.
68
// Try playing computer with the example to work out what is going on
79

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

15-
console.log(decimalNumber);
14+
// return percentage;
15+
// }
16+
// console.log(decimalNumber);
1617

1718
// =============> write your explanation here
18-
19+
// When I run the code, it gives a SyntaxError saying that, `decimalNumber` has already been declared.
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+
console.log(convertToPercentage(0.5));

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11

22
// Predict and explain first BEFORE you run any code...
3+
// Function parameters must be variables, not numbers.
34

45
// this function should square any number but instead we're going to get an error
56

67
// =============> write your prediction of the error here
8+
// The code will give a SyntaxError because `3` is not a valid parameter name.
79

8-
function square(3) {
9-
return num * num;
10-
}
10+
//function square(3) {
11+
// return num * num;
12+
// }
1113

1214
// =============> write the error message here
15+
// SyntaxError: Unexpected number
1316

1417
// =============> explain this error message here
15-
18+
// The error happens because `3` is used as a parameter name.
19+
// Parameters must be variable names, not numbers.
1620
// Finally, correct the code to fix the problem
1721

1822
// =============> write your new code here
1923

24+
function square(number) {
25+
return number * number;
26+
}
27+
console.log(square(3));
28+
2029

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

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

33
// =============> write your prediction here
4+
// When multiply(10, 32) runs, it prints 320, But the function does not return anything.
45

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
86

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

1113
// =============> write your explanation here
14+
// multiply prints the result but does not return anything, so using it in a template string shows undefined.
1215

1316
// Finally, correct the code to fix the problem
1417
// =============> write your new code here
18+
function multiply(a, b) {
19+
return (a * b)
20+
}
21+
22+
console.log(multiply(10, 32));

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The function sum does not return anything,
4+
// so the console will show "The sum of 10 and 32 is undefined".
35

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
6+
// function sum(a, b) {
7+
// return
8+
// a + b;
9+
// }
810

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1012

1113
// =============> write your explanation here
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
17+
function sum(a, b) {
18+
return (a+b);
19+
}
20+
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The output will always be 3.This is because the function uses the variable `num`,
6+
// which is set to 103.
7+
// const num = 103;
58

6-
const num = 103;
9+
// function getLastDigit() {
10+
// return num.toString().slice(-1);
11+
// }
712

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
11-
12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
13+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
27+
// The output is always 3 because the function uses `num` (103),and ignores the number passed into it.
2028
// Finally, correct the code to fix the problem
29+
2130
// =============> write your new code here
2231

32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
39+
40+
41+
2342
// This program should tell the user the last digit of each number.
2443
// Explain why getLastDigit is not working properly - correct the problem
44+
// We must pass the number into the function as a parameter.

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

Lines changed: 6 additions & 1 deletion
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+
const bmi = weight / (height * height);
19+
const roundedBMI = Math.round(bmi * 10) / 10;
20+
return roundedBMI;
21+
22+
}
23+
console.log(calculateBMI(70, 1.73));
1824
// return the BMI of someone based off their weight and height
19-
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
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+
19+
function toUpperSnakeCase(inputString) {
20+
return inputString.replaceAll(" ", "_").toUpperCase();
21+
22+
23+
}
24+
console.log(toUpperSnakeCase("hello there"));
25+
console.log (toUpperSnakeCase("lord of the rings"));

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,29 @@
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("400p"));
30+
console.log(toPounds("243p"));
31+
console.log(toPounds("9876p"));
32+
console.log(toPounds("18p"));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ function formatTimeDisplay(seconds) {
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
21+
// It is called 3 times, once for hours, once for minutes, and once for seconds.
2122

2223
// Call formatTimeDisplay with an input of 61, now answer the following:
2324

2425
// b) What is the value assigned to num when pad is called for the first time?
2526
// =============> write your answer here
27+
// The first argument passed to pad is totalHours, which is 0.
2628

2729
// c) What is the return value of pad is called for the first time?
2830
// =============> write your answer here
31+
// The first call to pad returns "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
3134
// =============> write your answer here
35+
// The last call to pad receives num = 1, because remainingSeconds = 61 % 60 = 1.
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
3438
// =============> write your answer here
39+
// The last call to pad returns "01". The number 1 is converted to a string and padded to two characters with a leading 0.

0 commit comments

Comments
 (0)