diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..6a7a7f69 100644 --- a/week-1/errors/0.js +++ b/week-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/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..23569c15 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,18 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +// const age = 33; + +let age = 33; age = age + 1; + + +// The issue in this code is that you are attempting to reassign a new value to a +// constant variable (age) after it has been initially assigned a value, and then reassign the value by 1 +// In JavaScript, const is used to declare a variable whose value should not be reassigned after it's +// initially set. Therefore, attempting to modify or reassign a const variable will result in an error. + +// solutions +/* +To resolve this issue, you should use let instead of const +if you want to declare a variable that can be reassigned. +*/ \ No newline at end of file diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..4fe8faca 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,7 @@ // 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}`); + diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..aacc4d4a 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,7 +1,13 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// const last4Digits = cardNumber.slice(-4); +const last4Digits = Math.floor(cardNumber % 10000); +console.log(last4Digits); + + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Make and explain a prediction about why the code won't work // Then try updating the expression last4Digits is assigned to, in order to get the correct value + +// .slice cannot slice numbers, it can only be used for strings. \ No newline at end of file diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..99b6dfb1 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; + +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..0a1672fe 100644 --- a/week-1/exercises/count.js +++ b/week-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 + +// count is a variable with an initial value of 0. It represents a numeric value. + +// count + 1 is an expression on the right side of the assignment operator (=). function is to count + +// \ No newline at end of file diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..9de24a23 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -1,6 +1,14 @@ const num = 56.5467; +const wholeNumberPart = Math.round(num) - 2; +const decimalPart = num - Math.round(num); +const roundedNum = Math.round(num) - 1; + +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 ) diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..24235d4c 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -2,5 +2,15 @@ let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; +<<<<<<< HEAD +const initials = `${firstName.substring(0, 1)}${middleName.substring( + 0, + 1 +)}${lastName.substring(0, 1)}`; +console.log(initials); + +// Declare a variable called initials that stores the first 3 characters of each string in upper case +======= // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c // Log the variable in each case diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..ebd5cc8a 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -14,5 +14,14 @@ const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); +const dir = filePath.slice(0, lastSlashIndex); +const dotIndex = base.lastIndexOf("."); +const ext = dotIndex !== -1 ? base.slice(dotIndex + 1) : ""; + +console.log(`The dir part of ${filePath} is: \n ${dir}`); +console.log(`The ext part of ${filePath} is: \n ${ext}`); + // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable + + diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..a0034c2f 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -3,6 +3,22 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +// - minimum and maximum are constants that define the range of numbers to generate. +// minimum is set to 1 and maximum is set to 100. +// - Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). +// - (maximum - minimum + 1) calculates the number of integers between the min and max values, inclusive. +// In this case it's 100 - 1 + 1 = 100. +// - Multiplying Math.random() by (maximum - minimum + 1) gives us a random decimal between 0 and 100. +// - Math.floor() rounds the random decimal down to the nearest whole number. +// - Adding minimum (which is 1) to the rounded number offsets the range to be between 1 and 100 instead +// of 0 and 99. +// - So Math.floor(Math.random() * (maximum - minimum + 1)) + minimum ultimately generates a random +// integer between the min and max values of 1 and 100. +// - The result is logged to the num and tested. +// So in summary, it uses Math.random(), Math.floor(), min/max ranges, multiplication, and addition +// to generate a random number between two set values. + // 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 diff --git a/week-1/explore/chrome.md b/week-1/explore/chrome.md index e7dd5fea..1bf3dbf8 100644 --- a/week-1/explore/chrome.md +++ b/week-1/explore/chrome.md @@ -12,7 +12,12 @@ invoke the function `alert` with an input string of `"Hello world!"`; What effect does calling the `alert` function have? +The Alert function pops out message whenever the user logs on the website. + 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? +The prompt function pops out the message whenever the user logs on the web page. + What is the return value of `prompt`? +The return vale of Prompt is the user's name. diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 0216dee5..9a5e2e76 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -5,8 +5,12 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? + ƒ log() { [native code] } Now enter just `console` in the Console, what output do you get back? +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + + Try also entering `typeof console` @@ -14,3 +18,5 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +The console.assert() method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. The dot. notation in console.log or console.assert() is a method used to access the specific debugging method on the global console object provided by web browsers. \ No newline at end of file diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..94ff05f2 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -9,12 +9,32 @@ 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 +<<<<<<< HEAD +// There are 3. Line 4, Line 7 and Line 10. + +// 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? +======= // b) Identify all the lines that are variable reassignment statements +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c // c) Identify all the lines that are variable declarations +<<<<<<< HEAD +// There are 2. On line 4 and line 5 + +// d) Identify all the lines that are variable declarations + +// There are 2. On line 7 and line 8 + +// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// This expression is saying that I want to replace the data type of carPrice from a string to a number + +======= // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 7961fe0d..1df86cf6 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -14,16 +14,38 @@ 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? +// There are 7. // b) How many function calls are there? +// There isn't any function calls. // c) Using documentation on MDN, explain what the expression movieLength % 60 represents +// This represents how many seconds are in a minute. +// This ensures that all the minutes are accounted for, and any remainders will be formatted in seconds. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// It means that I want you to collect the movieLength which is (8784), I want you to minus the remainingSeconds from this, +// and because remainingSeconds seconds is stored in a variable, it works out what the sum should be for remainingSeconds, +// which is (24 seconds). This leaves us with a sum from the expression movieLength - remainingSeconds = (8760), +// which if then divided by 60 = (146). + // e) What do you think the variable result represents? Can you think of a better name for this variable? +<<<<<<< HEAD +// The amount of time left of the film +// const filmTimeLeft = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`; + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// The code will work with all values for movieLength. When the result is executed with a value of 1, +// the timer will display 0(Hours):0(Minutes):1(Seconds). +// I did notice though that if I changed this number to a ridiculous number for example: 100000000000000 or 1000000000, +// it would return the same value, which it should not +======= // f) Think about whether this program will work for all values of movieLength. // Think of what values may cause problems for the code. // Decide the result should be for those values, then test it out. // Can you find any values of movieLength which don't give you what you'd expect? +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..048d1c0d 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -25,5 +25,21 @@ console.log(`£${pounds}.${pence}`); // You should use MDN to look up substring, padStart and padEnd to interpret this program // To begin, we can start with +<<<<<<< HEAD +// const penceString = "399p": initialises a string variable with the value "399p" + +// It stores actual number value of the string.From index of 0 until second last index of the string.so it can exclude letter p. + +//This line is ensuring that if its length is less than 3 characters, it adds "0" characters at the beginning of the string +//to make it a total of 3 characters long. If the original string is already 3 characters or longer, no padding is added. + +//It is storing first index of the string which is 3 in variable pounds and represent the number of pounds in that string, while excluding remaining number. + +// It is storing last two indexes of the string which are 99 in variable pence and represent the number of pence in that string, +//while excluding remaining number. After extracting the last two digits, +//the padEnd method is used to ensure that the string is always two characters long. If the extracted substring is less than two characters, it pads the string with zeros on the right side until it reaches a length of 2. +//It is displaying total number of pounds and pence. +======= // 1. const penceString = "399p": initialises a string variable with the value "399p" +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..2beaa4fd 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -1,7 +1,10 @@ // Predict and explain first... +// The console.log will not return the value of what action you want for a and b to execute and it should be changed to return a * b + + function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..d22fb9a7 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,10 @@ // Predict and explain first... +// Line 4 is returning nothing because of semicolon and command is not confirmed. + function sum(a, b) { - return; - a + b; + return a + b; + } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..d64a5450 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,8 +1,8 @@ // Predict and explain first... -const num = 103; -function getLastDigit() { + +function getLastDigit(num) { return num.toString().slice(-1); } @@ -12,3 +12,4 @@ 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 +//It was not taking num as an argument and have fixed value of num which is 103, each time when it is called it take declared value of num. \ No newline at end of file diff --git a/week-2/errors/0.js b/week-2/errors/0.js index 58b1349d..779eb839 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -1,9 +1,23 @@ // Predict and explain first... -// write down the error you predict will be raised -// then call the function capitalise with a string input -// interpret the error message and figure out why it's happening, if your prediction was wrong + +// str has been stored as a value inside the function capitalize, meaning that it will not work, +// once a name has been used it cannot be reused as this will cause conflict. + + +// 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; + return `${str[0].toUpperCase()}${str.slice(1)}`; } + +const capitaliseTest = capitalise("hi"); +const myNameCapTest = capitalise("karam"); + +console.log(capitaliseTest); +console.log(myNameCapTest); diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..4044d37b 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -3,11 +3,26 @@ // Why will an error occur when this program runs? // Play computer with the example to work out what is going on +//This will not work because in line 7 the variable decimal number declaration came after the function. It should come first then the function declaration. +//Also we need to call the function in the console log for it to work. + +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; + +// return percentage; +// } + +// console.log(decimalNumber); + +//I have corrected the code below + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); + +console.log(convertToPercentage(0.7)); diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..25df5fc1 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -1,10 +1,15 @@ // Predict and explain first... +// The number 3 should have been declared as an argument not as a declaration. // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? -function square(3) { + + +function square(num) { return num * num; } - +console.log(square(3)); +console.log(square(2)); +console.log(square(4)); diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..a7a5b8cd 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -11,5 +11,15 @@ // Implement a function that calculates the BMI of someone using their weight and height // Given someone's weight in kg and height in metres -// When we call this function with the weight and height -// Then it returns their Body Mass Index to 1 decimal place +// Then when we call this function with the weight and height +// It should return their Body Mass Index to 1 decimal place + +function bodyMassIndex(height, weight) { + const sum = weight / (height * height); + const roundSum = sum.toFixed(1); + return roundSum; +} + +const test = bodyMassIndex(1.73, 70); + +console.log(test); diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index 56b0d8d0..5ebc65fd 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -13,5 +13,11 @@ // Test case: we expect "the great gatsby" to be "THE_GREAT_GATSBY" // Test case: we expect "the da vinci code" to be "THE_DA_VINCI_CODE" -// Come up with a clear, simple name for the function -// Use the string documentation to help you plan your solution +// You will need to come up with an appropriate name for the function +// Use the string documentation to help you find a solution + +function upperSnakeCase(str) { + return str.toUpperCase().replace(/ /g, "_"); +} + +console.log(upperSnakeCase("lord of the rings")); diff --git a/week-2/implement/to-pounds.js b/week-2/implement/to-pounds.js index 7add3d05..c7aea00e 100644 --- a/week-2/implement/to-pounds.js +++ b/week-2/implement/to-pounds.js @@ -1,5 +1,14 @@ // Find to-pounds.js in an earlier week in this repo -// Take this code and turn it into a reusable block of code. -// Declare a function called toPounds with an appropriately named parameter. -// Call this function a number of times to check it works for different inputs +// 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(str) { + return `£${(Number(str.substring(0, str.length - 1)) / 100).toFixed(2)}`; +} + +console.log(toPounds("499p")); +console.log(toPounds("293p")); +console.log(toPounds("27p")); diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..3a561758 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -7,4 +7,14 @@ // Given a number, // When I call this function with a number -// Then it returns the new price with VAT added on +// it returns the new price with VAT added on + +function priceWithVAT(price) { + newPrice = price * 1.2; + return `New price (20% VAT inclusive): £${newPrice}`; +} + + +console.log(priceWithVAT(60)); +console.log(priceWithVAT(39)); +console.log(priceWithVAT(28)); diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..989828f3 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -25,12 +25,37 @@ console.log(formatTimeDisplay(143)); // Questions +<<<<<<< HEAD +// a) Run this program with node, identify the line the error is thrown. Explain why this error is occurring. How can you fix this error? + // There is no error. + +// b) When formatTimeDisplay is called how many times will pad be called? + // 3 times +======= // a) When formatTimeDisplay is called how many times will pad be called? // Call formatTimeDisplay with an input of 143, now answer the following: +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c // b) What value is assigned to the parameter num when pad is called for the first time? +<<<<<<< HEAD +// c) What is the value assigned to num when pad is called for the first time? + // remainingHours + + +// d) What is the return value of pad is called for the first time? + // 00 + +// e) What is the value assigned to num when pad is called for the last time in this program? Explain your answer + // It is taking remainingSeconds last time. First assigned value was remainingHours to num when called, +//then assigned value was remainingMinutes to num. So, the last time it has the assigned value of num was remainingSeconds. + +// f) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer + //It has the return value of 01 because when it took remainingSeconds as a parameter +//which has 1 value return the number padded with 0 at the start. + +======= // c) What is the return value of pad when it is called for the first time? // d) What is the value assigned to the parameter num when pad @@ -41,3 +66,4 @@ console.log(formatTimeDisplay(143)); // f) Research an alternative way of padding the numbers in this code. // Look up the string functions on mdn +>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c