diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039f..c2cc3b112 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-1/errors/0.js @@ -1,2 +1,2 @@ -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? */ \ No newline at end of file diff --git a/Sprint-1/errors/1.js b/Sprint-1/errors/1.js index 7a43cbea7..78250141a 100644 --- a/Sprint-1/errors/1.js +++ b/Sprint-1/errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +/* const age = 33; +age = age + 1; */ + +let age = 33; age = age + 1; +console.log(age); diff --git a/Sprint-1/errors/2.js b/Sprint-1/errors/2.js index e09b89831..a7401245d 100644 --- a/Sprint-1/errors/2.js +++ b/Sprint-1/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"; +console.log(`I was born in ${cityOfBirth}`); \ No newline at end of file diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index ec101884d..9a8ec7058 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -1,4 +1,4 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber @@ -7,3 +7,6 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +//Error: cardNumber.slice is not a function \ No newline at end of file diff --git a/Sprint-1/errors/4.js b/Sprint-1/errors/4.js index 21dad8c5d..19fecac48 100644 --- a/Sprint-1/errors/4.js +++ b/Sprint-1/errors/4.js @@ -1,2 +1,39 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +//const 12HourClockTime = "20:53"; +//const 24hourClockTime = "08:53"; + + function convert12To24(time) { + const [hours, minutes] = time.split(":"); + let hour = parseInt(hours); + const period = time.includes("PM") ? "PM" : "AM"; + + if (period === "PM" && hour < 12) { + hour += 12; + } else if (period === "AM" && hour === 12) { + hour = 0; + } + + return `${hour.toString().padStart(2, '0')}:${minutes}`; +} + +function convert24To12(time) { + const [hours, minutes] = time.split(":"); + let hour = parseInt(hours); + const period = hour >= 12 ? "PM" : "AM"; + + if (hour > 12) { + hour -= 12; + } else if (hour === 0) { + hour = 12; + } + + return `${hour}:${minutes} ${period}`; +} + +const twelveHourClockTime = "08:53 PM"; // Example input for 12-hour format +const twentyFourHourClockTime = "20:53"; // Given input for 24-hour format + +const convertedTo24 = convert12To24(twelveHourClockTime); +const convertedTo12 = convert24To12(twentyFourHourClockTime); + +console.log(`12-hour to 24-hour: ${convertedTo24}`); // Output: 20:53 +console.log(`24-hour to 12-hour: ${convertedTo12}`); // Output: 8:53 PM diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b6..d4bfb4b84 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -4,3 +4,9 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + +/* Answer: In line 3, count = count + 1;, the = operator is performing an assignment. +It takes the current value of the variable count, which is 0 at this point, +adds 1 to it, and then assigns the result back to the variable count. +So, after this line executes, count will now hold the value of 1. +Essentially, this line increments the value of count by 1 */ \ No newline at end of file diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce2..b291f697a 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -1,9 +1,18 @@ const num = 56.5678; +const wholeNumberPart = Math.floor(num); +const decimalPart = num - wholeNumberPart; +const roundedNum = Math.round(num); + +console.log(wholeNumberPart); +console.log(decimalPart); +console.log(roundedNum); + + // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math // Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) // Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num ) // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) -// Log your variables to the console to check your answers +// Log your variables to the console to check your answers \ No newline at end of file diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd137..8fd660f6e 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -2,5 +2,9 @@ let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); + +console.log(initials); + // 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. diff --git a/Sprint-1/exercises/paths.js b/Sprint-1/exercises/paths.js index c91cd2ab3..5e207c9a6 100644 --- a/Sprint-1/exercises/paths.js +++ b/Sprint-1/exercises/paths.js @@ -15,4 +15,7 @@ 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 +// Answer: const dir = slice(0, lastSlashIndex); + // Create a variable to store the ext part of the variable +// Answer: const ext = slice(base.lastIndexOf(".")); diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aab..a672ca0bb 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -7,3 +7,5 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 + +//Answer: Each time I run the code, num will be a random integer between 1 and 100, including both endpoints. \ No newline at end of file diff --git a/Sprint-1/explore/chrome.md b/Sprint-1/explore/chrome.md index e7dd5feaf..bf87c99df 100644 --- a/Sprint-1/explore/chrome.md +++ b/Sprint-1/explore/chrome.md @@ -12,7 +12,11 @@ invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +-->Answer: This will pop up an alert box displaying "Hello world!". + Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. What effect does calling the `prompt` function have? What is the return value of `prompt`? + +-->Answer: This code works as follows: prompt("What is your name?") displays a dialog box prompting the user to enter their name. The value entered by the user will be stored in the variable myName. \ No newline at end of file diff --git a/Sprint-1/explore/objects.md b/Sprint-1/explore/objects.md index 0216dee56..c479176f2 100644 --- a/Sprint-1/explore/objects.md +++ b/Sprint-1/explore/objects.md @@ -6,11 +6,22 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +-->Answer: ƒ log() { [native code] } + Now enter just `console` in the Console, what output do you get back? +-->Answer: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + Try also entering `typeof console` +-->Answer: 'object' + Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +-->Answer: +console: This is a built-in global object in JavaScript that provides access to the browser's debugging console. It includes various methods for logging information, warnings, errors, and more. + +The dot (.) is used to access properties and methods of an object. In this case, it is used to access the log and assert methods of the console object. \ No newline at end of file diff --git a/Sprint-1/interpret/percentage-change.js b/Sprint-1/interpret/percentage-change.js index e24ecb8e1..58c210050 100644 --- a/Sprint-1/interpret/percentage-change.js +++ b/Sprint-1/interpret/percentage-change.js @@ -12,11 +12,30 @@ 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 +/* Three. + 1. Number + 2. replaceAll + 3. console.log // 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? +/* a "," missing in fifth line, should be: +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); */ + // c) Identify all the lines that are variable reassignment statements +/* 1. carPrice + 2. priceAfterOneYear + // d) Identify all the lines that are variable declarations +/* 1. carPrice + 2. priceAfterOneYear + 3. priceDifference + 4. percentageChange // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +/* carPrice.replaceAll(",", "") removed comma from carPrice + +The overall purpose of the expression Number(carPrice.replaceAll(",", "")) is to: + 1. Clean up the string representation of the car price by removing any commas that are commonly used in formatting large numbers. + 2. Convert the cleaned string into a numeric type so that mathematical operations (like addition, subtraction, or percentage calculations) can be performed accurately. \ No newline at end of file diff --git a/Sprint-1/interpret/time-format.js b/Sprint-1/interpret/time-format.js index 83232e43a..c5b4139ea 100644 --- a/Sprint-1/interpret/time-format.js +++ b/Sprint-1/interpret/time-format.js @@ -12,13 +12,21 @@ 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? +/* One. movieLength // b) How many function calls are there? +/* One. console.log // c) Using documentation, explain what the expression movieLength % 60 represents +/* It is used to calculate the remaining seconds in the movie length (given in seconds). +Here, % is the modulus operator, which calculates the remainder of two numbers when divided. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* It is calculating the total number of minutes in the movie length, excluding the remaining seconds. // e) What do you think the variable result represents? Can you think of a better name for this variable? +/* Total length of the movie. +"total_length" // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +It works for non-negative values of movieLength. \ No newline at end of file diff --git a/Sprint-1/interpret/to-pounds.js b/Sprint-1/interpret/to-pounds.js index 60c9ace69..4148a204f 100644 --- a/Sprint-1/interpret/to-pounds.js +++ b/Sprint-1/interpret/to-pounds.js @@ -25,3 +25,9 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1): remove "p" +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Pad the pence number string +// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2): Extract pounds +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: Extract pence +// 6. console.log(`£${pounds}.${pence}`);: Output the Result +