diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..78b6084db 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -5,9 +5,19 @@ // 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; + let str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; } // =============> write your explanation here +// The function parameter declares the variable string in the first line and then the variable is declared again using let. This will not run because you cannot declare a variable with the same name twice. +// The return returns the same variable making the funtion useless. +// The is no console.log() to print anything. // =============> write your new code here + +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} +let result = capitalise("str") +console.log(result); \ No newline at end of file diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..716791ac3 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -5,16 +5,27 @@ // 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 +//The function has already declared the decimalNumber variable. The same variable cannot be used or declared twice. +//The decimalNumber variable is only existing inside the the function. Therefore the console.log() will not have anything to print. +//The decimalNumber must be called outside first. + // 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.5)); \ No newline at end of file diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..085d9d06a 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -4,17 +4,25 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// The will be no output printed on the console because there is no code to instructing printing. +// The num variable is not defined and 3 is used instead of num function square(3) { return num * num; } // =============> write the error message here +SyntaxError: Unexpected number // =============> explain this error message here +// This error illustrates that a number was used instead of a variable name. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + squareNum = num * num; + return squareNum; +} +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..23dd1f961 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +//The code will run and print the outcoome of (a * b), however it will have 2 outputs. +// The first output is the console.log(a * b) and the second output will execute the console in line 11. function multiply(a, b) { console.log(a * b); @@ -9,6 +11,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// console.log is use to print the instead osf return such that the return value can be called at the end. // 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)}`); + diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..4a3139d2d 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +// The return and a + b are on 2 different lines leading to a syntax error. The sum fuctions does not work like this. function sum(a, b) { return; a + b; @@ -9,5 +9,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +//The code runs but gives undefined in the console when we call the sum(10, 32). The return function is not defined. We must define it a + b so that when we call it, it give the sum of the 2 values // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..2e1b4508d 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +// num is declared as a const variable of 103, this will give the same outcome because num is suppose to a placeholder not a number. const num = 103; function getLastDigit() { @@ -14,11 +14,20 @@ 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 -// =============> write the output here +// The output gives the same answer that is 3 + // Explain why the output is the way it is -// =============> write your explanation here +// The variable num was declared to a const number (103) instead of a placeholder therefore it cannot be changed // 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 +// The const variable must be removed from the code. The computer reads that first and gives the same output for all console.log statements. +// A placeholder variable for num must be declared in the function parameter. \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..a480c9457 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -14,6 +14,12 @@ // Then when we call this function with the weight and height // It should return a string of 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 +//} +function calculateBMI(weight, height) { + squareHieght = height * height; + calculateBMI = Math.round(((weight / squareHieght)) *10 )/10; + return calculateBMI } +console.log(calculateBMI(70, 1.73)) diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..b450427b9 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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(str) { + return str.replaceAll(" ", "_").toUpperCase(); +} +console.log(toUpperSnakeCase("hello world")) +console.log(toUpperSnakeCase("lord of the rings")) \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..3104bc846 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,15 @@ // 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("399p")) +console.log(toPounds("10p")) +console.log(toPounds("1023p")) +console.log(toPounds("5p")) \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..63dd8c20b 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// 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 +// 0 // c) What is the return value of pad when it is called for the first time? // =============> write your answer here - +// 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 - +// remainingSeconds is 1, the last pad will execute so the function will divide 61 by 60 and then return the modulus or remainder which is 1. // 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 +// "01", The pad function recieves the number 1 and converts it to a number string. +// The while loop checks the string length to get 1. 1 is less than 2 therefore the loop executes and adds a 0 to the string giving "01". +// The loop will finishes because 2 is not less than 2. The function returns "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..ed69dcc3b 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -2,24 +2,28 @@ // Make sure to do the prep before you do the coursework // 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`; + const timeString = String(time); + let [hours, minutes] = timeString.split(":").map(Number); + if (isNaN(minutes)){ + minutes = 0; + } + let period = "am" + if (hours >= 12) { + period = "pm"; + if (hours > 12) { + hours = hours - 12; + } + } else if (hours === 0) { + hours = 12; } - return `${time} am`; + const paddedHours = String(hours).padStart(2, "0"); + const paddedMinutes = String(minutes).padStart(2, "0"); + return `${paddedHours}:${paddedMinutes}${period}`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); - -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); +console.log(formatAs12HourClock("00:30")) +console.log(formatAs12HourClock("23:32")) +console.log(formatAs12HourClock("08:50")) +console.log(formatAs12HourClock("13:00"))