diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6e..1cb53ace08 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,3 +1,4 @@ + let count = 0; count = count + 1; diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f6175..b8b2e5e974 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,15 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; + +function first(firstName, middleName, lastName){ + + let initials = firstName.slice(0,1) + middleName.slice(0,1) + lastName.slice(0,1); + + return `${initials}`; +} +console.log(first(firstName, middleName, lastName)); + // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28e..4ae6c9e912 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -11,13 +11,15 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); + +// 1. Get the base (file.txt) const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); +const dir = filePath.slice(0, lastSlashIndex); + // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aabb..0cb3c7dce2 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,18 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +Math.floor is a function to make sure it is a whole number. + +The fuction will randomly choose a decimal number from 0 - 1. +After that will do the Math(100-1+1) and multiply the decimal value and add 1. + + + + // In this exercise, you will need to work out what num represents? // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing + + + diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f7..f2702bcd14 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,3 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea76..60526e41e2 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 - -const age = 33; -age = age + 1; +function X(num){ +const age = num +1; +return age;} +console.log(X(33)); diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831d..1cdbd587a9 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? - -console.log(`I was born in ${cityOfBirth}`); -const cityOfBirth = "Bolton"; +const cityOfBirth = "BOlton"; +const sentence = `I was born in ${cityOfBirth}`; +console.log(sentence); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884db..01f7180b71 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,13 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); + +because it doesn't have console.log, so it doesn't print the result. +but it gives the function errors. + +it is not what I expected, because we should change the number into string, because it is not a +function. + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 5f86c730bc..06c2f6483b 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const twelvehourClockTime = "8:53pm"; +const twentyfourhourClockTime = "20:53"; + +console.log(twelvehourClockTime); +console.log(twentyfourhourClockTime); + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e18..28ec7b90e4 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,33 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +There are 4 function calls here. There are two when you use Number(); + The other call is about console.log() to print the string out. + When you call the variable (priceDifference/ carPrice), it is a function call. +The last one is // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +It shows that I miss a ) parenthesis. + + // c) Identify all the lines that are variable reassignment statements +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + // d) Identify all the lines that are variable declarations -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +// e) Describe what is the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +It deletes all the comma insides.I +The Number helps convert that string into an actual figure. + + + diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d2395587..9bc36f82e7 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,31 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? + Six, the const with the functions inside. + // b) How many function calls are there? +4 // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +It is called the remaining function, because if you use this function , you will use the remainingMinutes to divides 60 and get the remaining value . + + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +It calls the variable movieLength and remainingSeconds and substract together and divide it 60. + + // e) What do you think the variable result represents? Can you think of a better name for this variable? +I think it represents the total hour , remaining minutes and the remaining seconds .Duration is a better name + + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +yes +I tried to put 6(second), 60(1 minute), 3600(equivalent to 1 hour) + + + diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69a..bec37b8685 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -24,4 +24,17 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" + +line 1. const penceString = "399p": initialises a string variable with the value "399p" +line 3. You define a penceStringWithoutTrailingP variable and call a function to cut a line, you use penceString.length -1 because +you want to cut the p. + +line 8: You define a variable paddedPenceNumberString and call a function to add 0 into the letters, if the words are not three characters long. +line 9: You want to take the pound out of 399, so you call the function substring and only wants before the index 1, which is calculated by (0 , length -2); + +line 14: You call a variable function and you subtract the value from index 1 to the end. +line 18: print the variable pound and the variable pence with the . + + + + diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..7a9eb878cf 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,25 @@ -// 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)}`; + let str = '${str[0].toUpperCase()}${str.slice(1)}`; return str; } -// =============> write your explanation here -// =============> write your new code here +It said syntaxError because identifier 'str' has already been declared +because we should use backtick`` to wrap the template literal.The first one of the bracket is a +single quote, not a backtick. + + Also, since str has already been declared as a parameter, we should not use it to define as variable. + + + + + +function capitalise(str) { + + let result = `${str[0].toUpperCase() + str.slice(1)}`; + return result; +} + +console.log(capitalise("str")); + diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..9c0e5ca561 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,10 +1,3 @@ -// 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}%`; @@ -14,7 +7,19 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +The terminal shows the syntax error because decimal number has been declared. +because the decimal number is a variable which is not supposed to define in the function, +but the decimal number has been defined insides the functon, so it will forever return 50 %. + Also, when we call the fucnton, we should name the function convertToPercentage. + + + +function convertToPercentage(decimalNumber) { + + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(0.5)); -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..d52f46729a 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,22 @@ -// 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 + It shows the syntax error which is an unexpected number; + because we should not put 3 insides the variable, because it will lead to variable: num not defined +insides the function, so the 3 doesn't work insides the function. + +We need to put num instead of 3 insides the(); +That way when we call the function by using console.log.The 3 will go into the num variable. + -// Finally, correct the code to fix the problem +function square(num) { + return num * num; +} -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..f59f347333 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ -// Predict and explain first... -// =============> write your prediction here +The problem is unexpected identifier "problem" +The problem is the result doesn't print the mulitplication of the number, +because we should not call the function inside the function; + function multiply(a, b) { console.log(a * b); @@ -8,7 +10,11 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +Instead, we should define the expression a * b +and return the expression. + +/ function multiply(a, b) { + return (a * b); + } -// Finally, correct the code to fix the problem -// =============> write your new code here +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..2ca9b6c6cf 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ -// Predict and explain first... -// =============> write your prediction here +Unexpected identifier "problem" +variable hasn't been defined so the variable cannot do function. function sum(a, b) { return; @@ -8,6 +8,14 @@ function sum(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 + +because return should be followed by a + b; +the final result doesn't return the value. + + +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 57d3f5dc35..2281e71754 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,12 @@ -// Predict and explain first... -// Predict the output of the following code: -// =============> Write your prediction here +The const should not be put into the global frame. +if it is set in the global frame, this will result in a single result:3, +no matter which variables you put in later. + +The variable insides should have num to match the function expression. + + +My prediction of the result will be all in 3. const num = 103; @@ -13,12 +18,22 @@ 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 + +yes the output is all in 3, because the reason are above . + + + +... ... ... ... ... ... ... ... ... ... ... ... ... ... ... + +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)}`); + + +After we delete the global frame and put in the variable, we see +the result now shows three different digit number. \ 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 17b1cbde1b..9bc97e2820 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -1,19 +1,20 @@ -// 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. +function calculateBMI(weight, height) { + + let newheight = height / 100; + let num = weight / (newheight ** 2); + let idea = num.toFixed(1); + + return idea; +} + + +console.log(calculateBMI(58, 178)); + + + -// 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 their Body Mass Index to 1 decimal place -function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ 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..7c30f5334f 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -4,6 +4,16 @@ // UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. // Implement a function that: +function convertToUpperCase(text) { +const result = text.toUpperCase(); +return result; +} + +console.log(convertToUpperCase("hello")); + + + + // Given a string input like "hello there" // When we call this function with the input string diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 9e05a871e2..d37f305526 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -1,5 +1,4 @@ // Implement a function getAngleType -// // When given an angle in degrees, it should return a string indicating the type of angle: // - "Acute angle" for angles greater than 0° and less than 90° // - "Right angle" for exactly 90° @@ -15,7 +14,24 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + else if(angle === 90){ + return "Right angle"; + } + else if(angle >90 && angle < 180){ + return "Obtuse angle"; + } + else if(angle === 180){ + return "Straight angle"; + } + else if(angle >180 && angle<360){ + return "Reflex angle"; + } + else if(angle>0 && angle < 90){ + return "Acute angle"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -31,7 +47,9 @@ function assertEquals(actualOutput, targetOutput) { ); } + + // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles const right = getAngleType(90); -assertEquals(right, "Right angle"); +assertEquals(getAngleType(90), "Right angle"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b641..f85a329541 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,7 +11,12 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + + return numerator< denominator ; + + + + } // The line below allows us to load the isProperFraction function into tests in other files. diff --git a/Sprint-3/package.json b/Sprint-3/package.json new file mode 100644 index 0000000000..90405bdf0d --- /dev/null +++ b/Sprint-3/package.json @@ -0,0 +1,42 @@ +{ + "name": "week-4-test-example", + "description": "An example application showing how to write tests using the jest framework", + "scripts": { + "test": "jest" + }, + "devDependencies": { + "jest": "^29.5.0" + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +