Skip to content
Open
16 changes: 11 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// I guess that the error could be due to the having the same variable in parameter and in the function body.

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

// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

// So, the error sounds "SyntaxError: Identifier 'str' has already been declared", it means that we can't declare the sane variable name twice
// =============> write your new code here
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct but can you think of a way to simplify it? Also, does it need to be let?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, "const" might be better option, cause I don't need to change "capitalisedStr" in next evalutaion of my function.
About simplifying - actually I can return the result without any defined variable.

}

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("greetings")); // Output: "Greetings"
24 changes: 18 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I suppose the error could be:
// 1. The variable "decimalNumber" declared twice in the same scope (in the function's parameter and in the function's body)
// 2. We can't reassign a value if we use word "const" to declare a variaable
// 3. The variable "decimalNumber" isn't defined in the global scope, so when we try to use function console.log(decimalNumber) it will throw a ReferenceError. If we want to log the result of the function we have to call this function with some argument: console.log(convertToPercentage(0.5))

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here
//So, when I run the code, I got "SyntaxError: Identifier 'decimalNumber' has already been declared" and that the same as my prediction above. I didn't think that actually we don't need to declare "decimalNumber" again inside the function, and I removed line "const decimalNumber = 0.5; to fix the problem. THen, I called the function with the argument 0.5 in console.log and this worked fine.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;

return percentage;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my previous comment for capitalise function, can you think of a way to simplify it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified this function in the same way like with previous task :)

}

console.log(convertToPercentage(0.5));
15 changes: 10 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

// Predict and explain first BEFORE you run any code...

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

// =============> write your prediction of the error here
//I think that's an error because there is no declared variable "num".

function square(3) {
return num * num;
}
// function square(3) {
// return num * num;
// }

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// As I can see now after running the code, I missed the very first error - we can't use a number as a parameter name, and JavaScript even didn't go after the very first mistake to check others errors (and my predicted error about undeclared "num")

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log(square(12)); // 144
11 changes: 7 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Predict and explain first...

// =============> write your prediction here
// =============> I think the result will be undefined because in the function there is no return statement.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good guess!


function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// =============> Its interesting - function log the result but doesn't return it. So when we see the output 320 and "undefined"

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> function multiply(a, b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you would want to comment out the broken code and have your fixed code uncommented. Same for other files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! It was confusing for me because there weren't some clear instructions about what do I have to comment/uncomment.
Now I've done this due to your recommendation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note this!

// return a * b;
// }
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
8 changes: 5 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> I guess that the output will be undefined because there is a semicolon after the "return" and this breaks the line of code, and after return statement nothing can be returned

function sum(a, b) {
return;
Expand All @@ -8,6 +8,8 @@ function sum(a, b) {

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// =============> The output is "The sum of 10 and 32 is undefined" as I predicted above
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> function sum(a, b) {
// return a + b;
// }
31 changes: 25 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> The output will be:
// 1. The last digit of 42 is 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

// 2. The last digit of 105 is 3
// 3. The last digit of 806 is 3
// Why? Because we don't pass any parameter to the function ant it always uses the variable "num", which defined in the global scope, so function has access to it and the last digit of 103 is 3.

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -14,11 +18,26 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// =============> The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
// Explain why the output is the way it is
// =============> write your explanation here
// =============> Because we use the global variable "num" in the function instead of passing the parameter to the function.
// Finally, correct the code to fix the problem
// =============> write your new code here

//=============> const num = 103;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point of this exercise is not to declare any global variables and just pass all the numbers as function arguments

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about it , but I was sure that I have to change only function. My bad!

// function getLastDigit(num) {
// return num.toString().slice(-1);
// }
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
//
// This program should tell the user the last digit of each number.
//
// Now output looks correctly:
// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 6
//
// Explain why getLastDigit is not working properly - correct the problem
// Actually, maybe its not the good idea to use the same name "num" for different variables in different scopes, because it can be confusing. It works correctly but next time I'll use for the global scope some more meaningful name.
9 changes: 7 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
bmi = Number((weight / height ** 2).toFixed(1));
return bmi;
}

console.log(calculateBMI(70, 1.73)); // 23.4
console.log(calculateBMI(120, 1.82)); // 36.2
console.log(calculateBMI(53, 1.68)); // 18.8
9 changes: 9 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

function toUpperCaseSnakeString(str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine but could be better. Do you need to say string here? Think of other string methods such as toUpperCase or to toLowerCase. Can you think of a better name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I changed this name to "toUpperSnakeCase".

transformedStr = str.toUpperCase().replaceAll(" ", "_");
return transformedStr;
}

console.log(toUpperCaseSnakeString("hello there")); // "HELLO_THERE"
console.log(toUpperCaseSnakeString("lord of the rings")); // "LORD_OF_THE_RINGS"
console.log(toUpperCaseSnakeString("code your future is great")); // "CODE_YOUR_FUTURE_IS_GREAT"
23 changes: 23 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs

function toPounds(penceStr) {
const penceStrWithoutTrailingP = penceStr.substring(0, penceStr.length - 1);
const paddedPenceNumberString = penceStrWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

return `£${pounds}.${pence}`;
}

console.log(toPounds("123p")); // £1.23
console.log(toPounds("1002p")); // £10.02
console.log(toPounds("0p")); // £0.00
console.log(toPounds("99p")); // £0.99
console.log(toPounds("250p")); // £2.50
console.log(toPounds("7p")); // £0.07
console.log(toPounds("48p")); // £0.48
console.log(toPounds("53647868p")); // £536478.68
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times

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

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00"

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> "1". The last time pad works on remainingSeconds which is "1" if the input (remainingSeconds) is 61: remainingSeconds = seconds % 60, modulo operator gives the remainder of the division of seconds by 60, so 61 % 60 = 1.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> "01". THe pad(1) is called, converting 1 to string and transform it to "01".
Loading
Loading