diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..2c591fd7e 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,19 @@ // Predict and explain first... -// =============> write your prediction here +// on line 8, let declaring with name the same as the parameter // 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)}`; +//It says syntax error Identifier 'str' has already been declared. +// function parameter and let which is inside the funtion can never +// be the same because both variables are in the same scope +// function capitalise(str) { +//let str = `${str[0].toUpperCase()}${str.slice(1)}`; +//return str; +//} +//console.log(capitalise("error") +// +function capitalise(firstLetter) { + let str = `${firstLetter[0].toUpperCase()}${firstLetter.slice(1)}`; return str; } - -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("error")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..38ec4a596 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,7 +1,11 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// / to calculate the percentage it has used the value from +// convertToPercentage() function so there will be error because +// the variable name for the parameter and const is the same. and console.log is called +// to print decimalNumber which is not known outside the function +// instead of printing the retun value of the function. // Try playing computer with the example to work out what is going on @@ -14,7 +18,28 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// the error was syntaxError it is stating decimalNumber has already been declared +//the parameter of the function convertToPercentage() and the const variable is the same // Finally, correct the code to fix the problem -// =============> write your new code here +// There can be two ways to fix the problem +// 1. to change const variable and the name decimalNumber in line 15. +// but still need to call the function convertToPercentage() in the consol.log() + +// function convertToPercentage(decimalNumber){ +// const numeral = 0.5; +// const percentage = `${numeral*100}%`; +// return percentage; +// } +// console.log(convertToPercentage()); + +//2 .The second one is to call the function with value in it. +// and to keep the code as it is except change the const variable name/or delet +// the const numeral with its value because there is no poin of keeping it. +// +// function convertToPercentage(decimalNumber){ +// const numeral = 0.5; +// const percentage = `${decimalNumber*100}%`; +// return percentage; +// } +// console.log(convertToPercentage()); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..39b639397 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,24 @@ - // 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 +// =============> function square(), shouldn't give a value to it , we can only pass +// an argument when the function is called, with value in it which is called argument. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} +//console.log(square()) -// =============> write the error message here +// =============> Unexpected number -// =============> explain this error message here +// =============> unexpected number in the function, there should be only parameter there but here +// number is assigned instead of the variable name // Finally, correct the code to fix the problem // =============> write your new code here - - +function square(num) { + return num * num; +} +console.log(square(3)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..9aaa0eb72 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> there is no return,to return the value to the function call function multiply(a, b) { console.log(a * b); @@ -8,7 +8,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> the error is it is not printing out the function call in line 9. +// and returning is as undefined. // Finally, correct the code to fix the problem -// =============> write your new code here +// Functiion multiply(a,b){ +// return a*b +// } +// console.log (`The result of multiplying 10 and 32 is ${multiply(10, 32)}`) diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..a082469e4 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> return value is written before the expression. function sum(a, b) { return; @@ -8,6 +8,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> when the expression evaluates a value it will +// need a return statment,but the return statment is before the expression +// which will result in undefined outcome because the function that is passing +// an argument is not receiving output of the expression. // Finally, correct the code to fix the problem -// =============> write your new code here +// +// function sum(a, a){ +// return a+b; +//} +// console.log(`the sum of 10 and 32 is ${sum(10,32)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..aa7d03e84 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,48 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> I think the num variable can be accesed by the funtion getLastDigit() +// so it means it is not using the argument that is passed by the function. +// the output for the three of function calls will be 3. -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 output is + // * 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 is the way it is because it was taking the num value +// from the variable num that has been declared and initialized outside the function. +// and const num has a global scope.if the function hasn't declared a variable num +// inisde the function it will use the gobaly declared and insialized variable num // Finally, correct the code to fix the problem -// =============> write your new code here +// =============>function getLastDigit() { + 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. + // yes it does tell the last digit of each number + // 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 diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..51fe1866b 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,10 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + return (weight / (height* height)).toString().slice(0,4); + } +console.log(`Your BMI is ${calculateBMI(79,1.73)} `); + +//The out come is + // Your BMI is 26.3 \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..f6ea5d518 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/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 upperSnakeCase(string){ + + return string.toUpperCase().replaceAll(" ", "_") +} + console.log(upperSnakeCase("lord of the Rings.")); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..0bc732b95 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,19 @@ // 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){ + let withOutP = penceString.substring( 0,penceString.length - 1); + let paddedPenceNumberString = withOutP.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("502p")); + + +//output for 4050p is £40.50 +//output for 29p is £0.29 +//output for 502 is £5.02 \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..e9a1d1382 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,24 +15,35 @@ 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 pad() will be called // 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 is the value assigned to num. // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// =============> 00 is the value of pad when it was called for the first time. // 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 is the value of num when the pad is called for the last time. The last time pad is called is + // by by putting "remaining seconds" which is 1 in the function pad . // 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 is the return value of pad.when pad()is called with the value of num 1 the expression +// inside the pad function evaluates to a value 01. If we go through the code +// function pad(num) { + //let numString = num.toString();===> the value of numstring becomes "1" + //while (numString.length < 2) { ===> this line it is checking if numString.length< 2, numString.length is 1 in this line + //numString = "0" + numString; ===> in this line numString will be 01. and again will go to while again to check if + // } numString.length is <2 and it is not then pad() will return 01. + // + //