Skip to content
Closed
5 changes: 5 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,8 @@ 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

// Answer:

// Line 3 takes the current variable count with a value, that was initially assigned to 0 and then adds 1 to it.
// Then store result back in the same variable.
10 changes: 8 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ 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 = ``;
// Answer:

// https://www.google.com/search?q=get+first+character+of+string+mdn
// There are two ways to access an individual character in a string. The first is the charAt() method. Another -
// is to treat the string as an array-like object, where individual characters correspond to a numerical index.
// I choose the second one.

let initials = firstName[0] + middleName[0] + lastName[0];
console.log(initials);

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

// https://www.google.com/search?q=slice+mdn
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex + 1);
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The extension part of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
10 changes: 10 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,18 @@ 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

// 1st Step Math.random() to generate random number between 0 and 1.
// 2nd Step is (maximum - minimum + 1)
// 3d Step is Math.random() * (maximum - minimum + 1)
// 4th Step is Math.floor(Math.random() * (maximum - minimum + 1))
// 5th Step is Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
// This is about simulating random numbers. Useful for quizzes, games, for anything that needs random choice.
// Math.floor() to round float number to the greatest than or equal integer.
// + minimum in this case 1, to make the random possible choice real one, not 0.
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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?
3 changes: 3 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

const age = 33;
age = age + 1;

// This is not possible because the variable is const type, so immutable.
// TypeErrore: Assignment to constant variable.
3 changes: 3 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

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

// That is because the console.log is going first than variable which even doesn't exist.
// Causes a ReferenceError.
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const cardNumber = 4533787178994213;
const cardNumber = `4533787178994213`;
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

// My prediction: the code is not working because cardNumber variable contains numbers but not string.
// TypeError: ReferenceError
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const HourClockTime = "20:53";
const hourClockTime = "08:53";

// Mistake is that the variables' name start with number.
// There are some rules how to name variables:
// 1.Name shouldn't start with number.
// 2.Variable name shouldn't contain any spacial characters or spaces.
// 3.Variable name shouldn't be a specific key word.
8 changes: 7 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 @@ -12,11 +12,17 @@ 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
// 5

// 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 "","" after argument list Line: 5

// c) Identify all the lines that are variable reassignment statements
// Lines: 4 and 5

// d) Identify all the lines that are variable declarations
// Lines: 1, 2, 7, 8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// The Number function changes the type of value from string to number. carPrice.replaceAll(",","") replaces all
// comas in string on space.
11 changes: 10 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ 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?
// 1

// 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 returns the remainder left over when one operand is divided
// by a second operand. It always takes the sign of the dividend. 8784 % 60 = 24 It calculates the leftover
// seconds after full minutes are taken out.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// const totalMinutes = (movieLength - remainingSeconds) / 60;
// TotalMinutes means movie length in seconds minus remaining seconds and transforms it into minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// e) What do you think the variable result represents? It represent time in the format: HH.MM.SS
// The variable Result has a too general name. I would rename it. Foe example, FormattingTime

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// It works well for integers. Doesn't work for negative numbers and strings. Unless we convert them. for 0 it gives 0:0:0
25 changes: 24 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,27 @@ 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"

// 2. const penceStringWithoutTrailingP: give a string variable without the last character, "p".
// We use the method substring to get under 0-index string character as `a start-parameter` of substring and inside the substring function
// calling function (penceString.length - 1) to get `the end` parameter of substring. As a result, we got `399` without `p`.

// 3. const paddedPenceNumberString: the padStart method pads 0 of the start nceStringWithoutTrailingP variable until
// it becomes a 3-character length.

// 4. const pounds: give a string variable without the last 2 characters, "99".
// We use the method substring to get under 0-index string character as `a start-parameter` of substring and inside the substring
// calling function (paddedPenceNumberString.length - 2) to get `the end` parameter of substring (not included).
// As a result, we got `3` without `99`.

// 5. const pence:
// - .substring(0, paddedPenceNumberString.length - 2) to substring the last two string characters "99" from "399"
// length -2 of "399" is "3", "3-2=1", so under index `0` as `a start-parameter` we have figure 3 and under index `1`as
// `the end parameter` we have `9`(not included), As a result, we subtracted everything except "99".
// - .padEnd(2, "0") We use the method padEnd to get at least 2 characters by adding "0" at the end of our string. S0 we get 99.

// 6. console.log(`£${pounds}.${pence}`): to return the final price we use two gotten variables above and before them
// £ sign and dot between them. As a result, we have: "£3.99" string.

// In summary, the described step-by-step small program above gives output price in pounds and pence.
9 changes: 9 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

My answer: it opens a popup alert-box chrome://new-tab-page-says in the browser with a message Hello world!, and it is not going to be disappear until press buttons "ok" or "cancel".

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

My answer: let myName = prompt("What is your name?")

What effect does calling the `prompt` function have?

My answer: it invokes the popup box chrome:// new-page-says and an input field for user to enter his name. User prompt to enter his name and press "ok" or "cancel", then box disappear.

What is the return value of `prompt`?

My answer: return some string typed by user or Null if the user press button "cancel".
9 changes: 8 additions & 1 deletion Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

My answer: Undefined

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

Try also entering `typeof console`

Answer the following questions:
Answer the following questions: built-in 'object' in Console developer environment on Chrome V8.

What does `console` store?

My answer: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} The object `console` stores a list of logging and debugging functions.

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

My answer: .log and .assert are methods(functions) attached to object `console`. The "." (dot) is a operator or member operator. `console.log` inspect variables, debug code. `console.assert` test condition. If the condition is False, it log a message about error. If Tru, it records nothing(console.log(condition, message)).
Loading