Skip to content

Commit 266b20f

Browse files
committed
Sprint-2 Mandatory-implement,interpret and stretch-extend are done.
1 parent 6a375b5 commit 266b20f

11 files changed

Lines changed: 270 additions & 228 deletions

File tree

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Predict and explain first...
2-
// =============> write your prediction here
3-
// I predict the code will give an error because inside the function we create a variable called str again.
4-
5-
// call the function capitalise with a string input
6-
// interpret the error message and figure out why an error is occurring
7-
8-
function capitalise(str) {
9-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
10-
return str;
11-
}
12-
13-
// =============> write your explanation here
14-
// The error occurs because we are trying to declare a variable named str inside the function, but it is already declared as a parameter. This creates a conflict and results in a syntax error.
15-
// =============> write your new code here
16-
function capitalise(str) {
17-
return `${str[0].toUpperCase()}${str.slice(1)}`;
18-
}
1+
// Predict and explain first...
2+
// =============> write your prediction here
3+
// I predict the code will give an error because inside the function we create a variable called str again.
4+
5+
// call the function capitalise with a string input
6+
// interpret the error message and figure out why an error is occurring
7+
8+
function capitalise(str) {
9+
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
10+
return str;
11+
}
12+
13+
// =============> write your explanation here
14+
// The error occurs because we are trying to declare a variable named str inside the function, but it is already declared as a parameter. This creates a conflict and results in a syntax error.
15+
// =============> write your new code here
16+
function capitalise(str) {
17+
return `${str[0].toUpperCase()}${str.slice(1)}`;
18+
}

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
// Predict and explain first...
2-
3-
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
// console.log(decimalNumber); will cause error because decimalNumber only exists inside the function.
6-
// Try playing computer with the example to work out what is going on
7-
8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
11-
12-
return percentage;
13-
}
14-
15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
18-
//The error happens because the function already has a parameter called decimalNumber.
19-
//Creating another variable with the same name inside the function causes a conflict.
20-
21-
// Finally, correct the code to fix the problem
22-
// =============> write your new code here
23-
function convertToPercentage(decimalNumber) {
24-
const percentage = `${decimalNumber * 100}%`;
25-
26-
return percentage;
27-
}
28-
29-
console.log(convertToPercentage(0.5));
1+
// Predict and explain first...
2+
3+
// Why will an error occur when this program runs?
4+
// =============> write your prediction here
5+
// console.log(decimalNumber); will cause error because decimalNumber only exists inside the function.
6+
// Try playing computer with the example to work out what is going on
7+
8+
function convertToPercentage(decimalNumber) {
9+
const decimalNumber = 0.5;
10+
const percentage = `${decimalNumber * 100}%`;
11+
12+
return percentage;
13+
}
14+
15+
console.log(decimalNumber);
16+
17+
// =============> write your explanation here
18+
//The error happens because the function already has a parameter called decimalNumber.
19+
//Creating another variable with the same name inside the function causes a conflict.
20+
21+
// Finally, correct the code to fix the problem
22+
// =============> write your new code here
23+
function convertToPercentage(decimalNumber) {
24+
const percentage = `${decimalNumber * 100}%`;
25+
26+
return percentage;
27+
}
28+
29+
console.log(convertToPercentage(0.5));

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
2-
// Predict and explain first BEFORE you run any code...
3-
4-
// this function should square any number but instead we're going to get an error
5-
6-
// =============> write your prediction of the error here
7-
// it is syntax error. 3 is a value not a variable name.
8-
9-
function square(3) {
10-
return num * num;
11-
}
12-
13-
// =============> write the error message here
14-
//function square(3) {
15-
^
16-
17-
//SyntaxError: Unexpected number
18-
19-
// =============> explain this error message here
20-
//The error is as I predicted. The function parameter should be a variable name, not a value.
21-
//In this case, 3 is a value and cannot be used as a parameter name.
22-
23-
// Finally, correct the code to fix the problem
24-
25-
// =============> write your new code here
26-
function square(num) {
27-
return num * num;
28-
}
1+
2+
// Predict and explain first BEFORE you run any code...
3+
4+
// this function should square any number but instead we're going to get an error
5+
6+
// =============> write your prediction of the error here
7+
// it is syntax error. 3 is a value not a variable name.
8+
9+
function square(3) {
10+
return num * num;
11+
}
12+
13+
// =============> write the error message here
14+
//function square(3) {
15+
^
16+
17+
//SyntaxError: Unexpected number
18+
19+
// =============> explain this error message here
20+
//The error is as I predicted. The function parameter should be a variable name, not a value.
21+
//In this case, 3 is a value and cannot be used as a parameter name.
22+
23+
// Finally, correct the code to fix the problem
24+
25+
// =============> write your new code here
26+
function square(num) {
27+
return num * num;
28+
}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// Predict and explain first...
2-
3-
// =============> write your prediction here
4-
// I predict the code will give an error because the multiply function does not return a value.
5-
6-
function multiply(a, b) {
7-
console.log(a * b);
8-
}
9-
10-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11-
12-
// =============> write your explanation here
13-
//The function multiply(a, b) logs the result but does not return anything.
14-
15-
// Finally, correct the code to fix the problem
16-
// =============> write your new code here
17-
function multiply(a, b) {
18-
return a * b;
19-
}
20-
21-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1+
// Predict and explain first...
2+
3+
// =============> write your prediction here
4+
// I predict the code will give an error because the multiply function does not return a value.
5+
6+
function multiply(a, b) {
7+
console.log(a * b);
8+
}
9+
10+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11+
12+
// =============> write your explanation here
13+
//The function multiply(a, b) logs the result but does not return anything.
14+
15+
// Finally, correct the code to fix the problem
16+
// =============> write your new code here
17+
function multiply(a, b) {
18+
return a * b;
19+
}
20+
21+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// Predict and explain first...
2-
// =============> write your prediction here
3-
// I predict the code will give an error because the sum function does not return a value.
4-
5-
function sum(a, b) {
6-
return;
7-
a + b;
8-
}
9-
10-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11-
12-
// =============> write your explanation here
13-
// The function sum(a, b) has a return statement that does not return the result of a + b.
14-
// Instead, it returns undefined because the return statement is followed by a semicolon, which ends the statement before the addition operation is executed.
15-
// Finally, correct the code to fix the problem
16-
// =============> write your new code here
17-
function sum(a, b) {
18-
return a + b;
19-
}
20-
21-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1+
// Predict and explain first...
2+
// =============> write your prediction here
3+
// I predict the code will give an error because the sum function does not return a value.
4+
5+
function sum(a, b) {
6+
return;
7+
a + b;
8+
}
9+
10+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
12+
// =============> write your explanation here
13+
// The function sum(a, b) has a return statement that does not return the result of a + b.
14+
// Instead, it returns undefined because the return statement is followed by a semicolon, which ends the statement before the addition operation is executed.
15+
// Finally, correct the code to fix the problem
16+
// =============> write your new code here
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: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
// Predict and explain first...
2-
3-
// Predict the output of the following code:
4-
// =============> Write your prediction here
5-
//The function has no parameter, so it cannot use input values like 42, 105, 806.
6-
7-
const num = 103;
8-
9-
function getLastDigit() {
10-
return num.toString().slice(-1);
11-
}
12-
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)}`);
16-
17-
// Now run the code and compare the output to your prediction
18-
// =============> write the output here
19-
// Explain why the output is the way it is
20-
// =============> write your explanation here
21-
//The function is not using the value passed into it.
22-
// Finally, correct the code to fix the problem
23-
// =============> write your new code here
24-
function getLastDigit(num) {
25-
return num.toString().slice(-1);
26-
}
27-
28-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
29-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
30-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
31-
// This program should tell the user the last digit of each number.
32-
// Explain why getLastDigit is not working properly - correct the problem
33-
// If a function should work with different values → it must have parameters.
34-
// Otherwise it will always use the same fixed value
1+
// Predict and explain first...
2+
3+
// Predict the output of the following code:
4+
// =============> Write your prediction here
5+
//The function has no parameter, so it cannot use input values like 42, 105, 806.
6+
7+
const num = 103;
8+
9+
function getLastDigit() {
10+
return num.toString().slice(-1);
11+
}
12+
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)}`);
16+
17+
// Now run the code and compare the output to your prediction
18+
// =============> write the output here
19+
// Explain why the output is the way it is
20+
// =============> write your explanation here
21+
//The function is not using the value passed into it.
22+
// Finally, correct the code to fix the problem
23+
// =============> write your new code here
24+
function getLastDigit(num) {
25+
return num.toString().slice(-1);
26+
}
27+
28+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
29+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
30+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
31+
// This program should tell the user the last digit of each number.
32+
// Explain why getLastDigit is not working properly - correct the problem
33+
// If a function should work with different values → it must have parameters.
34+
// Otherwise it will always use the same fixed value
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
// Below are the steps for how BMI is calculated
2-
3-
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
4-
5-
// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
6-
7-
// squaring your height: 1.73 x 1.73 = 2.99
8-
// dividing 70 by 2.99 = 23.41
9-
// Your result will be displayed to 1 decimal place, for example 23.4.
10-
11-
// You will need to implement a function that calculates the BMI of someone based off their weight and height
12-
13-
// Given someone's weight in kg and height in metres
14-
// Then when we call this function with the weight and height
15-
// It should return their Body Mass Index to 1 decimal place
16-
17-
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
const bmi = weight / (height * height);
20-
return Number(bmi.toFixed(1));
21-
}
22-
console.log(calculateBMI(72, 1.76));
23-
// just to check the function is working correctly, and it works.
1+
// Below are the steps for how BMI is calculated
2+
3+
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
4+
5+
// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
6+
7+
// squaring your height: 1.73 x 1.73 = 2.99
8+
// dividing 70 by 2.99 = 23.41
9+
// Your result will be displayed to 1 decimal place, for example 23.4.
10+
11+
// You will need to implement a function that calculates the BMI of someone based off their weight and height
12+
13+
// Given someone's weight in kg and height in metres
14+
// Then when we call this function with the weight and height
15+
// It should return their Body Mass Index to 1 decimal place
16+
17+
function calculateBMI(weight, height) {
18+
// return the BMI of someone based off their weight and height
19+
const bmi = weight / (height * height);
20+
return Number(bmi.toFixed(1));
21+
}
22+
console.log(calculateBMI(72, 1.76));
23+
// just to check the function is working correctly, and it works.
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
// A set of words can be grouped together in different cases.
2-
3-
// For example, "hello there" in snake case would be written "hello_there"
4-
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
5-
6-
// Implement a function that:
7-
8-
// Given a string input like "hello there"
9-
// When we call this function with the input string
10-
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
11-
12-
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
13-
14-
// You will need to come up with an appropriate name for the function
15-
// Use the MDN string documentation to help you find a solution
16-
// 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-
console.log(toUpperSnakeCase("hello there"));
21-
console.log(toUpperSnakeCase("lord of the rings"));
1+
// A set of words can be grouped together in different cases.
2+
3+
// For example, "hello there" in snake case would be written "hello_there"
4+
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.
5+
6+
// Implement a function that:
7+
8+
// Given a string input like "hello there"
9+
// When we call this function with the input string
10+
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
11+
12+
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"
13+
14+
// You will need to come up with an appropriate name for the function
15+
// Use the MDN string documentation to help you find a solution
16+
// 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+
console.log(toUpperSnakeCase("hello there"));
21+
console.log(toUpperSnakeCase("lord of the rings"));

0 commit comments

Comments
 (0)