Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ 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

// Answers
// Line 3 is updating the value of the variable count by adding 1 to its current value.
// The = operator is an assignment operator that takes the value on the right side
// (which is `count + 1`) and assigns it to the variable on the left side (`count`).
// This means that after line 3 is executed, the value of `count` will be incremented by 1.

3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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 = ``;
let initials = firstName[0] + middleName[0] + lastName[0];
// String indexing is used to access the first character of each string.

// https://www.google.com/search?q=get+first+character+of+string+mdn

11 changes: 8 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ 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}`);
// The base part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is file.txt

// 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 = ;
const dir = filePath.slice(0, lastSlashIndex); // Keeps everything except the last slash just before week-1/interpret.
const ext = filePath.slice(lastSlashIndex + 1).split(".")[1]; // Keeps everything after the last slash and splits it into an array of strings at the dot.
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);
// The dir part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is /Users/mitch/cyf/Module-JS1/week-1/interpret
// The ext part of /Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt is txt

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
8 changes: 8 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num);

// 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

// Answers
// num represents a random integer between 1 and 100, inclusive.
// Math.floor() rounds down to the nearest integer.
// Math.random() generates a random decimal number between 0 and 1 but not including 1.
// maximum - minimum + 1 helps to keep both end of the range 1 to 100 after multiplying by Math.random()
// + minimum shifts the range up to start at 1 instead of 0
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?
/*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?
*/

// We can use forward slash and asterisk for multi-line or block comments as used above line 1 to 3. Or we can use double forward slashes for single line comments as used on this 5.
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
//age = age + 1; The variable age is declared as a constant using the const keyword, which means its value cannot be changed after it is assigned.
const newAge = age + 1; // A new variable called newAge is assigned.This way, we can still calculate the new age without modifying the original constant variable.

5 changes: 5 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton"; // The variable cityOfBirth is declared after it is used in the console.log statement.

// The order of the code should be as below.

const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
10 changes: 9 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213"; // parentises
const last4Digits = cardNumber.slice(-4);

// The last4Digits variable should store the last 4 digits of cardNumber
Expand All @@ -7,3 +7,11 @@ 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

// Answer
// "TypeError: cardNumber.slice is not a function" is the error that it was given when running the code.
// This error occurs because the `slice` method is a string method, and `cardNumber` is a number, not a string.
// Therefore, we cannot use `slice` directly on a number.
// To fix the issue, we need to convert `cardNumber` to a string by adding parentises.


4 changes: 4 additions & 0 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";

// In js variable names cannot start with a number. The variable names are changed to start with a letter instead of a number as below.
const twelveHourClockTime = "8:53pm";
const twentyFourHourClockTime = "20:53";
18 changes: 17 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,3 +20,19 @@ console.log(`The percentage change is ${percentageChange}`);
// 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?

// Answer
// a) Line 4: Number(carPrice.replaceAll(",", "")); and carPrice.replaceAll(",", "")
// a) Line 5: Number(priceAfterOneYear.replaceAll("," "")); and priceAfterOneYear.replaceAll("," "")

// b) The error is coming from line 5. The error is occurring in the replaceAll method a comma is missing between ("," "") it should be (",", "") to fix the problem.

// c) line 4: carPrice = Number(carPrice.replaceAll(",", ""));
// c) line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));

// d) Line 1: let carPrice = "10,000";
// d) Line 2: let priceAfterOneYear = "8,543";
// d) Line 7: const priceDifference = carPrice - priceAfterOneYear;
// d) Line 8: const percentageChange = (priceDifference / carPrice) * 100;

// e) Number() is converting the string into a number. replaceAll() is removing the comma from the string.
14 changes: 14 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ console.log(result);
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

// answer

// a) 6 variables: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result

// b) 1 function calls: console.log(result)

// c) Reminder operator: movieLength (8784 seconds)is divided by 60 and converted into minutes. The reminder value then stored as seconds.

// d) It calculates how many full minutes (60 seconds) in movieLength are there excluding the seconds.

// e) total movie length in hours, minuets and seconds. movieDuration can be an alternative variable.

// f) I tried negative and decimal number. The code works but it doesn't make any sense to represent with this type of values other than integers.
13 changes: 13 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

// Answers
// 1) variable const penceStringWithoutTrailingP stores the value of only the pennies part without the p.
// This is done first with penceString.length -1 removes the last index of pence string (p).
// Then penceString.substring(0) returns the characters from 0 to 2 of the indexes(399)

// 2) paddedPenceNumberString adds 0 at the beginning until the string is 3 characters long to penceStringWithoutTrailingP variable

// 3) pounds stores 3 by first paddedPenceNumberString.length - 2 removing index 1 and 2. Then paddedPenceNumberString.substring(0) returning index 0.

// 4) pence store the pennies first .length starting from index 1 then padEnd adding 0 to characters that less than 2 counts.

// 5) console.log prints out the final result adding a £ sign at the beginning and a . between pounds and pence.
8 changes: 7 additions & 1 deletion Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ Let's try an example.

In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

- A window pops up with `"Hello world!"` message

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?

- a new window popup saying what is your name with a text input field for user to type.

What is the return value of `prompt`?

- A text string that input by the user is returned.
8 changes: 8 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ 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`
- Object

Answer the following questions:

What does `console` store?

- console means display or print a message

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?

- console.log display data inside log. console.assert is evaluate if a condition is true. The `.` means telling console or any other function to access data inside that object just after the dot.
Loading