Skip to content

Commit 15c4f3b

Browse files
committed
Completed exercises work
1 parent d28242a commit 15c4f3b

13 files changed

Lines changed: 246 additions & 40 deletions

File tree

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,45 @@
33

44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
6+
// The error is SyntaxError: Identifier 'str' has already been declared
7+
//in line 9 and 10 str is declared as a parameter of capitalise and again when let is used to initalise it
8+
//syntax error means something in the code violate javascript rules
9+
//in this case str has been declared twice
10+
//I had to search solutions for this case which I encountered several times.
11+
//it looks like a common error
12+
13+
//function capitalise(str) {
14+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
15+
//return str;
16+
//}
17+
18+
// =============> write your explanation here
19+
// =============> write your new code here
20+
//the solution would be one of these:
21+
//the easiest solution would be to remove let and not to declare it again, since it's already declared inside a function as a parameter
22+
//other solutions I will list them later
623

724
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
25+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
926
return str;
1027
}
28+
console.log(capitalise("hi"));
29+
30+
// Predict and explain first...
31+
// =============> write your prediction here
32+
// A function capitalise is caolled, and a variable called str is initialised
33+
//the variable str value is assigned using the string methods toUpperCase and slice
34+
35+
//than a return function is called to show the str value
36+
//I predict that the input str characters get all capitalsed becuase toUpperCase method
37+
//it starts applying this from index 0 or first character and the slice method is telling it to leave every character afrter the index 1 as it is
38+
39+
//so where does it end?
40+
//another method str.slice is used to extract part of the string and returning it
41+
//here it has a start index only which is 1 and the end index omitted
42+
//because the end index omitted it will start from index 1 to the end of the string
43+
44+
//I predict the value returned to be nothing because no value was assigned to str
45+
// call the function capitalise with a string input
46+
// interpret the error message and figure out why an error is occurring
1147

12-
// =============> write your explanation here
13-
// =============> write your new code here

‎Sprint-3/1-key-errors/1.js‎

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
54

5+
// =============> write your prediction here
6+
//this has the same issue as the one I encountered in the previous example. decimalNumber is declared twice
7+
//so I'm expecting a syntaxError, the identifier decimalNumber has been declared twice
68
// Try playing computer with the example to work out what is going on
79

10+
//function convertToPercentage(decimalNumber) {
11+
// const decimalNumber = 0.5;
12+
// const percentage = `${decimalNumber * 100}%`;
13+
14+
// return percentage;
15+
//}
16+
17+
//console.log(decimalNumber);
18+
19+
// =============> write your explanation here
20+
//the solution is to make the variable declared only once as a parameter
21+
// and than remove the declaration from line 11 by removing let and reassigning its value
22+
// Finally, correct the code to fix the problem
23+
// =============> write your new code here
24+
825
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
26+
decimalNumber = 0.5;
1027
const percentage = `${decimalNumber * 100}%`;
1128

1229
return percentage;
1330
}
1431

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
32+
console.log(convertToPercentage(0.8));
33+
//ok I had to correct my solution because a reference error is caused by
34+
//calling a parameter that only identified inside the function
35+
//the solution would be to change line 18 to call the function and input
36+
//input the value we want to convert because no point of declaring identifier twice
37+
//what I notice by connicting both examples: the previous example the identifier was declared twice inside the function which caused a syntaxError
38+
//in this example there is also declaring identifier that only identified inside a function , so the program doesn't recognise it which cause
39+
//cause a reference error
1840

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
21
// Predict and explain first BEFORE you run any code...
32

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

65
// =============> write your prediction of the error here
6+
//ok, according to javaScript rules or convention for variables, we can't start with a number
7+
//I'm expecting a syntaxErrror because its against javaScript rules
78

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

1213
// =============> write the error message here
13-
14+
//SyntaxError: Unexpected number
15+
//not bad for a prediction-_-
1416
// =============> explain this error message here
15-
17+
//it means javaCript can't identify what this number represent
18+
//it's not part of the built in rules
1619
// Finally, correct the code to fix the problem
17-
20+
//we just change number 3 to a the word three and assign its value to num
1821
// =============> write your new code here
22+
function square(three) {
23+
num = three;
24+
return num * num;
25+
}
26+
console.log(square(4));
1927

2028

29+
//so far this sprint is easy-_-

‎Sprint-3/1-key-errors/Terminal.log‎

Whitespace-only changes.

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

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

33
// =============> write your prediction here
4+
//inside the function console.log doesn't have the value of a and b parameters
5+
//if I think like the computer than I will see a function declared with parameters a and b
6+
//next line I will see a print function of the parameters a and b with operator *
7+
//I'm not a computer but I think if I was , I will print a * b
8+
//than I will call the multiply function again to show the message
9+
//The result of multiplying 10 and 32 is 320
10+
//function multiply(a, b) {
11+
//console.log(a * b);
12+
//}
413

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)}`);
14+
//console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1015

1116
// =============> write your explanation here
17+
//ok, I was werong in my prediction about console.log not using the parameters value outside the function, which I correctr my understanduing Headers.apply
18+
//what happens is the result is printed twice, the solution would be to remove the console.log from inside the function and use return instead while keeping console.log function call or in line 15
1219

1320
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
21+
// ===========> write your new code here
22+
function multiply(a, b) {
23+
return (a, b);
24+
}
25+
26+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//ok, I predict a syntaxError: a and b are not identified, and I think this because
4+
//because the use of return function, I haven't encountered a similar case but I think it has to do
5+
//with what the return function do, it returns the values of the parameters so
6+
//so when a and b are used there is an issue happining but I can't say what because I don't have the information
7+
//function sum(a, b) {
8+
//return;
9+
//a + b;
310

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
11+
//}
812

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

1115
// =============> write your explanation here
16+
//ok I see this error "The sum of 10 and 32 is undefined"
17+
//I had to search what return do that cause this to be able to explain it correctly.
18+
// this is my explaination before searching " I'm suspecting the error is caused by the return function returning the parameters values so when we use a + b; the program doesn't find what we are looking for because it was already returned"
19+
//after searching I understand it caused becaused return means exiting the function , so line 10 will never be excuted
20+
1221
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
22+
//the correction would be to include a + b inside return function so the value become identified
23+
// ===========> write your new code here
24+
25+
function sum(a, b) {
26+
return a + b;
27+
}
28+
29+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//ok the issue is clear, const is used to initialise the variable num
6+
//at the same time num supposed to take several inputs or change its inputs
7+
//to get the last digit of each number. However, its declared before the function using const so its value can't be reassigned
8+
//so I'm assuming its value will always be excuted in the calculation
59

6-
const num = 103;
10+
//const num = 103;
711

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
12+
//function getLastDigit() {
13+
//return num.toString().slice(-1);
14+
//}
1115

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)}`);
16+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
17+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
18+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519

1620
// Now run the code and compare the output to your prediction
1721
// =============> write the output here
22+
//The last digit of 42 is 3
23+
//The last digit of 105 is 3
24+
//The last digit of 806 is 3
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
//Because num was declared as a constant value 103 , so the assigned value of num is 103
28+
29+
//so out output of the last digit will calculate num when the input is 103 only
30+
//no matter how many numbers we include in the brackets, the excution of the function depend on the declared num value
31+
//num is declared with const and has the value 103
32+
//the solution is to declare num as a parameter inside the function
2033
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
34+
// ===========> write your new code here
2235

36+
function getLastDigit(num) {
37+
return num.toString().slice(-1);
38+
}
39+
40+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
41+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
42+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem
45+
//because the declared declared variable num is assigned a constant value 103.
46+
//the solution is to delete the loine before the function and declare num as a parameter inside the function

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
// Then when we call this function with the weight and height
1515
// It should return a string of their Body Mass Index to 1 decimal place
1616

17+
//function calculateBMI(weight, height) {
18+
// return the BMI of someone based off their weight and height
19+
//}
20+
1721
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
22+
//I had to search about how to include Math.floor and decimal digits to do line 30 correctly
23+
return Math.floor((weight / (height * height)) * 10) / 10;
1924
}
25+
26+
console.log(`Your Bmi is ${calculateBMI(70, 1.73)}`);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@
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 capitalise_all_letters(str) {
19+
//I used toUpperCase string method to capitalise all the string
20+
//I used replace to replace first space " " in the string and replace it with "_"
21+
return str.toUpperCase().replace(" ", "_");
22+
}
23+
24+
console.log(`The result is ${capitalise_all_letters("hello world")}`);
25+
26+
// so far its easy -_-

‎Sprint-3/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+
8+
//I got some help from CoPilot in debugging some parts.
9+
// hard one
10+
11+
function toPounds(penceString) {
12+
const penceStringWithoutTrailingP = penceString.substring(
13+
0,
14+
penceString.length - 1,
15+
);
16+
17+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
18+
const pounds = paddedPenceNumberString.substring(
19+
0,
20+
paddedPenceNumberString.length - 2,
21+
);
22+
23+
const pence = paddedPenceNumberString
24+
.substring(paddedPenceNumberString.length - 2)
25+
.padEnd(2, "0");
26+
27+
return `£${pounds}.${pence}`;
28+
}
29+
30+
console.log(toPounds("399p"));
31+
32+
//This part we are removing the last character from a string. We are removing p from 399p
33+
34+
//,padStart(3, "0"); it means make sure the string is 3 chaeacter ,
35+
// if not, add zero to the front
36+
//substring(0)
37+
//paddedPenceNumberString.length - 2 means take all except the last two characters
38+
39+
//why do we do this code line? because pence is the decimals in pound
40+
//so first we take all except the last two digets, means we take the pound part
41+
42+
//here we are dealing with the pence or decimal part of the pound
43+
//line 35, 36 mean we create a variable pence and assign it the last two digits of (paddedPrnceString)
44+
45+
//here we make sure the pence value is 2 digits, if not add zero to the end
46+
47+
//this part is the money format, we created a template literal to add the value of pounds to pence
48+
//we seprate them with a dot

0 commit comments

Comments
 (0)