diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..69142c85f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +//function capitalize 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 +//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 +// The Error message is -function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; -} +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// console.log(str); +// return str; +// } + + +// =============> write your explanation here -// =============> write your explanation here // =============> write your new code here + +function capitalise(str) { +return `${str[0].toUpperCase()}${str.slice(1)}`; + +} +capitalise("ebrahim"); +capitalise('salomi'); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..78fe0ad0c 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -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 +// =============> write your explanation here +//similar to the previous exercise where the variable decimal Number has already been declared in the parameter +// and already is a local variable which can't be declared again +//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 which would be nicely work // 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)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..83aebfc22 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,16 +4,22 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +//The error might be accuring because the parameter can't be a number and should be a variable -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 +// we can put a number or any argument when we call the number not when we shape it // Finally, correct the code to fix the problem +function square(num) { + return num * num; + } // =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..9f36c2f46 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -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 means will return nothing)for the two variables a and b // 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)}`); + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..fa992a8ef 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // 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 probably a syntax Error -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)}`); +// 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 // =============> write your explanation here + // 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..1b897f8f1 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,23 +2,41 @@ // Predict the output of the following code: // =============> Write your prediction here +//getLastDigit will convert a number to a string and reverse it +// However, since there is no parameter or local variable 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..9b759d6dd 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -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 -} \ No newline at end of file + let result = Number(weight/(height*height)); + return result.toFixed(1); + + +} + +console.log(calculateBMI(70,1.7)) +console.log(calculateBMI(72,1.7)) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..d035a53b1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -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){ + return string.toUpperCase().replaceAll(" ","_") + +} +console.log(toUpperSnakeFormat("hello there")) +console.log(toUpperSnakeFormat("lord of the rings")) \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..6d6d0502c 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -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')); \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..4d01b1681 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,24 +11,28 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(60)) // 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 assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> "01" +// When it is call for the last time it adds 0 before 1 to make it 2 characters long