Skip to content

Commit 2dc31c3

Browse files
committed
I have dome sprint2 for structuring data module
1 parent 3372770 commit 2dc31c3

File tree

12 files changed

+179
-25
lines changed

12 files changed

+179
-25
lines changed

Sprint-1/1-key-exercises/1-count.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
66
// Describe what line 3 is doing, in particular focus on what = is doing
7+
// updates the value of the variable count by adding 1

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// Missing backticks for template string template strings need to use backticks `, but here ${} is used without them
33

4-
// call the function capitalise with a string input
4+
// call the function capitalise with a string input (error)
55
// interpret the error message and figure out why an error is occurring
6+
// ${} must be inside backticks and str is being redeclared (it is already the function parameter)
67

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

129
// =============> write your explanation here
13-
// =============> write your new code here
10+
// =============> This function takes a string and returns a new string with the first letter in capital
11+
function capitalise(str) {
12+
return `${str[0].toUpperCase()}${str.slice(1)}`;
13+
}

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

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

3-
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
3+
// decimalNumber is already declared as a function parameter. and name variable the same
54

65
// Try playing computer with the example to work out what is going on
76

87
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
108
const percentage = `${decimalNumber * 100}%`;
11-
129
return percentage;
1310
}
1411

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
12+
console.log(convertToPercentage(0.5));
1813

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
14+
// creates a function named convertToPercentage
15+
// The function returns the percentage string.SS

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
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+
// Error because the variable 'num' is not defined when we try to call the function.
7+
// The function expects
8+
// a parameter but we're not passing any argument when calling it.
79

8-
function square(3) {
10+
function square(num) {
911
return num * num;
1012
}
1113

12-
// =============> write the error message here
14+
// The function will return NaN  (Not a Number)
15+
// this doesn't throw an error, but returns NaN  because:
16+
// - num is undefined (no argument passed)
17+
// - undefined * undefined = NaN 
1318

14-
// =============> explain this error message here
19+
// When square() is called without an argument, the parameter 'num' has
20+
// // the value 'undefined'. When you multiply undefined * undefined,
21+
// // JavaScript returns NaN (Not a Number) instead of throwing an error. 
1522

16-
// Finally, correct the code to fix the problem
17-
18-
// =============> write your new code here
23+
function square(num)
24+
{
25+
return num * num;
26+
}
27+
console.log(square(5)); // Output: 25 console.log(square(10)); 
1928

2029

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

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

3-
// =============> write your prediction here
3+
//The output is 320
4+
//no define the reult of multiplying 10 and 32
5+
// This is because the multiply function uses console.log() instead of return.
6+
// The function will print 320 to the console, but then will show
7+
// "undefined" because the function doesn't return a value.
48

59
function multiply(a, b) {
610
console.log(a * b);
@@ -10,5 +14,21 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1014

1115
// =============> write your explanation here
1216

17+
//The multiply() function calculates a * b (which is 320) and prints it
18+
// using console.log().
19+
//console.log() doesn't return a value just output of console
20+
// it only displays output to the console.
21+
//
22+
// there is no return statement, it returns 'undefined'.
23+
// the function:
24+
// 1. Prints "320" to the console
25+
// 2. Returns undefined
26+
// "The result of multiplying 10 and 32 is undefined"
27+
1328
// Finally, correct the code to fix the problem
1429
// =============> write your new code here
30+
31+
function multiply(a, b) {
32+
return a * b; // use return not console
33+
}
34+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
//The output will be:
5+
// "The sum of 10 and 32 is not defined because the return statement without a value.
6+
// The line "a + b;" after return will not executed
7+
// return undefined. The code after return is unreachable.
8+
9+
410
function sum(a, b) {
511
return;
612
a + b;
@@ -9,5 +15,19 @@ function sum(a, b) {
915
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1016

1117
// =============> write your explanation here
18+
// a + b; ← This line will not executes (unreachable code)
19+
// So the function effectively becomes:
20+
// function sum(a, b) {
21+
// return undefined;
22+
// a + b; // not execute
23+
// }
24+
// The return statement immediately exits the function and returns undefined.
25+
// Any code after a return statement is "unreachable" and not execute.
26+
// result is the sum of 10 and 32 is undefined"
1227
// Finally, correct the code to fix the problem
1328
// =============> write your new code here
29+
30+
function sum(a, b) {
31+
return a + b;
32+
}
33+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//The output will be:
6+
// The last digit of 42 is 3
7+
// The last digit of 105 is 3
8+
// The last digit of 806 is 3
9+
//
10+
// All outputs show "3" because the function not taking the parameter
11+
// passe value to 'num' (which is 103), so it returns the last digit of 103, which is 3.
512

613
const num = 103;
714

@@ -15,10 +22,34 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1522

1623
// Now run the code and compare the output to your prediction
1724
// =============> write the output here
25+
26+
// The last digit of 42 is 3
27+
// The last digit of 105 is 3
28+
// The last digit of 806 is 3
1829
// Explain why the output is the way it is
1930
// =============> write your explanation here
31+
// The function getLastDigit() has TWO problems:
32+
// The function definition doesn't have a parameter,
33+
// it can't receive the values (42, 105, 806) added to it.
34+
//inside the function, it uses the global constant
35+
// 'num' (which is 103) instead of using the parameter
36+
//getLastDigit(42) call function not taking 42
37+
// num (103) converts to "103"
38+
// -slice(-1) gets the last character "3"
39+
// returns "3"
2040
// Finally, correct the code to fix the problem
2141
// =============> write your new code here
2242

43+
const num1 = 103;
44+
function getLastDigit(number) { // Added parameter 'number'
45+
return number.toString().slice(-1); // Use 'number' instead of 'num'
46+
}
47+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
48+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
49+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
50+
2351
// This program should tell the user the last digit of each number.
2452
// Explain why getLastDigit is not working properly - correct the problem
53+
//The function has no parameter when call getLastDigit(42),
54+
// the number 42 is passed but storage variable to save it.
55+
// The function can't use it because it doesn't have a parameter to receive it.

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+
return Math.round((weight / (height * height)) * 10) / 10;
20+
}
21+
22+
console.log(calculateBMI(70, 1.73));

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@
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(str) {
19+
// Step 1: Replace all spaces with _
20+
const withUnderscores = str.replace(/ /g, '_');
21+
22+
// Step 2: Convert to uppercase
23+
const upperCase = withUnderscores.toUpperCase();
24+
25+
return upperCase;
26+
}
27+
28+
// Test
29+
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
30+
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
31+
console.log(toUpperSnakeCase("the quick brown fox")); // "THE_QUICK_BROWN_FOX"
32+
console.log(toUpperSnakeCase("code your future")); // "CODE_YOUR_FUTURE"

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,32 @@
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 to convert pence string to pounds format
9+
function toPounds(penceString) {
10+
const penceStringWithoutTrailingP = penceString.substring(
11+
0,
12+
penceString.length - 1
13+
);
14+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
15+
const pounds = paddedPenceNumberString.substring(
16+
0,
17+
paddedPenceNumberString.length - 2
18+
);
19+
const pence = paddedPenceNumberString
20+
.substring(paddedPenceNumberString.length - 2)
21+
.padEnd(2, "0");
22+
return ${pounds}.${pence}`;
23+
}
24+
25+
// Test the function with inputs
26+
console.log(toPounds("399p")); // "£3.99"
27+
console.log(toPounds("50p")); // "£0.50"
28+
console.log(toPounds("5p")); // "£0.05"
29+
console.log(toPounds("1p")); // "£0.01"
30+
console.log(toPounds("99p")); // "£0.99"
31+
console.log(toPounds("100p")); // "£1.00"
32+
console.log(toPounds("1250p")); // "£12.50"
33+
console.log(toPounds("10000p")); // "£100.00"
34+
console.log(toPounds("199p")); // "£1.99"
35+
console.log(toPounds("2550p")); // "£25.50"

0 commit comments

Comments
 (0)