diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039f..3cd8d38ea 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-1/errors/0.js @@ -1,2 +1,6 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? + +// add comets for Line use // +/* comments for many lines +can use */ \ No newline at end of file diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b6..d71b8f1c3 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -1,6 +1,6 @@ let count = 0; -count = count + 1; +count++;; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing diff --git a/Sprint-2/debug/0.js b/Sprint-2/debug/0.js index b46d471a8..58dadd2d6 100644 --- a/Sprint-2/debug/0.js +++ b/Sprint-2/debug/0.js @@ -1,7 +1,32 @@ // Predict and explain first... +/*it will print +the first console.log (a * b) which will call the function inputs 10, 32 = 320 function multiply(a, b) { console.log(a * b); } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +DEBUG*/ + +//The result of multiplying 10 and 32 is 320 + +function multiply(a, b) { + return a * b; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +// The function wasn't called initially, so it didn't display the result. +// //However, it has now been called and is returning the result. + + + + + + + + + + diff --git a/Sprint-2/debug/1.js b/Sprint-2/debug/1.js index df4020cae..043381039 100644 --- a/Sprint-2/debug/1.js +++ b/Sprint-2/debug/1.js @@ -1,8 +1,23 @@ // Predict and explain first... +/*It will print a error as return is in one line and Javasript will Not +read any code after that line + + function sum(a, b) { return; a + b; } +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/ + +//FIX:// + +function sum(a, b) { + return a + b; +} + console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +//The result wasn't returned because the sum of a and b wasn't included. +//Now I have fixed the code, and it is returning the result diff --git a/Sprint-2/debug/2.js b/Sprint-2/debug/2.js index bae9652a8..54ca44edc 100644 --- a/Sprint-2/debug/2.js +++ b/Sprint-2/debug/2.js @@ -1,6 +1,34 @@ // Predict and explain first... -const num = 103; +//const num = 103; + +function getLastDigit(numLast) { + return numLast.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. + the function doesn’t have a parameter to receive the num variable it’s trying to process. +// Explain why getLastDigit is not working properly - correct the problem +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)}`); + + + +function getLastDigit() { + return num.toString().slice(-1); +}*/ +// FIX + +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -10,5 +38,13 @@ 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 +function getLastDigit() { + return num.toString().slice(-1); +} + +gisdellabella@Giss-MBP debug % node 2.js +The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3 +42 +gisdellabella@Giss-MBP debug */ \ No newline at end of file diff --git a/Sprint-2/errors/0.js b/Sprint-2/errors/0.js index 74640e118..9277c64bb 100644 --- a/Sprint-2/errors/0.js +++ b/Sprint-2/errors/0.js @@ -3,7 +3,23 @@ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring -function capitalise(str) { +/*function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } + +Error: SyntaxError: Identifier 'str' has already been declared + +Fix:*/ + +function capitalise(capital) { + let str = `${capital.toUpperCase()}${capital.slice(1)}`; + return str; +} + +console.log(`${capitalise(" change this text to uppercase " )}`); + + +/*gisdellabella@Giss-MBP errors % node 0.js + CHANGE THIS TEXT TO UPPERCASE change this text to uppercase +gisdellabella@Giss-MBP errors %*/ \ No newline at end of file diff --git a/Sprint-2/errors/1.js b/Sprint-2/errors/1.js index 4602ed237..18a9a8372 100644 --- a/Sprint-2/errors/1.js +++ b/Sprint-2/errors/1.js @@ -3,11 +3,16 @@ // Why will an error occur when this program runs? // Try playing computer with the example to work out what is going on + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(0.5); + +// const decimalNumber = 0.5; +// ERROR: SyntaxError: Identifier 'decimalNumber' has already been declared + diff --git a/Sprint-2/errors/2.js b/Sprint-2/errors/2.js index 814334d9e..2742fd7d4 100644 --- a/Sprint-2/errors/2.js +++ b/Sprint-2/errors/2.js @@ -1,10 +1,13 @@ // Predict and explain first... -// this function should square any number but instead we're going to get an error + function square(num) { +return num * num; -function square(3) { - return num * num; -} + } + console.log (square (3)) + +//The error was with parameter 3, which always gave me the same result. +// By changing it to "num," the function now works with any number. Also 3 is used as the parameter name for the function, which is not valid in JavaScript. diff --git a/Sprint-2/extend/format-time.js b/Sprint-2/extend/format-time.js index f3b83062d..5ec03c47b 100644 --- a/Sprint-2/extend/format-time.js +++ b/Sprint-2/extend/format-time.js @@ -22,3 +22,52 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("19:00"); +const targetOutput3 = "7:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + + +const currentOutput4 = formatAs12HourClock ("03:00"); +const targetOutput4 = "03:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` + ); + + const currentOutput5 = formatAs12HourClock ("01:00"); + + const targetOutput5= "01:00 am"; + + console.assert( + currentOutput5 === targetOutput5, + `curent output: ${currentOutput5}, target output: ${targetOutput5}` + ) + + const currentOutput6 = formatAs12HourClock ("04:00"); + const targetOutput6= "04:00 am"; + console.assert( + currentOutput6 === targetOutput6, + `curent output: ${currentOutput6}, target output: ${targetOutput6}` + ) + + + const currentOutput7 = formatAs12HourClock("22:00") + const targetOutput7 = "10:00 pm"; + + console.assert( + currentOutput7 === targetOutput7, + `curent output: ${currentOutput7}, target output: ${targetOutput7}` + + ) + + const currentOutput9 = formatAs12HourClock("00:00") + const targetOutput9 = "00:00 am"; + console.assert( + currentOutput9 === targetOutput9, + `curent output: ${currentOutput9}, target output: ${targetOutput9}` + + ) \ No newline at end of file diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d48..28488fee0 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -13,3 +13,21 @@ // 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 + + +// const weight = 150; +// const height = 170 / 100; //tranforme centimeter to meter + + +function ibmCalculator( weight, height) { + + height = height / 100; + let calculateIBM = weight / Math.pow(height, 2); + let result = calculateIBM.toFixed(1); //chang for decimal + return result; +} + + +console.log(`Your Body Mass Index (BMI) is ${ibmCalculator(98,126)}`); + + diff --git a/Sprint-2/implement/cases.js b/Sprint-2/implement/cases.js index 9e56a27b6..0395c0f94 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 + + +// my fucntion + +function upperSnakeCase(upperCaseTexts) { + return upperCaseTexts.toUpperCase().replace(/\s+/g, '_'); +} + +console.log(`${upperSnakeCase("hello there")}`); + +//gisdellabella@Giss-MBP implement % node cases.js +// HELLO_THERE \ No newline at end of file diff --git a/Sprint-2/implement/to-pounds.js b/Sprint-2/implement/to-pounds.js index 6265a1a70..96a2282c5 100644 --- a/Sprint-2/implement/to-pounds.js +++ b/Sprint-2/implement/to-pounds.js @@ -4,3 +4,57 @@ // 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"); +} +const penceString = "399p"; +console.log(`£${pounds}.${pence}`);*/ + +function toPounds(penceString) { + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); // Remove the "p" + + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // make sure number string has at least 3 digits + + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); // Extract pounds and pence + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return `£${pounds}.${pence}`; +} // Return the formatted string + +const result1 = toPounds("799p"); +const result2 = toPounds("50p"); +const result3 = toPounds("5230p"); +const result4 = toPounds("545p"); +console.log(result1); +console.log(result2); +console.log(result3); +console.log(result4); + +gisdellabella@Giss-MBP implement % node to-pounds.js +£7.99 +£0.50 +£52.30 +£5.45 \ No newline at end of file diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb167226..1eec2cde2 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,15 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on + +function addPercentageToPrice(price) { + let vat = price * 1.2; + return vat; +} + +let price = 675; +console.log(`Product total price is ${addPercentageToPrice(price)}`); + +//let price = 30; Product total price is 36 +//let price = 45; Product total price is 54 +//let price = 45; Product total price is 810 diff --git a/Sprint-2/interpret/myexc2.js b/Sprint-2/interpret/myexc2.js new file mode 100644 index 000000000..e0735b3b6 --- /dev/null +++ b/Sprint-2/interpret/myexc2.js @@ -0,0 +1,50 @@ +/*function formatAs12HourClock() {} + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "08:00 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +);*/ + + +/*function formatAs12HourClock(time) { + return `${time} am`; +} + +const currentOutput = formatAs12HourClock("09:00"); +const targetOutput = "09:00 am"; + +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +);*/ + +function formatAs12HourClock(time) { + //time format hh:mm in 24h time + + let hours = time[0] + time[1]; + let mint = time[3] + time[4]; // not count the colon + hours = parseInt(hours); + hours = hours % 12; + hours = hours.toString(); + return hours + ":" + mint ; + + +} + +console.log(formatAs12HourClock("15:34")) + + + +//Console assert practice: + +// const currentOutput7 = formatAs12HourClock("22:00") +// const targetOutput7 = "10:00 pm"; + +// console.assert( +// currentOutput7 === targetOutput7, +// `curent output: ${currentOutput7}, target output: ${targetOutput7}` + +// ) + diff --git a/Sprint-2/interpret/time-format.js b/Sprint-2/interpret/time-format.js index c5a0c1619..06dacb2b3 100644 --- a/Sprint-2/interpret/time-format.js +++ b/Sprint-2/interpret/time-format.js @@ -1,17 +1,16 @@ -function pad(num) { - return num.toString().padStart(2, "0"); +/*function pad(num) { + return num.toString().padStart(2, "0");// convert to a string take 2 caracters if short add "0" to the string } function formatTimeDisplay(seconds) { - const remainingSeconds = seconds % 60; - const totalMinutes = (seconds - remainingSeconds) / 60; - const remainingMinutes = totalMinutes % 60; - const totalHours = (totalMinutes - remainingMinutes) / 60; + //This function converts a time duration (in seconds) into a formatted string in the format "HH:MM:SS" (hours, minutes, seconds). + const remainingSeconds = seconds % 60;// Finds the leftover seconds that don’t make up a full minute. % (modulus operator) returns the remainder when seconds is divided by 60. + const totalMinutes = seconds - remainingSeconds; //Converts the remaining seconds into full minutes by subtracting remainingSeconds and dividing by 60. + const remainingMinutes = totalMinutes % 60; //Converts the remaining seconds into full minutes by subtracting remainingSeconds and dividing by 60. + const totalHours = totalMinutes - remainingMinutes; - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad( - remainingSeconds - )}`; -} + return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; +} */ // 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 @@ -19,13 +18,50 @@ function formatTimeDisplay(seconds) { // 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? +//totalHours = 0 (calculated as (totalMinutes - remainingMinutes) / 60). +//pad is called first with totalHours. +// the value assigned to num is 0. // c) What is the return value of pad is called for the first time? +//The first call to pad receives num = 0. +//Inside pad, 0 is converted to a string: "0". +//.padStart(2, "0") ensures it is at least 2 characters long by adding a leading zero: "00". +//The return value is "00" // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +//The last call to pad is for remainingSeconds, which is 1: +//remainingSeconds = seconds % 60. +//With seconds = 61, remainingSeconds = 61 % 60 = 1. +// the value assigned to num 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 +//The last call to pad receives num = 1. +//Inside pad, 1 is converted to a string: "1". +//.padStart(2, "0") ensures it is at least 2 characters long by adding a leading zero: "01". +// return value is "01" + +function pad(num) { + return num.toString().padStart(2, "0"); +} + +function formatTimeDisplay(seconds) { + const remainingSeconds = seconds % 60; + const totalMinutes = (seconds - remainingSeconds) / 60; + const remainingMinutes = totalMinutes % 60; + const totalHours = (totalMinutes - remainingMinutes) / 60; + + return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; +} + +//console.log(formatTimeDisplay(61)); +//00:01:01 +//gisdellabella@Giss-MBP interpret % + +console.log(formatTimeDisplay(52)); +00:00:52 +gisdellabella@Giss-MBP interpret %