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
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 @@ -4,3 +4,5 @@ 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 3 is updating the value of the variable `count`. The `=` operator is an assignment operator, which means it takes the value on the right side (in this case, `count + 1`) and assigns it to the variable on the left side (`count`). So, it takes the current value of `count`, adds 1 to it, and then stores that new value back into `count`.
3 changes: 2 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,7 @@ 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[0] + middleName[0] + lastName[0];

console.log(initials);
// https://www.google.com/search?q=get+first+character+of+string+mdn
11 changes: 7 additions & 4 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
//console.log(lastSlashIndex);
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
//console.log(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 = ;

const dir = filePath.slice(0, lastSlashIndex);
const ext = filePath.slice(filePath.lastIndexOf("."));
console.log(dir);
console.log(ext);
// https://www.google.com/search?q=slice+mdn
2 changes: 1 addition & 1 deletion Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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
Comment on lines 6 to 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.

I just noticed you missed this exercise.

Could you give a precise description what each of these expressions does, and the range of the numbers it may produce?

  1. Math.random()
  2. Math.random() * (maximum - minimum + 1)
  3. Math.floor(Math.random() * (maximum - minimum + 1))
  4. Math.floor(Math.random() * (maximum - minimum + 1)) + minimum

Note: To describe a range of numbers, we could use the concise and precise interval notation:

  • [, ] => inclusion
  • (, ) => exclusion

For example, $x$ is a number in $[1, 10)$ means:

$x$ is a number between 1 and 10, including 1 but excluding 10.

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?

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

// We use let because the value of age needs to change.


let age = 33;

age = age + 1;
console.log(age);
9 changes: 8 additions & 1 deletion Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// 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";

// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

const cityOfBirth = "Bolton";

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

// 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 predict the code will give an error because cardNumber is a number, and slice() works on strings.
//const last4Digits = String(cardNumber).slice(-4);
const cardNumber = 4533787178994213;

const last4Digits = String(cardNumber).slice(-4);
4 changes: 3 additions & 1 deletion 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";
//const 24hourClockTime = "20:53";
// I predict the code will give an error because a variable name cannot start with a number.
const twelveHourClockTime = "8:53pm";
Comment on lines +2 to +4

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 understanding is correct. There were two variable declarations in the original code though.

20 changes: 17 additions & 3 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ 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;

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
Expand All @@ -19,4 +18,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?
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of thi e() and Number().

//a) There are 4 function calls. They are on lines 4 and 5. Each line contains two function calls: replaceAll() and Number().
//b) The error comes from line 5. The replaceAll() function is missing a comma between its two arguments. The problem can be fixed by adding a comma: replaceAll(",", "").
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
//c) The variable reassignment statements are lines 4 and 5.
//d) The variable declaration statements are lines 1, 2, 7 and 8.

//e) The expression removes the comma from the price string and then converts the result from a string into a number, so JavaScript can use it for calculations.

//carPrice = Number(carPrice.replaceAll(",", ""));
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

//const priceDifference = carPrice - priceAfterOneYear;
//const percentageChange = (priceDifference / carPrice) * 100;

8 changes: 8 additions & 0 deletions Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ 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. It is on line 10: console.log(result);
// c) The % operator gives the remainder after dividing movieLength by 60.
// c) The % operator gives the remainder after dividing movieLength by 60. 60 represents the number of seconds in one minute.
// d) It subtracts the remaining seconds from the movie length and then divides by 60 to convert the remaining seconds into total minutes.
// e) The variable result represents the movie duration in hours, minutes and seconds. A better name would be movieDuration.

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 name movieDuration does not quite indicate the value stored in the variable
is a formatted string in the form "2:12:02".

Could you suggest a more descriptive name?

// f) The code works for different positive values of movieLength because it converts seconds into hours, minutes and seconds. It expects movieLength to be a number representing seconds.
6 changes: 6 additions & 0 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ console.log(`£${pounds}.${pence}`);

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

// 2. const penceStringWithoutTrailingP = ...: removes the final "p" from "399p", leaving "399"
// 3. padStart(3, "0"): makes sure the pence string has at least 3 characters by adding zeros to the beginning if needed.
// 4. const pounds = ...: takes all the characters except the last two, giving the pounds part of the price.
// 5. const pence = ...: takes the last two characters as the pence part and makes sure it has two characters.
// 6. console.log(`£${pounds}.${pence}`): displays the final price in pounds and pence format.
3 changes: 3 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ Now try invoking the function `prompt` with a string input of `"What is your nam

What effect does calling the `prompt` function have?
What is the return value of `prompt`?
Calling the alert function displays a pop-up message to the user.

Calling the prompt function asks the user for information. The return value of prompt is the information the user entered.

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.

When a program uses prompt() to ask the user for input, how can it tell whether the user clicked "OK" or "Cancel"?

2 changes: 2 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ What output do you get?
Now enter just `console` in the Console, what output do you get back?

Try also entering `typeof console`
The console stores an object containing functions and other properties used for interacting with the console.

The syntax console.log or console.assert accesses a function that belongs to the console object. The . means access a property or function that belongs to an object.
Answer the following questions:

What does `console` store?
Expand Down
Loading