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
33 changes: 26 additions & 7 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// Predict and explain first...
// =============> write your prediction here
/*
I predict the function will throw a SyntaxError as the variable str has already been declared in the function parameter
function capitalise will capitalise the first letter with index 0
and will append the sliced string from index 1 which is the second letter
*/

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

// interpret the error message and figure out why an error is occurring :

/* syntax Error :identifier the variabel has already been declared.
As we can see the str variable has already been declared in the prameter of the function instead we just return the value */

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


// =============> write your explanation here
/* syntax Error :identifier the variabel has already been declared.
As we can see the str variable has already been declared in the prameter of the function instead we just return the value */

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

// =============> write your explanation here
// =============> write your new code here
}
console.log(capitalise("ebrahim"));
console.log(capitalise('salomi'));
29 changes: 22 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
// Predict and explain first...


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

//1- The function is trying to declare the decimalNumber twice in the perameter and later as a new variable
//2-console.log() is printing a variable in the local scope where it can't be accessed as it is inside the function

// 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);

// =============> write your explanation here
/* The variable decimal Number has already been declared in the parameter
and already is a local variable which can't be declared again causing a ReferenceError
As the decimalNumber inside the function we can't print it using the console.log() as it has no power to the local scope
instead we can delete the second declaration and just leave the parameter as an input for the user
Finally, correct the code to fix the problem */

// 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));
15 changes: 12 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
//I predict the funciton will throw a SyntaxEror as the perametters sould be valid variables like names or identifiers with upholding to the variable naming convention and in this case a number found

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

// =============> write the error message here
//the Error is a syntax error: Unexpected number

// =============> explain this error message here
/* The function was givin a number as its perameter where in JS only allows varaible names and usnig a number causes a SyntaxError
Also, the function was returning num*num where num is undefined causing a ReferenceError
*/

// Finally, correct the code to fix the problem

// =============> write your new code here

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

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

// =============> write your prediction here
// This function would multiply the two arguments a and b whatever that is
// when we log the output we will get the string and the output of the function but because we are not returning an output we might run into an error

function multiply(a, b) {
console.log(a * b);
}
// 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
//The console.log() inside the funtion will print a*b but when we call the function will return undefined

// 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)}`);

20 changes: 15 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// The function suppose to return the sum of the two variables but we will encounter an error as the code after return is unreachable.

function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// function sum(a, b) {
// return;
// a + b;
// }
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

// As we return nothing by closing the line with a semicolon hence a + b is not returned where we tell the computer to exit and go back to global scope and the computer will not be able to read the lines after that

// 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)}`);
36 changes: 29 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@

// Predict the output of the following code:
// =============> Write your prediction here
//getLastDigit will convert a number to a string
// However, since there is no parameter or local variable to the function we might run into an Error

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

function getLastDigit() {
return num.toString().slice(-1);
}
// function getLastDigit() {
// 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)}`);
// 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)}`);

// 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
// As expected, the function has no parameters, so it relies on the global variable "num" as its only variable.
// Since there are no parameters defined, the function permanently uses the global variable instead.
// This means even if we pass arguments when calling the function, they will be ignored because there are no parameters to receive them.

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

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
/* the reason why getLastDigit isn't working properly is that it doesn't accept any arrguments where there isn't any perameters in its difination.
where it uses the golabal varaible 'num' decalred outside the funtion and already set to 103

The correct code includes a parameter 'num' in the difination this allows the function to accept the number passed to it when called*/
10 changes: 8 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,11 @@
// 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
}
let result = (weight/(height*height));
return Number(result.toFixed(1));


}

console.log(calculateBMI(70,1.7))
console.log(calculateBMI(72,1.7))
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 toUpperSnakeFormat(string){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rename to match the vocabulary brief in the link, i.e toUpperSnakeCase

return string.toUpperCase().replaceAll(" ","_")

}
console.log(toUpperSnakeFormat("hello there"))
console.log(toUpperSnakeFormat("lord of the rings"))
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}`
}


console.log(toPounds('987p'));
console.log(toPounds('909p'));
console.log(toPounds('345p'));
14 changes: 9 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61))
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// 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
// =============> the first time pad was called with the totalHours

// 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() is called with the remaining seconds which is value "1"

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> "01"
// the return value is 1 put as we are using pad wouldbe adding leading "0"