diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js deleted file mode 100644 index ab90ebb28..000000000 --- a/Sprint-1/1-key-exercises/3-paths.js +++ /dev/null @@ -1,23 +0,0 @@ -// The diagram below shows the different names for parts of a file path on a Unix operating system - -// ┌─────────────────────┬────────────┐ -// │ dir │ base │ -// ├──────┬ ├──────┬─────┤ -// │ root │ │ name │ ext │ -// " / home/user/dir / file .txt " -// └──────┴──────────────┴──────┴─────┘ - -// (All spaces in the "" line should be ignored. They are purely for formatting.) - -const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); -console.log(`The base part of ${filePath} is ${base}`); - -// 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/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js deleted file mode 100644 index cf6c5039f..000000000 --- a/Sprint-1/2-mandatory-errors/0.js +++ /dev/null @@ -1,2 +0,0 @@ -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 diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js deleted file mode 100644 index 21dad8c5d..000000000 --- a/Sprint-1/2-mandatory-errors/4.js +++ /dev/null @@ -1,2 +0,0 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js deleted file mode 100644 index e24ecb8e1..000000000 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ /dev/null @@ -1,22 +0,0 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; - -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); - -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; - -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 - -// 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? - -// c) Identify all the lines that are variable reassignment statements - -// 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? diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..d86ca61e5 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +//I think it will give an error because we have `str` twice in line 10 we should only have return without `str` // 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; -} +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } // =============> write your explanation here -// =============> write your new code here +// The error happened because 'str' was declared twice, once as a parameter and once with 'let' inside the function. +// Also, when calling the function, using a word without quotes caused a ReferenceError. +// After removing the duplicate declaration and passing a string with quotes, the function works correctly, +// =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +console.log(capitalise("Hard")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..00699d27d 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,20 +1,24 @@ // 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 +// =============> write your prediction here -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// the error occur because the decimalNumber has already been declared. + +// Try playing computer with the example to work out what is going on - return percentage; -} -console.log(decimalNumber); // =============> write your explanation here +// I tried to run the code it gives me SyntaxError,that the decimalNumber has already been declared, we can see in the parameter already has name decimalNumber. +// thats why declaring it again inside the function, causes an errors. + // 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-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..6454e9b0a 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,20 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +//I think it will give an error because the function parameter can not be a number like (3). -function square(3) { - return num * num; -} - -// =============> write the error message here +// =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here - -// Finally, correct the code to fix the problem +// the parameter can't be a number like (3),also changing num * num to number instead to match the parameter. +// Finally, correct the code to fix the problem. // =============> write your new code here +function square(number) { + return number * number; +} +console.log(square(3)); + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..b6f8ab512 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,17 @@ // 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)}`); +// in console.log we should just write (multiply(10,32) because writing in plain english will give us an error, +// and it won't give the output. // =============> write your explanation here +// the code was broken because the console.log was inside the function it not a function,by using the return it gives the outupt + // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return (a * b) +} + +console.log(multiply(10, 32)); diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..696c0ce65 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,16 @@ // Predict and explain first... // =============> write your prediction here +// The return statement is not in the right place, so the function doesn’t return the value. -function sum(a, b) { - return; - a + b; -} +// =============> write your explanation here -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// The return statement is stopping the function too early, so it doesn’t return a + b. -// =============> write your explanation here // 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..ac6a2d4e5 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,22 +3,40 @@ // Predict the output of the following code: // =============> Write your prediction here -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// 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)}`); +// 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 +// 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 +// we will always get number 3 because num is 103 in the last digit of it is 3. + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +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 +// We need num in the function as a parameter. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..fab3dfe3d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + let BMI = weight / (height * height); + let rounded = Math.round(BMI * 10) / 10; + return rounded; + +} +console.log(calculateBMI(70, 1.73)); // 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 5b0ef77ad..d047cede3 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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(inputString) { + return inputString.replaceAll(" ", "_").toUpperCase(); + + +} +console.log(toUpperSnakeCase("hello there")); +console.log (toUpperSnakeCase("lord of the rings")); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..7b8f31f62 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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("400p")); + console.log (toPounds("243p")); + console.log (toPounds("9876p")); + console.log (toPounds("18p")); \ 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..322ba0c06 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,24 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// It will be called 3 times hours,minutes and Seconds. // 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 first argument passed to pad (totalHours). +// totalHours = 0 // c) What is the return value of pad is called for the first time? // =============> write your answer here +// pad (totalHours) = pad(0) returns "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 when pad called last time is num = 1 because that's the remainingSeconds which is 1 after +// we 61 divided by 60 = 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 +// The return value will be 01 because ToString gives "1" and padStart("2, 0") that adds 0 in the front \ 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..9d5bc0b12 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,7 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); +console.log(formatAs12HourClock("08:00")); +console.log(formatAs12HourClock("23:00")); +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("12:00")) \ No newline at end of file