diff --git a/.gitignore b/.gitignore index bde36e530..ca75f83ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .DS_Store .vscode +testing.js --- IGNORE --- **/.DS_Store \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..1090b27dd 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,24 @@ // Predict and explain first... -// =============> write your prediction here +// The code has a function that is supposed to capitalise the string passed to it. (only the first letter) +// the string will be stored in the variable str and the function will return its value. +// it takes the first letter (str[0]) and uses toUpperCase() method to capitalise it. +// the str.slice(1) method is used to show the letter. +// // 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 +// an error occured because the variable str was already declared as a parameter of the function so +// there was no need to use let again. + + // =============> write your new code here +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("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 f2d56151f..9892dae5f 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,11 @@ // Predict and explain first... +// the code is supposed to convert a decimal number to percentage by multiplying +// the decimal Number by 100 then add the sign % to show that it is a percentage // Why will an error occur when this program runs? // =============> write your prediction here +// an error will occur because the variable decimalNumber is redeclared inside the function. +// it's already declared as a parameter of the function, so redeclaring it with const causes a syntax error. // Try playing computer with the example to work out what is going on @@ -15,6 +19,18 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// the fucntion convertToPercentage has a parameter named decimalNumber, +// but inside the function, there is a line that tries to declare a new constant with the same name decimalNumber. +// there is no need to redeclare the variable or edit it since it is a user input. +// also the variable decimalNumber is called from outside the function which menas from another scope. +// that is wrong because the variable can only be used inside the fucntion. instead of that we need to call the funtion, not hte variable. // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(0.3)); \ 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 aad57f7cf..e95d45601 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,32 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// the error will occur because the parameter name is a number (3) which is not allowed. +// it should be a variable or empty. function square(3) { return num * num; } // =============> write the error message here - +// SyntaxError: Unexpected number +// at internalCompileFunction (node:internal/vm:73:18) +// at wrapSafe (node:internal/modules/cjs/loader:1274:20) +// at Module._compile (node:internal/modules/cjs/loader:1320:27) +// at Module._extensions..js (node:internal/modules/cjs/loader:1414:10) +// at Module.load (node:internal/modules/cjs/loader:1197:32) +// at Module._load (node:internal/modules/cjs/loader:1013:12) +// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) +// at node:internal/main/run_main_module:28:49 + +// Node.js v18.19.1 // =============> explain this error message here - +// ther error message says that a number can not put as an input when declaring the function // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(5)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..3bcfca9bf 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... - // =============> write your prediction here +// this code will use the declared function multiplay (a, b) to multiply two numbers, 10 and 32, and print the result to the console. +// However, the function can not be used to get the result because it does not return any value. Instead, it only logs the result to the console. function multiply(a, b) { console.log(a * b); @@ -9,6 +10,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// the function does not return any value, it only prints the result to the console, so it can't be used to return anything, it will return "undefined". // 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 37cedfbcf..d91b81184 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// the function will throw an error because it does not return any value, the use of return without passing any value will cause an error. +// also the last line of the function is unreachable because it comes after the return statement. function sum(a, b) { return; @@ -9,5 +11,13 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The function will not return the expected sum because the return statement is not followed by any value. + // 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 57d3f5dc3..56a33f0d1 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,15 @@ // Predict and explain first... +// the code should display the last digit of any number passed to the function. +// but there is an error which is the variable num is globally declared and has a value of 103. +// the function does not take any parameters, and it takes the global variable num which is always 103. +// that causes the code to always return 3 as 3 is the last digit of 103 which is the value of the global variable num. // Predict the output of the following code: // =============> Write your prediction here +// since 3 is the last digit of 103, I think the output will be: +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 const num = 103; @@ -14,11 +22,29 @@ 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 +// The last digit of 42 is 3 ... the output is as I predicted. + // =============> write the output here +// 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 +// because the function getLastDigit does not take any parameters, and it uses the global variable num which is always 103. // Finally, correct the code to fix the problem // =============> write your new code here +// no need to declare the global variable num here. instead, we will pass it as a parameter to the function. +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 +// because the function does not take the parameter so it will always use the variable num which is always 103. \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..a8c508bdd 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,13 @@ // Then when we call this function with the weight and height // It should return their Body Mass Index to 1 decimal place -function calculateBMI(weight, height) { +function calculateBmi(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + let squaredHeight = height * height; + let bmi = weight/squaredHeight; + bmi = bmi.toFixed(1); + return Number(bmi); +} + +console.log(calculateBmi(70, 1.73)); +//console.log(calculateBmi(70, 1.73) + 5); \ 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 5b0ef77ad..810feb63f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,20 @@ // 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 toUpperSnakeCase(string) +{ + let output = ''; + for(let i = 0; i < string.length; i++) + { + let char = string[i]; + if(char === " ") + { + char = "_"; + } + output += char; + } + return output.toUpperCase(); +} + +console.log(toUpperSnakeCase("Hello there, How are you doing guys?")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..787ffc088 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,29 @@ // 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"); + + return (`£${pounds}.${pence}`); +} + +console.log(toPounds("5764p")); +console.log(toPounds("813p")); +console.log(toPounds("90897867564p")); +console.log(toPounds("71p")); +console.log(toPounds("312987p")); \ 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 7c98eb0e8..4d278fa7c 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,7 @@ 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 @@ -18,17 +19,23 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// The function 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 is 0 // c) What is the return value of pad is called for the first time? // =============> write your answer here +// # The Python visualizer stops working when it runs pad because (padStart is not a function), maybe because it uses ES6 version of javascript! +// The return value of pad when it's called for the first time is String "00" // 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 is 1 . the value 1 is the number of the remaining seconds, pad() converted it to 01 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here +// the return value is 01 . the function pad() converted num (which values 1) to 01 so the seconds can be readable easily. \ No newline at end of file diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..7978b5a62 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,23 +3,60 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + let hours = Number(time.slice(0, 2)); + let minutes= time.slice(3, 5); + let timeIndicator; + + if (hours >= 12) { + hours = hours - 12; + timeIndicator = "pm"; + } else { + timeIndicator = "am"; } - return `${time} am`; + if ( hours === 0) + hours = 12; + + return `${String(hours).padStart(2, "0")}:${minutes} ${timeIndicator}`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +let currentOutput = formatAs12HourClock("14:00"); +let targetOutput = "02:00 pm"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +//========================== +currentOutput = formatAs12HourClock("23:00"); +targetOutput = "11:00 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +//========================== +currentOutput = formatAs12HourClock("07:51"); +targetOutput = "07:51 am"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +//========================== +currentOutput = formatAs12HourClock("00:37"); +targetOutput = "12:37 am"; console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +//========================== +currentOutput = formatAs12HourClock("12:37"); +targetOutput = "12:37 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` +); +//========================== +currentOutput = formatAs12HourClock("20:59"); +targetOutput = "08:59 pm"; +console.assert( + currentOutput === targetOutput, + `current output: ${currentOutput}, target output: ${targetOutput}` ); diff --git a/testing.js b/testing.js new file mode 100644 index 000000000..146392abf --- /dev/null +++ b/testing.js @@ -0,0 +1,23 @@ +function formatAs12HourClock(time) { + if (Number(time.slice(0, 2)) > 12) + { + return `${Number(time.slice(0, 2) - 12)}:00 pm`; + } + return `${time} am`; +} + +let targetOutPut = "2:00 pm"; +let currentOutPut= formatAs12HourClock("14:00"); + +console.assert( + targetOutPut === currentOutPut, + `current output: ${currentOutPut}, target output: ${targetOutPut}` +); +// ========= +targetOutPut = "11:00 am"; +currentOutPut= formatAs12HourClock("11:00"); + +console.assert( + currentOutPut === targetOutPut, + `Error,, current is ${currentOutPut} and target is ${targetOutPut}` +); \ No newline at end of file