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
1 change: 0 additions & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ 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
//In line 3 we are taking the current value of count (which is 0) and adding 1 to it, and store the new value back into count" so now count is 1.the = is an assignment operator that Assigns values to variables so it takes the value on the right side (count + 1) and assigns it to the variable on the left side (count).
4 changes: 1 addition & 3 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ 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 = `${firstName[0]}${middleName[0]}${lastName[0]}`;
let initials = ``;

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

console.log(initials); //that should print "CKJ"
//so the code is going to check the first character of each variable that means “the character at position 0
9 changes: 2 additions & 7 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ 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 = filePath.slice(0, lastSlashIndex);// the slice is a method that cuts out part of a string and returns it as a new string. we use it here to get the dir part of the filePath
const ext = base.slice(base.lastIndexOf("."));//the ext is any thing after the last dot
//const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
//const dir = "/Users/mitch/cyf/Module-JS1/week-1/interpret";

console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);
const dir = ;
const ext = ;

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

//num is a random number between minimum and maximum (inclusive)
/*Math.random()

This gives you a random decimal number between 0 (inclusive) and 1 (exclusive) — meaning it can be 0, but never exactly 1.

Example outputs:
0.23

/*(maximum - minimum + 1)

This calculates how many whole numbers are in the range, including both ends.

Example:
100 - 1 + 1 = 100

So, there are 100 possible integers from 1 to 100.

/*Math.random() * (maximum - minimum + 1)

This scales the random decimal to the desired range size.

Example if Math.random() gave 0.57:
0.57 * 100 = 57
/*Math.floor(...)
The Math.floor() method in JavaScript is used to round a number down to the nearest integer, regardless of whether the number is positive or negative or has a decimal part.
Example:
Math.floor(57.8) → 57

/*+ minimum

Finally, we shift the range up so it starts at 1 instead of 0.
0–99 becomes 1–100*/

//The order for evaluation is:
/*maximum - minimum + 1

Math.random() * (that result)

Math.floor(...)

+ minimum*/

//I run the code several times and got different numbers one was 60 the other one was 88
8 changes: 2 additions & 6 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +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?


//if we have any instructions or explanations in our code without the computer executing them,we can use comments
//comments In javascript are created by using (//) for single line comments
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?
4 changes: 0 additions & 4 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@

const age = 33;
age = age + 1;
// This will give an error because we only use const for Fixed values so we can change const to let
let age = 33;
age = age + 1;
console.log(age); // that should print 34
3 changes: 1 addition & 2 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// 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}`);
//we have to declare the variable cityOfBirth with const or let before using it in the console.log statement
const cityOfBirth = "Bolton";
11 changes: 1 addition & 10 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
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
//the slice method is used for strings not numbers.
// Then run the code and see what error it gives.
//Uncaught TypeError: cardNumber.slice is not a function
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
//javascript can not slice a number
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
//I have added the console.log to see the output.
//there is another way to get the last 4 digits by using slice method is to convert the number to a string first by using toString() method
const last4Digits = cardNumber.toString().slice(-4); //it wil print "4213"
//If you want it to be a number, you can convert it back:
const last4Digits = Number(cardNumber.toString().slice(-4));
Now last4Digits will be 4213 as a number.
10 changes: 1 addition & 9 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";

//because variables names cannot start with a number
//I have changed the variable names to start with a letter
const twelveHourClockTime = "08:53 PM";
const twentyFourHourClockTime = "20:53";
console.log(twelveHourClockTime);
console.log(twentyFourHourClockTime);
// that should print "08:53 PM" and "20:53"
const 24hourClockTime = "08:53";
19 changes: 2 additions & 17 deletions 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,26 +12,11 @@ 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
//after running the code I found 5 function calls
//line 1: replaceAll
//line 2: replaceAll
//line 4: Number
//line 5: Number
//line 8: 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?
// after running the code i found an error in line 5 missing a comma in the replaceAll method to fix it I have added the comma

// c) Identify all the lines that are variable reassignment statements
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));


// d) Identify all the lines that are variable declarations
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Fist the car price is a string so we can't do any calculation on it yet
//so first we remove all the comma by using replaceAll method then we convert it to a number by using Number method
8 changes: 1 addition & 7 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ 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?
// there is 6 variable declarations

// b) How many function calls are there?
// there is only 1 function call which is the last onw in line 10 console.log(result);

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
//modulus operator % gives the remainder after dividing one number by another.which is in this case8784 % 60 = 24 second


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
//we are converting the seconds into minutes by dividing by 60

// e) What do you think the variable result represents? Can you think of a better name for this variable?
//Duration of the movie in hours, minutes, and seconds.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//yes it will work for all values of movieLength because the code will always convert seconds into hours, minutes, and seconds format.
7 changes: 1 addition & 6 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,4 @@ console.log(`£${pounds}.${pence}`);
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": a string variable with the value "399p"
//2.in line 3 because the value is a string we can't do any calculation on it yet we need to convert it to a number so the first step is to get rid of the letter P at the end so no matter how long the number is we get rid of the last character.
//3. in line 8 we make sure the number has 3 digits so if needed we add zeros at the start at the number
//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);: by taking away the last 2 digit we will get the pounds in this example the amount is 399 so by taking away the 99 we will get 3 pounds
//6. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: the same thing again now extracts the pence part by taking the last two digits if it is one digit we add 0 at the end to make it two digits
// in line 18 to print the result in the right format £ sign then pounds then a dot and then pence
// 1. const penceString = "399p": initialises a string variable with the value "399p"
6 changes: 0 additions & 6 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
the browser shows a popup message box with your text — "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`.

let myName = prompt("What is your name?");
The browser shows a popup with a text input box and OK / Cancel buttons.

What effect does calling the `prompt` function have?
It display a dialog box that prompts the user for input
What is the return value of `prompt`?
The text the user entered (string) if clicked OK, or null if Cancel
5 changes: 0 additions & 5 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ 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?
nothing

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

Try also entering `typeof console`

Answer the following questions:

What does `console` store?
console store Functions
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
console.log will print the message to the console
console.assert will print a message if the condition is false