Skip to content
Open
Show file tree
Hide file tree
Changes from 14 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
6 changes: 6 additions & 0 deletions Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ let count = 0;
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



Line 1 is a declaration and Line 3 is a statement that changes the value of the variable 'count' by adding 1 to its current value.

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 answer is right. But run node 1-count.js. Node stops here with a SyntaxError. It tries to read your sentence as code.

Start each answer line with //. Then the line is a comment, and node skips it. Do this in every .js file.

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.

All your answers are comments now, and every file runs. Good.

The variable is changing from 0 to 1 after line 3 is executed after we used 'let' to declare the variable 'count' and then we used the = operator to assign the new value.
10 changes: 9 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,14 @@ 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 = ``;
//const initials = ``;

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


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

console.log (initials);



6 changes: 4 additions & 2 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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 = ;
const dir = filePath.slice(1, lastSlashIndex);

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.

Log dir. It prints Users/mitch/.... The path starts with a /. In the diagram, the root / is part of dir. Which index does your slice start at?

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.

dir is right now.


const ext = base.slice(45);

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.

Log ext. What does it print? base is "file.txt", only 8 characters. What is at index 45? Line 13 finds the last /. Could you find the . the same way?

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.

ext prints .txt now, and it works for any file name. Good.



// https://www.google.com/search?q=slice+mdn
18 changes: 18 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ 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

Num represents a random whole number between the value of 'minimum' (1) and 'maximum' (100)
By creating a constant variable called 'num' and assigning it the value of the expression, we can generate a random number within the mentioned range.

When running the program we have to understand how the expression works:

we have to work out the parentheses first, maximum - minimum + 1, which is equal to 100 - 1 + 1 = 100.
so: Math.floor(Math.random() *100) + 1
Math.random() produces a pseudo-random decimal number that is greater than or equal to 0 but less than 1.

Next, we need to multiply the result from Math.random() by 100. This may give us the result of a decimal between 0 and 100.
Next step is to use Math.floor() wich then rounds down the decimal number to the nearest whole integer. This then means that the result will be a whole number between 0 and 99.
But we want 1-100, so we add 1 to the result of Math.floor() this then changes the range to be inclusive of 1 and 100.
After adding 1, we need to round down the number to the nearest interger, this is done by using Math.floor(). After this we will have a whole number between maximum and minimum.

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 4 again. How many times is Math.floor used? Does it happen before or after + minimum?

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 21 is right. But line 29 still says you round down after adding 1. That is the opposite order. Delete line 29.

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 29 is gone, and the order is right now.

Therefore, the final result of num will be a random whole number between 1 and 100, inclusive.
6 changes: 5 additions & 1 deletion Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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 don't

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 node 0.js. Line 1 has no //, so node still reads it. Also, the original line 2 is now split over lines 2 and 3. Where does each // need to go?

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.

Both lines are comments now, and the file runs.

// want the computer to run these 2 lines - how can we solve this problem?

We can add 2 forward slashes , by typing two forward slashes ( // ) at the beginning of the line, we can turn that line into a comment. This then means that the computer will completely ignore that line and not run it.

15 changes: 13 additions & 2 deletions Sprint-2/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age= 33;
age = age + 1;

console.log (age);



const means that the variable age is a constant and cannot be reassigned. When we try to reassign the value of age by adding 1 to it, we get an error.
This is because we cannot change the value of a constant variable once we have created it.

If we want to change the value of age, we need to use let instead of consts when declaring the variable. this then allows us to reassign the value of age by adding 1 to it.



12 changes: 12 additions & 0 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

error

console.log(`I was born in ${cityOfBirth}`);

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 6 and 7 are the original code, and they still run. So the file still stops with the same error. Your fixed version on lines 16 and 17 is right. What should happen to lines 6 and 7?

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 file runs now, and your error / right way layout is clear. Good.

const cityOfBirth = "Bolton";

The reason there is an error is because we are using the wrong order, we are telling Javascript to print the value of cityOfBirth before creating a variable.
So we need to create the variable first, this is because Javascript runs code from top to bottom, so a variable that is declared with const needs to be declared before using it.



right way

const cityOfBirth = "Bolton";
console.log (`I was born in ${cityOfBirth}`);
15 changes: 14 additions & 1 deletion Sprint-2/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";
const last4Digits = cardNumber.slice(-4);

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


console.log(last4Digits);

I think that Javascript will give an error because .slice() is normally used with strings, and cardNumber is currently a number.
const cardNumber = 4533787178994213; does not have any quotation marks, so Javascript will treat it as a number.

When run with console.log(last4Digits); we get --> TypeError : cardNumber.slice is not a function

.slice is a method used with strings and we need to make cardNumber into a string by adding quotation marks.I

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 prediction and the error message are good. But you changed cardNumber on line 1 into a string. The exercise asks you to change the expression on line 2 instead. How can line 2 turn the number into a string?

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 21 is exactly right. Just check it says cardNumber, with a capital N.

You pass cardNumber to the function String. It converts the number into a string. You explained this in your comment. But in your code, you made it a string by hand. You put quotes around the number on line 1.

Please update your code to use String. That is what it is designed to do. Put line 1 back to the number, then change line 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.

Line 2 uses String now, and line 1 is a number again. Your code is right, and line 21 explains it well.

Some old notes still describe your first fix. Line 19 ends with "by adding quotation marks". Lines 23 and 24 say you put quotes around the number on line 1. You don't do that any more. Please change the end of line 19 to match your new fix, and delete lines 23 and 24.


const cardNumber = 4533787178994213 to const cardNumber = "4533787178994213";
Now cardNumber is a string, then we can re run the code with console.log(last4Digits); to get the correct code.
14 changes: 12 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const Hour12ClockTime = "8:53pm";

console.log(Hour12ClockTime);

const hour24ClockTime = "20:53";

console.log(hour24ClockTime);




Variable names cannot start with a number as Javascript doesnt allow it, it can contan number inside the variable but not at the beginning.
10 changes: 6 additions & 4 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ 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

There are 5 function calls, two function calls on line 4 = Number(...) and replaceAll(...) two more on line 5 = Number(...) and replaceAll(...) and finally on line 10 = 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?

SyntaxError: missing a comma to separate the arguments

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.

Right reason. Which line is the error on? The comma is still missing on line 5, too.

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 comma is fixed and the file runs. The question also asks which line the error was on. Add the line number.

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 5 is right. Done.

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

carPrice = Number(carPrice.replaceAll(",", "")); and priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// d) Identify all the lines that are variable declarations

There are 4 variable declarations- 1) let carPrice = "10,000"; 2) let priceAfterOneYear = "8,543";
3) const priceDifference = carPrice-priceAfterOneYear; 4) const percentageChange = (priceDifferenc/carPrice)* 100;
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
Number(carPrice.replaceAll(",", "")); is a string and the expression removes the comma from "10,000" and converts "10000" from a string into the number 10000.
14 changes: 10 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,20 @@ 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 variable Declarations, every const creates a new variable.
// b) How many function calls are there?

1 function calls is console.log() as it is a function being called and told to do the job
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
movieLength % 60 means finding the remainder after dividing movieLength by 60. movieLength = 8784 so we need to do: 8784 % 60 = 146.4 and the nearest whole integer is 146.

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.

Is 146.4 what 8784 % 60 gives? Or is that 8784 / 60? Try both in node and compare.

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 25 is right: 8784 % 60 is 24. But line 23 says 8784 % 60 = 146.4. Did you mean / instead of % on line 23?

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.

Fixed, thanks.

So there are 146 complete groups of 60. we then multiply; 146 x 60= 8760 --> 8784 - 8760 = 24 so 8784 % 60 = 24 and therefore remainingSeconds becomes 24.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
const totalMinutes = (movieLength - remainingSeconds) / 60 ---> if we break it down -> movieLength = 8784 remainingSeconds = 24
we then subtract remainingSeconds from movieLength= 8784 -24 = 8670. we then have to divide it by 60 which gives us 8760 / 60 = 146. we divide because there is 60 seconds in one minute, and right now we are converting seconds into minutes.
// e) What do you think the variable result represents? Can you think of a better name for this variable?

The result variable will show us he complete results from our calculations of = totalHours:remainingMinutes:remainingSeconds.
result = 2:26:24
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
I tested differnet codes: 7965 and I got the result 2:12:45 and i also tried : 2654 and got 0:44:14.

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.

Both of those work. Now try 59, -90 and 90.5. What does each one print? Does the code work for all values?

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.

Good. 59, -90 and 90.5 all answered.

13 changes: 13 additions & 0 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ console.log(`£${pounds}.${pence}`);
// The program then builds up a string representing the price in pounds

// You need to do a step-by-step breakdown of each line in this program

// 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"
const penceString = "399p"; creates a variable. This string represents a price in pence.
We then count the variable penceString using index starting from 0 and ending at 3.
.length tells us how many characters there are in a string which in this case its 4.
Because we know penceString = 4 we have to then subtract 4-1=3.---> we have penceString.length-1 which then becomes 4 - 1 = 3.
we are doing this so we can remove the final character.
penceString.substring(0, 3) will give us "399"
padStart() adds characters to the beginning of a string until it reaches a certain length, we asked for a length of 3.
The final two characters represents pence and we are trying to get poundsright now.Because we have "399" and the last two digits represents pence and the character before that represents pounds.
we are subtracting 2 because when using substringwe remove the part that belongs to pence, 3 | 99
padEnd count at the position of the last two digits and take everything after that so we can get the right value for pence.
here we are making sure that pence has 2 characters- this generally means that if the string does not add to 2 characters, add 0 to the end until its 2 characters Long
console.log displays something in the console as the backticks create a template literal inside it we put our variables.
9 changes: 9 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,17 @@ 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?
alert is a function name and we are storing Hello world! as the argument. alert then alerts the screen with a banner with chrome url that says "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`.

when using prompt and input the argument "what is your name?" and press enter we get a text box where it asks for your name(because of the variable) and a cancel and ok button. you input the question, the function is what prompt() gives back and that is the return value.

What effect does calling the `prompt` function have?

the prompt function displays a pop-up input box asking "what is your name?" prompt asks a question and users give an input and the prompt then returns that input.


What is the return value of `prompt`?

we need to create a variable first ' const myName = prompt ("what is your name?"); that stores the returned value in the variable.
18 changes: 17 additions & 1 deletion Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

it showed the log function rather than calling it f log() { [natice code]}

Now enter just `console` in the Console, what output do you get back?

the console objects and properties/functions ie, {debug:f, error:f, info:f, log:f, warn:f,...}

Try also entering `typeof console`

we get 'object' why?--> because console is an 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?

console is an object that contains properties/functions used to interact with chrome/browser developer console.

What does the syntax `console.log` or `console.assert` mean?

console.log = access the log property inside the console object.
console.assert = access the assert property inside the console object.

In particular, what does the `.` mean?

The dot . is called the property accessor operator used to access an object's property. It also tells Javascript to go inside this object and get a particular property.
Loading