diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..5872c36d47 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,27 @@ // Predict and explain first... // =============> write your prediction here +// I don't think this will work because str is already an argument in the capitalise function +// so declaring it again is incorrect. Another thing is the variable str is being declared but the value assigned to +// it basically uses the str variable to interpolate the string // 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)}`; return str; } -// =============> write your explanation here +// =============> write your explanation here// +// SyntaxError: Identifier 'str' has already been declared +// this is happening because the str variable already exist as an argument in this function // =============> write your new code here + +function capitalize(str){ + let car = `${str[0].toUpperCase()}${str.slice(1)}`; + return car; +} +capitalize("hello") +console.log(capitalize("hello")) \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..341a1cb24f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -5,16 +5,23 @@ // 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); +// console.log(decimalNumber); // =============> write your explanation here - +// Prediction decimalNumber is already used as an argument so the variable name needs to be different // Finally, correct the code to fix the problem // =============> write your new code here +function ConvertToPercentage(decimalNumber){ + // const decimalNum = 0.5; + const percentage = `${decimalNumber* 100}%`; + return percentage; +} + +console.log(ConvertToPercentage(0.5)) \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..05506f5cba 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,6 +1,7 @@ // Predict and explain first BEFORE you run any code... - +// It will probably not run because the value 2 is a fixed integer, your parameters / arguments get values +// assigned to them so you can't really reassign 2. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here @@ -10,11 +11,14 @@ function square(3) { } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here - +// This is a syntax error the number 2 cannot be used as a parameter since it cannot be reassigned. // 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-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..d949242abf 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,7 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +// This function is not returning anything function multiply(a, b) { console.log(a * b); } @@ -9,6 +9,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// This function is not returning anything. It does multiply the assigned values of a,b but it does not return the String with the +// as required on line 9 // 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..121b7c923a 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here - +// this function does not return anything since the return statements has a semicolon before the command of what +// it has to actually return function sum(a, b) { return; a + b; @@ -11,3 +12,8 @@ console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..58fd1d0b4a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// the variable num is declared outside of the function so it's a global variable +// and it is constant. It should be a parameter or an argument of the getLastDigit function // Predict the output of the following code: // =============> Write your prediction here @@ -15,10 +16,18 @@ 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 3 for everything // Explain why the output is the way it is +// firstly because num is constant and it is a global variable outside the function. The last digit on 103 remains 3. It does not change // =============> write your explanation here // 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 17b1cbde1b..b1a48db7fb 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let bmi = weight/(height *height) + return `${bmi.toFixed(1)}` // return the BMI of someone based off their weight and height -} \ No newline at end of file +} +console.log(calculateBMI(80 , 1.73)) \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..28e52239be 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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(str) { + return `${str.toUpperCase().replaceAll(" ", "_")}`; +} +console.log(UpperSnakeCase("hello world in javascript")); \ 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 6265a1a703..7654249c25 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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 result = `£${pounds}.${pence}`; +return result +} +console.log(toPounds("399p")) \ 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 17127bc01e..03de3841fe 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -15,24 +15,34 @@ 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 +// pad will be called 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 value assigned to num when pad is called for the first +// time is 0 (the value of totalHours). // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// The return value of pad when it is called for the first time is "00" +// because 0 is padded with a leading zero. // 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 +//The value assigned to num when pad is called for the +// last time is 1 (the value of remainingSeconds). +// This is because the return statement is executed from left to right, so the order of pad calls is: totalHours, remainingMinutes, and finally remainingSeconds. + + // 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 +//The return value of pad when it is called for the last time is "01" because 1 is padded with a leading zero. + +// The final output of formatTimeDisplay(61) will be "00:01:01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..af50c78ea5 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,23 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +const currentOutput3 = formatAs12HourClock("14:00"); +const targetOutput = "02:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAsHourClock("00:00"); +const targetOutput4 = "12:00 am"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAsHourClock("17:00"); +const targetOutput5 = "05:00 pm"; +console.assert( + currentOutput5 === targetOutput5, + `currentOutput: ${currentOutput5}, target output: ${targetOutput5}` +);