diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..dd7dfa253 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,22 @@ -// Predict and explain first... -// =============> write your prediction here - -// 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 new code here +// Predict and explain first... +// I am predicting the code will generate error because the variable str is declared twice + +// 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; +} +console.log(capitalise("hello World"));*/ + +// After we run the code, it generated this error SyntaxError: Identifier str has already been declared +// This happened because we tried to declare again using the keyword let the variable str +// which was declared before in the parameter of the function + +//Here is the fixed code +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} +console.log(capitalise("hello World")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..fd820ff79 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,32 @@ -// Predict and explain first... - -// Why will an error occur when this program runs? -// =============> write your prediction here - -// 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); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +// Predict and explain first... + +// Why will an error occur when this program runs? +// I predict the code will generate 2 errors because one the variable decimalNumber is declared twice and two +// the function is not called instead the variable decimalNumber is called and it can't be accessed because +// it was declared locally as a parameter of the function + +// 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);*/ + +// After we run the code, it generated an error SyntaxError: Identifier 'decimalNumber' has already been declared +// because we tried to declare decimalNumber again. After fixing this error again we got another error ReferenceError: decimalNumber is not defined +// because the decimalNumber is locally scoped, javascript can't access it when the function is called +// additionally the function was not called console.log(decimalNumber) this simply means print the value of decimalNumber + +// Finally, correct the code to fix the problem +// Here is the fixed code +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.5)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..dfdb0d395 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,21 @@ - -// Predict and explain first BEFORE you run any code... - -// this function should square any number but instead we're going to get an error - -// =============> write your prediction of the error here - -function square(3) { - return num * num; -} - -// =============> write the error message here - -// =============> explain this error message here - -// Finally, correct the code to fix the problem - -// =============> write your new code here - - +// Predict and explain first BEFORE you run any code... + +// this function should square any number but instead we're going to get an error + +// I predict the error will come from the argument because when we write a function we first declare parameters not values + +/*function square(3) { + return num * num; +}*/ + +// SyntaxError: Unexpected number + +// this error tells us javascript expects valid variable identifiers when function is defined not values + +// Finally, correct the code to fix the problem + +// Here is the fixed code +function square(num) { + return num * num; +} +console.log(square(3)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..50c37d9ea 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,22 @@ -// Predict and explain first... - -// =============> write your prediction here - -function multiply(a, b) { - console.log(a * b); -} - -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +// Predict and explain first... + +// I predict the code will run normally and print the result of a * b on the first console, but it will return undefined on the +// second console.log + +/*function multiply(a, b) { + console.log(a * b); +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/ + +// The reason for printing undefined is that we didn't define a return value so the js doesn't know what to return and it simply returns undefined +// We can fix the code by replacing the console.log with a return keyword so the js knows what to return when the function is called + +// Finally, correct the code to fix the problem +// Here is the fixed code + +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..d5c63b0e5 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ -// Predict and explain first... -// =============> write your prediction here - -function sum(a, b) { - return; - a + b; -} - -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 +// Predict and explain first... +// I predict that the code will run but it will print undefined because the return expression is not setup properly + +/*function sum(a, b) { + return; + a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);*/ + +// When we run the code it printed out undefined because the return keyword is there but the expression that needs to be returned is not on the same line +// therefore the return keyword can't reach the expression and the expression is ignored because the function has done execution right after it reaches the return keyword + +// Finally, correct the code to fix the problem +// Here is the fixed code + +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..d3eff7d84 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,36 @@ -// Predict and explain first... - -// Predict the output of the following code: -// =============> Write your prediction here - -const num = 103; - -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)}`); - -// Now run the code and compare the output to your prediction -// =============> write the output here -// Explain why the output is the way it is -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here - -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +// Predict and explain first... + +// Predict the output of the following code: +// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument + +/*const num = 103; + +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)}`);*/ + +// Now run the code and compare the output to your prediction +// 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 +// It is because we declared a function with no parameters so the function will ignore any arguments we provide +// since the var num is declared and assigned with a value so every time the function is called it will use the var num + +// Finally, correct the code to fix the problem +// Here is the fixed code +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-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..b95e97ba2 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -1,19 +1,23 @@ -// Below are the steps for how BMI is calculated - -// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. - -// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: - -// squaring your height: 1.73 x 1.73 = 2.99 -// dividing 70 by 2.99 = 23.41 -// Your result will be displayed to 1 decimal place, for example '23.4'. - -// You will need to implement a function that calculates the BMI of someone based off their weight and height - -// Given someone's weight in kg and height in metres -// 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) { - // return the BMI of someone based off their weight and height -} +// Below are the steps for how BMI is calculated + +// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. + +// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: + +// squaring your height: 1.73 x 1.73 = 2.99 +// dividing 70 by 2.99 = 23.41 +// Your result will be displayed to 1 decimal place, for example '23.4'. + +// You will need to implement a function that calculates the BMI of someone based off their weight and height + +// Given someone's weight in kg and height in metres +// 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) { + const BMI = weight / height ** 2; + const stringBMI = BMI.toString(); + return stringBMI.slice(0, stringBMI.indexOf(".") + 2); +} + +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..c820144df 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -1,16 +1,20 @@ -// A set of words can be grouped together in different cases. - -// For example, "hello there" in snake case would be written "hello_there" -// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. - -// Implement a function that: - -// Given a string input like "hello there" -// When we call this function with the input string -// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" - -// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" - -// 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 +// A set of words can be grouped together in different cases. + +// For example, "hello there" in snake case would be written "hello_there" +// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. + +// Implement a function that: + +// Given a string input like "hello there" +// When we call this function with the input string +// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" + +// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" + +// 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 uppercaseAndUnderscore(word) { + return word.toUpperCase().replaceAll(" ", "_"); +} +console.log(uppercaseAndUnderscore("welcome to cyf")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..000b99a2d 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,25 @@ -// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js - -// You will need to take this code and turn it into a reusable block of code. -// 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 +// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js + +// You will need to take this code and turn it into a reusable block of code. +// 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) { + if (penceString == "") { + return "please put the amount"; + } + const totalPence = parseInt(penceString, 10); + const pounds = totalPence / 100; + const formattedPound = pounds.toFixed(2); + return "£" + formattedPound; +} + +console.log(toPounds("1991p")); +console.log(toPounds("2020p")); +console.log(toPounds("399p")); +console.log(toPounds("69p")); +console.log(toPounds("00p ")); +console.log(toPounds(" 50p ")); +console.log(toPounds("232423")); +console.log(toPounds("")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..027d3d473 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -1,38 +1,45 @@ -function pad(num) { - let numString = num.toString(); - while (numString.length < 2) { - numString = "0" + numString; - } - return numString; -} - -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)}`; -} - -// 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 - -// 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 - -// c) What is the return value of pad when it is called for the first time? -// =============> write your answer here - -// 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 - -// 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 +function pad(num) { + let numString = num.toString(); + while (numString.length < 2) { + numString = "0" + numString; + } + return numString; +} + +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)); + +// 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? +// The function pad will be called three 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? +// num is assigned value of totalHours when pad is called first time because js evaluates expressions from left to right +// Therefore num will be assigned with 0 when it is called for the first time + +// c) What is the return value of pad when it is called for the first time? +// The return value will be "00" + +// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer +// num will be assigned the value of remainingSeconds when pad is called for the last time because js evaluates any expression +// from left to right and the pad with argument remainingSeconds is found at the right end +// Therefore num will be assigned with 1 when it is called for the last time + +// e) What is the return value of pad when it is called for the last time in this program? Explain your answer +// The return value of pad when it is called for the last time is "01" +// We calculated remainingSeconds in the function formatTimeDisplay and we got the value 1 then when pad is called with +// an argument value of remainingSeconds we changed the value to string and 0 is added before 1 and it became 2 digits which is "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..beda57c17 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -1,25 +1,137 @@ -// This is the latest solution to the problem from the prep. -// 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`; - } - return `${time} am`; -} - -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}` -); +// This is the latest solution to the problem from the prep. +// 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 slicedHours = time.slice(0, 2); + const hours = Number(slicedHours); + const slicedMinutes = time.slice(-2); + const minutes = Number(slicedMinutes); + const pattern = /^\d{2}:\d{2}$/; + if (!pattern.test(time)) { + return `wrong format input`; + } + if (minutes < 60) { + if (hours >= 24) { + return `please check your input hour`; + } else if (hours > 12 && hours <= 21) { + return `0${hours - 12}:${slicedMinutes} pm`; + } else if (hours > 21) { + return `${hours - 12}:${slicedMinutes} pm`; + } else if (hours === 0) { + return `12:${slicedMinutes} am`; + } else if (hours === 12) { + return `12:${slicedMinutes} pm`; + } else { + return `${slicedHours}:${slicedMinutes} am`; + } + } else { + return `please check your input minute`; + } +} + +const currentOutput1 = formatAs12HourClock("00:00"); +const targetOutput1 = "12:00 am"; +console.assert( + currentOutput1 === targetOutput1, + `current output: ${currentOutput1}, target output: ${targetOutput1}`, + "dude whats wrong", +); + +const currentOutput2 = formatAs12HourClock("13:00"); +const targetOutput2 = "01:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + `current output: ${currentOutput2}, target output: ${targetOutput2}`, + "dude whats wrong", +); + +const currentOutput3 = formatAs12HourClock("21:00"); +const targetOutput3 = "09:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, + "dude whats wrong", +); + +const currentOutput4 = formatAs12HourClock("22:00"); +const targetOutput4 = "10:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, + "dude whats wrong", +); + +const currentOutput5 = formatAs12HourClock("23:59"); +const targetOutput5 = "11:59 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, + "dude whats wrong", +); + +const currentOutput6 = formatAs12HourClock("12:60"); +const targetOutput6 = "please check your input minute"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, + "dude whats wrong", +); + +const currentOutput7 = formatAs12HourClock("24:00"); +const targetOutput7 = "please check your input hour"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}`, + "dude whats wrong", +); + +const currentOutput8 = formatAs12HourClock("100:00"); +const targetOutput8 = "wrong format input"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}`, + "dude whats wrong", +); + +const currentOutput9 = formatAs12HourClock("0:00"); +const targetOutput9 = "wrong format input"; +console.assert( + currentOutput9 === targetOutput9, + `current output: ${currentOutput9}, target output: ${targetOutput9}`, + "dude whats wrong", +); + +const currentOutput10 = formatAs12HourClock("tt:00"); +const targetOutput10 = "wrong format input"; +console.assert( + currentOutput10 === targetOutput10, + `current output: ${currentOutput10}, target output: ${targetOutput10}`, + "dude whats wrong", +); + +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("13:00")); +console.log(formatAs12HourClock("21:00")); +console.log(formatAs12HourClock("22:00")); +console.log(formatAs12HourClock("23:59")); +console.log(formatAs12HourClock("12:60")); +console.log(formatAs12HourClock("24:00")); +console.log(formatAs12HourClock("100:00")); +console.log(formatAs12HourClock("0:00")); +console.log(formatAs12HourClock("tt:00")); + +// /^\d{2}:\d{2}$/ so this is regular expression or regex I used them to validate the input time format +// it is enclosed with /.../ telling the JS it is regular expression +// ^ this means check from the start of the string +// \d this refers single digit from 0 to 9 +// {2} means two times (\d{2} .....two single digits) +// : simply checks with : +// the same like before \d{2} checks two single digits +// $ checking ends + +// I preferred this because it will strictly check the format of the input with the desired input format +// so that the following process will continue with out error, and it saves a lot of time + +// It is rigid and does not check the value ....it only checks the format