From bcb7b45955216ff4ac115c418b1a2d5b59c2a357 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 21:04:45 +0000 Subject: [PATCH 01/12] solution 0.js --- Sprint-2/debug/0.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/0.js b/Sprint-2/debug/0.js index b46d471a..55eb1a61 100644 --- a/Sprint-2/debug/0.js +++ b/Sprint-2/debug/0.js @@ -1,7 +1,10 @@ // Predict and explain first... function multiply(a, b) { - console.log(a * b); + return(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +//The function will multiply a and b. it will return undefined. + From b01689724426773c24a0e9ca599f199995822de8 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 21:08:36 +0000 Subject: [PATCH 02/12] 1.js --- Sprint-2/debug/1.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/1.js b/Sprint-2/debug/1.js index df4020ca..0cfc9e93 100644 --- a/Sprint-2/debug/1.js +++ b/Sprint-2/debug/1.js @@ -1,8 +1,11 @@ // Predict and explain first... function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// The function will return the sum of the two parameters a and b, which serves as placeholders for values. +// there was a syntax error + From c4a59eed54df48b4c6e990a983e095b1ecc922a6 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 21:44:45 +0000 Subject: [PATCH 03/12] 2.js --- Sprint-2/debug/2.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-2/debug/2.js b/Sprint-2/debug/2.js index bae9652a..57956480 100644 --- a/Sprint-2/debug/2.js +++ b/Sprint-2/debug/2.js @@ -2,7 +2,7 @@ const num = 103; -function getLastDigit() { +function getLastDigit(num) { return num.toString().slice(-1); } @@ -12,3 +12,9 @@ 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 function uses the method .tostring to convert the num to string +//then returns the value of index -1, that is the last value of the string. +//it wasn't working properly because the function has no parameter. + + From 9ad5c19ab37b4ba8604a58a81ee85fad59acba12 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 22:22:20 +0000 Subject: [PATCH 04/12] errors 0.js --- Sprint-2/errors/0.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sprint-2/errors/0.js b/Sprint-2/errors/0.js index 74640e11..cf784f22 100644 --- a/Sprint-2/errors/0.js +++ b/Sprint-2/errors/0.js @@ -1,9 +1,14 @@ // Predict and explain first... -// call the function capitalise with a string input +// call the function capitalize 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)}`; - return str; +function capitalize(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; + } + +console.log(capitalize("dog")); + +//SyntaxError: Identifier 'str' has already been declared +//The variable has been declared twice in the same scope. From 514f84ee848eb1eb1fd64bafc859af5bab14f274 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 22:26:54 +0000 Subject: [PATCH 05/12] errors 1.js --- Sprint-2/errors/1.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sprint-2/errors/1.js b/Sprint-2/errors/1.js index 4602ed23..ab546597 100644 --- a/Sprint-2/errors/1.js +++ b/Sprint-2/errors/1.js @@ -4,10 +4,14 @@ // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; + //const decimalNumber = 0.5; + return percentage = `${decimalNumber * 100}%`; - return percentage; + //return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); + +//I predict function will return 50% +// I was wrong : +// From 0514eb4f3699c193aa5f0e098c92d4dbd91039e5 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Thu, 5 Dec 2024 22:28:32 +0000 Subject: [PATCH 06/12] errors 2.js --- Sprint-2/errors/2.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/errors/2.js b/Sprint-2/errors/2.js index 814334d9..c72cdec5 100644 --- a/Sprint-2/errors/2.js +++ b/Sprint-2/errors/2.js @@ -3,8 +3,10 @@ // this function should square any number but instead we're going to get an error -function square(3) { +function square(num) { return num * num; } +console.log(square(2)); + From 0b9823626a76ac95cd322445945883d1c106a2f2 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 14:11:03 +0000 Subject: [PATCH 07/12] added test cases / fixed bugs / updated the function --- Sprint-2/extend/format-time.js | 65 +++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/Sprint-2/extend/format-time.js b/Sprint-2/extend/format-time.js index f3b83062..93b296e2 100644 --- a/Sprint-2/extend/format-time.js +++ b/Sprint-2/extend/format-time.js @@ -3,11 +3,27 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const min = time.slice(3, 5); + if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; -} + const hoursPm = hours - 12 + //padding with 0 to make sure the string length is 2 + const adjustedHoursPm = String(hoursPm).padStart(2,0); + return `${adjustedHoursPm}:${min} pm`; + + } else if (hours === 12){ + return `12:${min} pm`; + + }else if (hours === 00){ + return `12:${min} am`; + + } + //padding with 0 to make sure the string length is 2 + const adjustedHoursAm = String(hours).padStart(2,0); + return `${adjustedHoursAm}:${min} am`; +}; + + const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; @@ -22,3 +38,44 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// test to check function formatAs12HourClock works for 12:00 +const currentOutput3 = formatAs12HourClock("12:00"); + +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +// test to check function formatAs12HourClock works for 00:00 +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +// test to check function formatAs12HourClock works for time thats not on the hour during am time +const currentOutput5 = formatAs12HourClock("06:35"); +const targetOutput5 = "06:35 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +// test to check function formatAs12HourClock works for time thats not on the hour during pm time +const currentOutput6 = formatAs12HourClock("20:45"); +const targetOutput6 = "08:45 pm"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}` +); + +// test to check function formatAs12HourClock works for time thats not on the hour during pm time +const currentOutput7 = formatAs12HourClock("00:45"); +const targetOutput7 = "12:45 am"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}` +); \ No newline at end of file From 4a4a314471ff169a96215f1dc507930ef7868830 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 15:28:54 +0000 Subject: [PATCH 08/12] solution bmi.js --- Sprint-2/implement/bmi.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d4..bb033022 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -13,3 +13,19 @@ // Given someone's weight in kg and height in metres // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place + +function calBmi(weightkg, heightM) { + const bmi = weightkg / (heightM * heightM); + return Number(bmi.toFixed(1)); +} + +//console.log(12.46.toFixed(1)); + +// test1 to check the example test case +const currentOutput1 = calBmi(70, 1.73); +const targetOutput1 = 23.4; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}` +); + From 5a634654ecc71fa2a3203950885617dca260673d Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 15:46:12 +0000 Subject: [PATCH 09/12] solution cases.js --- Sprint-2/implement/cases.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sprint-2/implement/cases.js b/Sprint-2/implement/cases.js index 9e56a27b..98a99bac 100644 --- a/Sprint-2/implement/cases.js +++ b/Sprint-2/implement/cases.js @@ -13,3 +13,15 @@ // You will need to come up with an appropriate name for the function // Use the string documentation to help you find a solution + +function toUpperSnakeCase(str) { + const upperCase = str.toUpperCase() + return upperCase.replaceAll(" ", "_"); +} + +const sentence = 'fat dog' +console.log(toUpperSnakeCase(sentence)); + +const sentence1 = 'lord of the rings' +console.log(toUpperSnakeCase(sentence1)); + From 43de2f54bea78fd8853ea3cea67c55eb7c7ea6b8 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 16:44:18 +0000 Subject: [PATCH 10/12] solution to to-pounds --- Sprint-2/implement/to-pounds.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Sprint-2/implement/to-pounds.js b/Sprint-2/implement/to-pounds.js index 6265a1a7..248a6dde 100644 --- a/Sprint-2/implement/to-pounds.js +++ b/Sprint-2/implement/to-pounds.js @@ -4,3 +4,49 @@ // 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 toPound(amountInPence){ + // removes p + const penceStringWithoutTrailingP = amountInPence.substring(0, amountInPence.length - 1); + // makes sure the length is 3 + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + //returns the pounds from the string i.e the first number in the string + const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + // returns a the pence, i.e the substring(length-2) then pads it with 0 to make sure length is 2 + const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + +return (`£${pounds}.${pence}`); +} + +// test to check function penceToPound 1digit input +const currentOutput = toPound("30p"); +const targetOutput = "£0.30"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); + +// test to check function penceToPound 3digit input +const currentOutput1 = toPound("454p"); +const targetOutput1 = "£4.54"; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}` +); + +// test to check function penceToPound 1digit input +const currentOutput2 = toPound("4p"); +const targetOutput2 = "£0.04"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}` +); + +// test to check function penceToPound 0 edge case +const currentOutput3 = toPound("0p"); +const targetOutput3 = "£0.00"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + From 0f40c216906ea24195185ef8214f698ce2697f68 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 18:30:47 +0000 Subject: [PATCH 11/12] solution vat.js --- Sprint-2/implement/vat.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb16722..df6bc531 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,12 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on + +function addVat(price){ + const vat = 0.2; + const priceWithoutPoundSign = Number(price.substring(1)); + return `£${(vat * priceWithoutPoundSign) + priceWithoutPoundSign}`; +} + +console.log(addVat("£50")); + From 9513daef2aa835a35d309f38aad165718965d2b3 Mon Sep 17 00:00:00 2001 From: fatima safana Date: Sat, 7 Dec 2024 19:22:02 +0000 Subject: [PATCH 12/12] solution time-format.js --- Sprint-2/interpret/time-format.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/interpret/time-format.js b/Sprint-2/interpret/time-format.js index c5a0c161..ac5d7262 100644 --- a/Sprint-2/interpret/time-format.js +++ b/Sprint-2/interpret/time-format.js @@ -2,6 +2,8 @@ function pad(num) { return num.toString().padStart(2, "0"); } +console.log(pad(2)); + function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; const totalMinutes = (seconds - remainingSeconds) / 60; @@ -13,19 +15,26 @@ function formatTimeDisplay(seconds) { )}`; } +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? +//3 // 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? +//0 // c) What is the return value of pad is called for the first time? +//00 // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +//1 the value of remaining seconds is 1 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer +//01 the value of remainingSeconds is padded with length 2 and 0 making it 01