Skip to content
Open
4 changes: 4 additions & 0 deletions Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ 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

// "=" is an assignment operator and so what line 3 is doing,
// is the new value is being assigned to the variable "count",
// which in this case is by using an expression
4 changes: 3 additions & 1 deletion Sprint-2/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const 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.

const initials = ``;
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

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

console.log(initials);
18 changes: 14 additions & 4 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@
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}`);
//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

// https://www.google.com/search?q=slice+mdn
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex + 1);
//or, const ext = filePath.slice(-3)

//const firstSlashIndex = filePath.indexOf("/");
//const dir = filePath.slice(firstSlashIndex + lastSlashIndex);
//const dir = filePath.slice(0 + lastSlashIndex);
//const dir = filePath.slice(0 + (lastSlashIndex - 1));
//const dir = filePath.slice(0 + (lastSlashIndex - 44));
const dir = filePath.slice(0, lastSlashIndex);

console.log(dir);
7 changes: 7 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ 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

console.log(num);
// This expression uses a function that returns a random number between (min)1 and (max)100.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range is right. The exercise also asks you to break the expression down. What does Math.random() give? What does Math.floor do to it? Which part makes the smallest value 1?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.random() gives a decimal between 0 < 1. I multiply it by 100 to stretch it out, then Math.floor() chops off the decimals to make it a whole number. That gives me 0 to 99. The minimum at the end just adds 1 to the whole thing, so now it goes from 1 to 100 instead of 0 to 99

// So every time I ran the program, it generated a different result (between 1 and 100).
// Math.random() gives a decimal between 0 < 1. I multiply it by 100 to stretch it out, then Math.floor() chops off the decimals
// to make it a whole number. That gives me 0 to 99.
// The minimum at the end just adds 1 to the whole thing, so now it goes from 1 to 100 instead of 0 to 99
6 changes: 4 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?

// the answer: by using "//" on each line
10 changes: 9 additions & 1 deletion Sprint-2/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// const age = 33;
// age = age + 1;

// the error is in line 4. It's a "TypeError: Assignment to constant variable", which was thrown because the value to
// a specific constant can only be assigned once (which is done in line 3 already).
// This wouldn't throw an error if instead of const, the let was used

let age = 33;
age = age + 1;
console.log(age);
8 changes: 7 additions & 1 deletion Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// 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}`);
// console.log(`I was born in ${cityOfBirth}`);
// const cityOfBirth = "Bolton";

// an error in line 4, "ReferenceError: Cannot access 'cityOfBirth' before initialization", which proves my assumption that
// the value of cityOfBirth should have been assigned prior to trying to print the string

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

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Before running the code, make and explain a prediction about why the code won't work
// 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

// I thought it was something to do with the slice "-4" (but i turned out i forgot that negative indices start at -1, not 0)
// The actual error indicates that on the 3rd line there is a typeError: "cardNumber.slice is not a function"
// Checked the error reference and decided to look more closely. Noticed that the card number is used as a number,
// so it answers why the function couldn't be called - because they can be only called on Arrays and Strings.
// Therefore, I'll add parentheses to turn the card number into a string (so that the function could be called)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your explanation of the error is good. But look at line 1. You changed cardNumber itself into a string. The exercise asks you to change the expression on line 3 instead. How can line 3 turn the number into a string?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. And yes, I need to focus more on what the exercise asks me to do. Fixed it by converting the number into string programmatically grammatically and then used the same method to extract the last 4 numbers

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

// SyntaxError: Invalid or unexpected token.
// it turns out that in JS variable names cannot begin with a number

const HourClockTime12 = "8:53pm";
const HourClockTime24 = "20:53";
console.log(HourClockTime24);
11 changes: 6 additions & 5 deletions Sprint-2/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 @@ -12,11 +12,12 @@ 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

// 4, 5, 8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at line 8 again. Is anything called there? And look at line 10. What is console.log(...)? Also, how many calls are there in total? Line 4 has two.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 in total?

// 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 was a syntax error, it can be fixed by adding the missing part (1 of 2 parentheses)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node says "missing ) after argument list", but a bracket was not the problem. Compare line 5 with line 4 in the original. What was missing between the two arguments? And which line was it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure

// c) Identify all the lines that are variable reassignment statements

// 4, 5
// d) Identify all the lines that are variable declarations

// 1, 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 1 and 2 are right. A declaration is any line that creates a new variable. Look at lines 7 and 8. Do they create new variables?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they sure do. Silly mistake

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// It turns/converts a string into a number by removing the comma and quotation marks (since they are non-number values)
12 changes: 8 additions & 4 deletions Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ 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?

// 6
// b) How many function calls are there?

// 5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which five? % and / are operators, not function calls. Look for a name followed by (...).

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// The remainder operator calculates the remainder of movie in seconds (it does it by dividing the number by 60)

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// total minutes (in seconds) - remaining seconds = remaining seconds. Then converts the seconds into minutes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 4 starts with movieLength, not total minutes. What is 8784 - 24? Why does the program take the 24 seconds away before it divides by 60?

// e) What do you think the variable result represents? Can you think of a better name for this variable?

// It represents how much of the movie is left to watch. Maybe something like remainderOfTheMovie

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run the file. It prints 2:26:24. movieLength is the length of the whole movie. So is result the time left to watch?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// It won't work with all values. Most importantly, the value must be strictly numeric and positive

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try some values and write down what each one prints. Try 59, -90 and 90.5. Would you show a time that way?


console.log(result);
14 changes: 11 additions & 3 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
penceString.length - 1,
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
paddedPenceNumberString.length - 2,
);

const pence = paddedPenceNumberString
Expand All @@ -24,4 +24,12 @@ 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"
// 1. const penceString = "399p": initializes a string variable with the value "399p"
// 3-6. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// : initializes a variable, the value of which is turned to numerical by removing the letter "p".
// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Here the padstart function turns "399" string into 399 number (it could add some zeroes in front, but here it serves as a converter)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does padStart turn "399" into a number? Change line 1 to "5p" and run it. What does each line give now? That shows what padStart and padEnd are for.

// 9-12. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// Here the goal is to initialize a variable of Pounds (with the value of 3), by splitting it from 99, and it's done using the substring method
// 14-16. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// Here the pence variable is introduced with the value of 99 and it's done by taking the 399 and removing 3 using substring and padEnd methods.
2 changes: 2 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Let's try an example.
In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`;

What effect does calling the `alert` function have?
It is used to alert the user about something very important. One cannot access the rest of their screen until they manually turn it off (that's why it should only be used when no other way of notification would do the job).

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`?
It also pops up/overlays the screen, in this case it asks the user to input some data. The return value of the prompt is the one specified in the variable attached to it (in this case,"Diana").
4 changes: 4 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ 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`

Answer the following questions:

What does `console` store?
It stores the things shown above: errors, warnings, messages, info
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
I found that console.assert prints an error message only if a given condition is false (unlike console.log), in which case the `.` might mean the point from which they differentiate.