diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..8f466e809 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -11,3 +11,20 @@ function capitalise(str) { // =============> write your explanation here // =============> write your new code here + +///The error is : + +let str = `${str[0].toUpperCase()}${str.slice(1)}`; + +//str is already a parameter of the function. +//Doing let str = ... is like giving it a new name in the same place, which JavaScript does not allow. +//we can just use the existing str instead of declaring it again. + +function capitalise(str) { + + // make the first letter uppercase + + str = str[0].toUpperCase() + str.slice(1); + + return str; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..8cad2edf7 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -9,12 +9,34 @@ function convertToPercentage(decimalNumber) { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; - return percentage; + return percentage;= } - console.log(decimalNumber); // =============> write your explanation here +Answer: + +The number (0.5) goes into the function — that’s our decimalNumber. + +We use that decimalNumber to make a percent. + +We don’t write const decimalNumber again because we already have it. + +We return the answer (like "50%"). + +Then we call the function with 0.5 and print what it gives back. + // 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)); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..e14f62d24 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -6,15 +6,33 @@ // =============> write your prediction of the error here function square(3) { + return num * num; } - // =============> write the error message here +SyntaxError: Unexpected number + + // =============> explain this error message here +the function takes the number we give it (3) and times it by itself. + +so 3 * 3 = 9. + +it works now cuz we used num instead of a number in the () + + // 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 b27511b41..eadee6a4e 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -10,5 +10,17 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +the function just prints the answer, it dont return it. + +so when we try to use it inside the message it shows 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)}`) + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..5a14317ae 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -9,5 +9,18 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +the code dont work cuz the return stops the code before it adds the numbers. + +so js never does a + b, that’s why it show undefined. + // 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)}`) + diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..376171437 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,11 +1,19 @@ // Predict and explain first... +it will print 3 for all of them + // Predict the output of the following code: + +The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3 + // =============> Write your prediction here const num = 103; function getLastDigit() { + return num.toString().slice(-1); } @@ -15,9 +23,35 @@ 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 last digit of 42 is 2 + +The last digit of 105 is 5 + +The last digit of 806 is 6 + // Explain why the output is the way it is // =============> write your explanation here + +It always shows 3 cuz the function just uses the num at the top (103). + +It ignores the numbers we put in () like 42, 105, 806. + +We need to let the function take a number as input. + // Finally, correct the code to fix the problem + +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)}) + // =============> write your new code here // This program should tell the user the last digit of each number. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..40de57413 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,18 @@ // 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 +} + + +function calculateBMI (weight,height) { + + const bmi = weight / (height*height) + + return Math.round(bmi * 10) / 10; + + } + + console.log(calculateBMI(70,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 5b0ef77ad..767f25910 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,18 @@ // 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 that turns text into UPPER_SNAKE_CASE + +function makeUpperSnake(text) { + + let newText =text.replace(/ /g,'_'); + + newText = newText.toUpperCase(); + + return newText; + + } + console.log(makeUpperSnake("hello there")); + console.log(makeUpperSnake("lord of the rings")); + + diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..49a148267 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,28 @@ // 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) { + + let numberStr =penceString.substring(0,penceString.length -1); + + numberStr = numberStr.padStart(3,"0"); + + let pounds = numberStr.substring(0, numberStr.length -2); + + let pence = numberStr.substring(numberStr.length -2).padEnd(2,"0"); + + return "£" +pounds + "." +pence; + } + +console.log(toPounds("399p")); +console.log(toPounds("50p")); +console.log(toPounds("5p")); +console.log(toPounds("1200p")); + + + + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..49c577234 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -19,16 +19,73 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here -// Call formatTimeDisplay with an input of 61, now answer the following: +Answer: 3 + +return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; + +pad is called three times: for totalHours, remainingMinutes, and remainingSeconds. + +So no matter what input we give to formatTimeDisplay, pad will always 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 +Answer; + +The first call is pad(totalHours). + +const totalHours = (totalMinutes - remainingMinutes) / 60; + +The value assigned to num when pad is called for the first time is 0. +Consequently, pad(0) will return '00', which ensures that the hour is formatted correctly in the final output string + + // c) What is the return value of pad is called for the first time? // =============> write your answer here +Answer; 1 + +return num.toString().padStart(2, "0"); + +num = 1 + +num.toString() = "1" + +"1".padStart(2, "0") = "01" + +If you call pad(0), the return value will be '00'. + +If you call pad(1), the return value will be '01'. + +If you call pad(5), the return value will be '05'. + +If you call pad(10), the return value will be '10' + + + // 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 +Answer; 1 + +The last call is pad(remainingSeconds). + +When you call format_time_display(61): + +The calculations result in: + +total_hours = 0 +remaining_minutes = 1 +remaining_seconds = 1 + // 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 +Answwer; 01 + + +Third Call: pad(remaining_seconds) (where remaining_seconds = 1) + +Value of num for the last call: 1 + +Return value: '01' diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..826e7b184 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,22 +3,36 @@ // 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) { + // Expect time in "HH:MM" format const hours = Number(time.slice(0, 2)); + const mins = time.slice(3); + + if (hours === 0) { + return `12:${mins} am`; + } + + if (hours === 12) { + return `12:${mins} pm`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + const newHr = hours - 12; + // pad single digits like 1:00 -> 01:00 + return `${String(newHr).padStart(2, "0")}:${mins} pm`; } + return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +let currentOutput = formatAs12HourClock("08:00"); +let targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}` ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +let currentOutput2 = formatAs12HourClock("23:00"); +let targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}`