Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

// =============>
I predicte that the error will be that the variable str is already declared in the function.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -9,5 +9,12 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here

// =============> write your explanation here
when I run the program it give an error message that the identifier str has already been declared. this is because the variable str is declared twice in the function capitalise and in the variable let str.
to fix this we can change the variable name of the let str to newStr and this will fix the issue.
// =============> write your new code here
function capitalise(str) {
let newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return newStr;
}
16 changes: 14 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
=============> write your prediction here: the error will occur because the variable decimslNumber gevine a value of 0.5
with a const declaration and this will make an error if the brograme given a diffrent value for the decimalNumber.

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

Expand All @@ -14,7 +15,18 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> write your explanation here
when the program run it give a SyntaxError: Identifier 'decimalNumber' has already been declared. This is because the variable decimalNumber is declared twice in the function
convertToPercentage, and inside the function with the constant varible it redeclared again also using the function name decimalNumber with the console.log function will give an error because the function name should be
Copy link
Contributor

Choose a reason for hiding this comment

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

This sentence is quite long, and without punctuation marks it is not easy to read. Can you revise it?

to fix this error we can remove the const variable declaration of decimalNumber from the function and use the function name
'ConvertToPercentage' to recall the function and pass the value of the decimalNumber.CSSStyleDeclaration
Copy link
Contributor

Choose a reason for hiding this comment

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

.CSSStyleDeclaration: What is this?


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

return percentage;
}

console.log(convertToPercentage(0.5));
23 changes: 19 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
the error will occur because the paramater of the function is not a valid variable name as it is a number.
also the variable num is not declared in the function and this will give an error when the program run because the function will not know what the value of the num is.

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

// =============> write the error message here
// =============> write the error message here
function square(3) {
^

// =============> explain this error message here
SyntaxError: Unexpected number

// Finally, correct the code to fix the problem
// =============> explain this error message here it means that the paramater of the function is an unexpected number (3) and this is not a valid variable number.
I deleted the paramater of the function and run the programe and I get this error:
/box/index.js:2
return num * num;
^

// =============> write your new code here
ReferenceError: num is not defined


// Finally, correct the code to fix the problem
change the function to this: function square(num)
// =============> write your new code here
function square(num) {
return num * num;
}
console.log(square(3));
17 changes: 17 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...

// =============> write your prediction here
I prediact that the error will occur because the function multiply does not return any value and this will make the console.log function to print undefined instead.


function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +11,21 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
when the program run it give this output:

320
The result of multiplying 10 and 32 is undefined

320: the out put for the console.log inside the function

while the ouout the out put for the console.log function out of the function returned the string provided in the function and an undefiened because the function multibly doesn't return any value.
to fix this error we need to change the console.log function inside the multiply function to return.
Comment on lines +21 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

You have probably figured out the bug, but I can't quite get what this sentence mean.



// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
I predicted that the function will not return the correct result because the return statment is not correctly declared.

function sum(a, b) {
return;
Expand All @@ -9,5 +10,15 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
when I run the program it give this output:
The sum of 10 and 32 is undefined

the output is undefiened because the function sum does not return any value.
to fix this error we need to change the return statement to return a + b instead of just return.

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b;
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
21 changes: 18 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Predict the output of the following code:
// =============> Write your prediction here
I predict that because the num variable has been given a constant value, the program will give undefiened when the getLastDigt function called after.


const num = 103;

Expand All @@ -14,11 +16,24 @@ 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
the output give 3 which is the last digit for the constant value declared to the num
because the function getLastDigit doesn't have the num as a paramater to accept it when we call it with the console.log function.
// Finally, correct the code to fix the problem
// =============> write your new code here

const num = 103;

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.
// Explain why getLastDigit is not working properly - correct the problem
because it hasn't been gevine a paramater when it declared but when the num added as a paramater the program give the xpeacted outputs.
7 changes: 6 additions & 1 deletion 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) {
let BMI = weight / (height ** 2);
return BMI.toFixed(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?

Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example,

  console.log(123);              // Output 123
  console.log("123");            // Output 123
  
  // Treated differently in the program
  let sum1 = 123 + 100;         // Evaluate to 223 -- a number
  let sum 2 = "123" + 100;      // Evaluate to "123100" -- a string.

}

console.log(calculateBMI(80, 1.7));

// return the BMI of someone based off their weight and height
}
6 changes: 6 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,9 @@
// 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 toUpperSnakeCase(str) {
return str.toUpperCase().replace(/ /g, '_')
}

console.log(toUpperSnakeCase ('hello there'));
21 changes: 21 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,24 @@
// 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(penceString) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);


const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
return `£${pounds}.${pence}`;
}
Comment on lines +8 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is off.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?


console.log(`£${pounds}.${pence}`);
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
Three times, once for the hours, minutes and seconds.

// 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
The value assighned to num when the pad function called for the first time is 0, because the total hours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
the return value of pad when it is called for the first time is '00' because the pad function add a '0' to the left of the num to make it 2 characters long. AS the num is 0 the return value is also ' 0'.

// 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
the value assighned to num when the pad function is called for the last time is 1 , because the remaining seconds is 1, as 61 seconds os equal to 1 minute and 1 second.

// 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
the return value of pad when it is called for the last time is '01' because the pad function adds a '0' to the left of the num to make it 2 characters long. As the num is 1, the return value is '01'.
Loading