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

count = count + 1;

// = is an assign operator so if count = 0 at line 1 then at line 3 count = 0 + 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
6 changes: 5 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,10 @@ 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 = firstName.charAt(0);
middleName.charAt(0);
lastName.charAt(0);

console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn
9 changes: 6 additions & 3 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ 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(0, lastSlashIndex);
const ext = filePath.slice(-4);

// https://www.google.com/search?q=slice+mdn
console.log(dir);
console.log(ext);

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

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
//Math.floor is rounding down number from float number to integer
//Math.random create a random decimal number(float) from 0 up to 1 we then multiply it by (100) and add 1(minimum)
// variable num represents a random integer ranging from 1 to 100
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
Expand Down
5 changes: 4 additions & 1 deletion Sprint-2/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?
We don't want the computer to run these 2 lines - how can we solve this problem?

//We have to use the // to comment a single line
/* or use /* to comment multiple lines */
2 changes: 2 additions & 0 deletions Sprint-2/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

const age = 33;
age = age + 1;
/* const means that the variable cannot be reassigned we should use let keyword instead.
TypeError displayed due to that. */
5 changes: 5 additions & 0 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";

//The error we see it's a ReferenceError which tells us that we cannot access variable before initialization.

/*JavaScript reads the code from top to bottom, order is wrong.
Task cannot be executed as variable cannot be access before being created. */
7 changes: 6 additions & 1 deletion Sprint-2/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().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

/*The code would not work because cardNumber variable stores number data type and .slice() method cannot be used on a number,
if we run the code without changing data type to string we will get TypeError displayed
I used .toString() method to convert cardNumber variable to a string and then used .slice(-4) to get the last 4 digits. */
2 changes: 2 additions & 0 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";

//A variable cannot start with a number, SyntaxError is displayed.
9 changes: 8 additions & 1 deletion 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 @@ -20,3 +20,10 @@ 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?

//a)There are 5 function calls on line 4, 5 and 10.
//b) error is coming from line 5, there is missing , between arguments in replaceAll method
//c) line 4 and 5
//d) line 1,2 and 7,8
/*e) replaceAll method inside of () replace comma with nothing, removing it from the string,
then Number converts string to number data type */
9 changes: 8 additions & 1 deletion Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = -8784.08; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -23,3 +23,10 @@ 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

//a) There are 6 variable declarations.
//b)There is 1 function call on line 10
//c) movieLength use remainder operator % . movieLength % 60 returns the remaining seconds after dividing the movie length into full minutes.
//d) expression assigned to totalMinutes calculate movie time in whole minutes.
//e) This variable represent total movie time in hh/mm/ss format, we could name it totalTime or totalMovieTime.
//f) Yes, the code works correctly when movieLength is non-negative whole number that represents seconds.
18 changes: 16 additions & 2 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,18 @@ 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"
// 3. penceStringWithoutTrailingP is created using substring() on penceString. The purpose is to remove the trailing "p"..
// 4. value for substring method to start from which is beginning of the string.
// 5. penceString variable uses argument of .length to -1 (p) from the value, 399p becomes 399.
// 6. substring method closure
// 8. paddedPenceNumberString uses padStart() to make the string at least 3 characters long, adding "0" at the beginning when needed.
// 9. pounds variable assigns paddedPenceNumberString that uses substring method which returns part of the string from the start of index.
//10. value for substring method which is beginning of the string.
//11. paddedPenceNumberString variable is using length argument and deduct 2 out of length of the string 399 become 3 as pounds.
//12. Closes the substring method.
//14. pence variable created from paddedPenceNumberString
//15. substring method called which value of is in the parenthesis (variable length -2 characters)
//16. method padEnd is called that contains at least 2 character and adds 0 at the end if needed.
//18. we log £3.99 in the console.
8 changes: 8 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ In the Chrome console, invoke the function `alert` with one argument, the string

What effect does calling the `alert` function have?

### Alert would display a pop up window which can be used to display for example error messages.

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`.

### let myName = prompt("What is your name?");

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

### Prompt display text input option that we can store in a variable.

### prompt returns text that user entered in the input field
11 changes: 11 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ 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?

### the output is console object with it's available properties and methods.

Try also entering `typeof console`

### console is an object

Answer the following questions:

What does `console` store?

### Console store functions that can interact with the console object

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

### "." is used to access a property or method inside an object eg. console.log access the log function where .assert access the assert function
4 changes: 2 additions & 2 deletions Sprint-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This README will guide you through the different sections for this week.

## 1 Exercises

In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation.
In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript

Expand All @@ -28,7 +28,7 @@ You must use documentation to make sense of anything unfamiliar - learning how t

You can also use `console.log` to check the value of different variables in the code.

https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript

## 4 Explore - Stretch 💪

Expand Down
Loading