diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..272619f3 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -1,7 +1,7 @@ // Predict and explain first... - +// instead of console.lon inside the function return a*b function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..f569ce6e 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,7 @@ // Predict and explain first... - +// did not return value truly function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..610545de 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,8 +1,6 @@ // Predict and explain first... - -const num = 103; - -function getLastDigit() { +// pass a parameter inside the function so return num.toString().Slice(-1) that takes the last digit for the user +function getLastDigit(num) { return num.toString().slice(-1); } diff --git a/week-2/errors/0.js b/week-2/errors/0.js index 58b1349d..a4240482 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -1,9 +1,11 @@ // Predict and explain first... // write down the error you predict will be raised +// the error that I predict is that there is no function call secondly same name variable str used in 2 times // then call the function capitalise with a string input // interpret the error message and figure out why it's happening, if your prediction was wrong function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + let strs = `${str[0].toUpperCase()}${str.slice(1)}`; + return strs; } +console.log(capitalise("arslan")); \ No newline at end of file diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..de3efb52 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -1,13 +1,13 @@ // Predict and explain first... // Write down the error you predict will be raised +//the error is function call is not write correctly so i correct it // Why will an error occur when this program runs? // Play computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + // const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; - return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..55e5c9ba 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -2,9 +2,10 @@ // Predict and explain first... // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? - -function square(3) { +// yes i fix it pass a function parameter then call the parameter outside the function +function square(num) { return num * num; } +console.log(square(3)); diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..18785cea 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -13,3 +13,9 @@ // Given someone's weight in kg and height in metres // When we call this function with the weight and height // Then it returns their Body Mass Index to 1 decimal place +function weightBmI(weight,height){ + const heightSqu = height * height; + const Bmi = weight/heightSqu; + return Bmi.toFixed(1); +} +console.log(weightBmI(70,1.27)); \ No newline at end of file diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index f66bf7e3..5641bf89 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -15,3 +15,10 @@ // Come up with a clear, simple name for the function // Use the string documentation to help you plan your solution +function uprCaseString(str){ +const uprCase =str.toUpperCase(); +const uprCaseStringSlash = uprCase.replace(/ /g,"_"); +return uprCaseStringSlash; +} +console.log(uprCaseString("hi arslan")) +console.log(uprCaseString("A quick brown fox jump over a lazy dog")); \ No newline at end of file diff --git a/week-2/implement/to-pounds.js b/week-2/implement/to-pounds.js index 7add3d05..9a808f19 100644 --- a/week-2/implement/to-pounds.js +++ b/week-2/implement/to-pounds.js @@ -3,3 +3,9 @@ // Take this code and turn it into a reusable block of code. // Declare a function called toPounds with an appropriately named parameter. // Call this function a number of times to check it works for different inputs +function toPound(kilogram){ + const pound = kilogram * 2.20462; + return pound; +} +console.log(toPound(1)); +console.log(toPound(3)); \ No newline at end of file diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..4e053a94 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -8,3 +8,8 @@ // Given a number, // When I call this function with a number // Then it returns the new price with VAT added on +function Vat(price){ + const vatOnPrice = price * 1.2; + return vatOnPrice; +} +console.log(Vat(50)); \ No newline at end of file diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..d5ee614f 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -4,40 +4,42 @@ function pad(num) { } return num; } - function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; + console.log(remainingSeconds); const totalMinutes = (seconds - remainingSeconds) / 60; + console.log(totalMinutes); const remainingMinutes = totalMinutes % 60; + console.log(remainingMinutes); const totalHours = (totalMinutes - remainingMinutes) / 60; + console.log(totalHours); const remainingHours = totalHours % 24; + console.log(remainingHours); - return `${pad(remainingHours)}:${pad(remainingMinutes)}:${pad( - remainingSeconds - )}`; + return `${pad(remainingHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } - console.log(formatTimeDisplay(143)); // You can 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? - +// pad is called three times on function call once for Minute and second and hour // Call formatTimeDisplay with an input of 143, now answer the following: - +// 00:02:23 // b) What value is assigned to the parameter num when pad is called for the first time? - +// first time when call the value is 0 // c) What is the return value of pad when it is called for the first time? - +// return 0 // d) What is the value assigned to the parameter num when pad +// remainingSeconds = 143 % 60 results in 23. // is called for the last time in this program? Explain your answer - +// when call the last time value is 23 // e) What is the return value when pad is called +// 00:02:23 // for the last time in this program? Explain your answer - // f) Research an alternative way of padding the numbers in this code. // Look up the string functions on mdn