Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NW6 | Rabia Avci | JS1 Module | [TECH ED] Complete week 1 exercises | Week1 #130

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
7 changes: 5 additions & 2 deletions week-1/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?
// 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?

// answer
// we turned it comment with // for human consumption
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

9 changes: 8 additions & 1 deletion week-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// const age = 33;
// age = age + 1;

let age = 33;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

age = age + 1;


console.log(age);

7 changes: 6 additions & 1 deletion week-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// 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}`);

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

// By declaring cityOfBirth before using it in the template string, we can be able to print the string "I was born in Bolton" without any issues.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good and well explained



8 changes: 7 additions & 1 deletion week-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,7 +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
// Make and explain a prediction about why the code won't work
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

// answer
// The slice method is used on strings, not numbers. with "" made string then slice function worked
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well said!


12 changes: 10 additions & 2 deletions week-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";


//An identifier or keyword cannot immediately follow a numeric literal.ts(1351)

const twelve_HourClockTime = "20:53";
const twentyFour_hourClockTime = "08:53";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


console.log(twelve_HourClockTime);
6 changes: 6 additions & 0 deletions week-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ 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

console.log(count);

// = operator in line 3 is used to update the value of the count variable.
// It assigns a new value to the variable, which is the result of adding 1 to its previous value.
// After this line of code is executed, the count variable will have a new value of 1.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good!

10 changes: 10 additions & 0 deletions week-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

const num = 56.5467;
const wholeNumberPart = Math.floor(num);
const decimalPart = num - wholeNumberPart;
const roundedNum = Math.round(num)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good!


// You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Expand All @@ -8,3 +11,10 @@ const num = 56.5467;
// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number )

// Log your variables to the console to check your answers

console.log(wholeNumberPart);
console.log (decimalPart);
console.log (roundedNum);



4 changes: 4 additions & 0 deletions week-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ let lastName = "Johnson";

// Declare a variable called initials that stores the first character of each string in upper case to form the user's initials
// Log the variable in each case


let initials = `${firstName[0].toUpperCase()}${middleName[0].toUpperCase()}${lastName[0].toUpperCase()}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

console.log(initials);
8 changes: 8 additions & 0 deletions week-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,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 = filePath.slice(0, lastSlashIndex);
console.log(`The dir part of ${filePath} is ${dir}`);

const ext = base.slice(base.lastIndexOf(".") );
console.log(`The ext part of ${filePath} is ${ext}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good work!



15 changes: 15 additions & 0 deletions week-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;



// 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 several times to build an idea of what the program is doing

console.log(num);

// answer
//const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

//(maximum - minimum + 1) >> which equals 100.

//Math.random() >> returns a random number between 0 (included) and 1 (excluded):

//Math.floor(Math.random() * range) generates a random integer between 0 and range - 1. !!!

//This will print a random number between 1 and 100 to the console.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good :)

7 changes: 7 additions & 0 deletions week-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

- Pop up message box with a message

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

What effect does calling the `prompt` function have?

- Message box with a input space to get data in it

What is the return value of `prompt`?

- myName
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All makes sense

11 changes: 11 additions & 0 deletions week-1/explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ 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

//console.log: function log(...arg) {}

What output do you get?

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

//console: {}

Try also entering `typeof console`

//typeof console: "object"

Answer the following questions:

What does `console` store?
//The console variable stores a reference to the console object.


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

//The syntax console.log or console.assert means that you are calling a method on the console object. The . is used to separate the object name from the method name. For example, console.log is calling the log method on the console object.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

16 changes: 16 additions & 0 deletions week-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@ 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

// 2 functions :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one more function being called, three in total

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

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

// 3 variable reassignment statements : carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// percentageChange = (priceDifference / carPrice) * 100;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question asked for varible reassignment statements. One of these is in fact an intial reassignment. Which one is it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it this one "carPrice = Number(carPrice.replaceAll(",", ""))"?


// c) Identify all the lines that are variable declarations

// 4 variable declarations : let carPrice = "10,000";
// let priceAfterOneYear = "8,543";
// const priceDifference = carPrice - priceAfterOneYear;
// const percentageChange = (priceDifference / carPrice) * 100;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Number(carPrice.replaceAll(",", "")) is converting the string carPrice to a number. The replaceAll(",", "") part of the expression is removing all of the commas from the carPrice string.
// This is necessary because the Number() function cannot convert a string with commas to a number.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

26 changes: 23 additions & 3 deletions week-1/interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const movieLength = 8784; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const remainingHours = totalHours % 24;

const result = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`;
Expand All @@ -15,15 +12,38 @@ console.log(result);

// a) How many variable declarations are there in this program?

//* 7 const movieLength = 8784; // length of movie in seconds
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// const remainingSeconds = movieLength % 60;
// const totalMinutes = (movieLength - remainingSeconds) / 60;
// const remainingMinutes = totalMinutes % 60;
// const totalHours = (totalMinutes - remainingMinutes) / 60;
// const remainingHours = totalHours % 24;
// const result = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`;


// b) How many function calls are there?

//* const totalMinutes = (movieLength - remainingSeconds) / 60;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither of these are function calls I'm afraid. The brackets here are just being used for mathamatical reasons. There is one function call in the code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry Anna, I am not sure which is that "one function" ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Sorry for the misunderstanding, I'll explain again .This question asks you to identify functions in the code. You answered with two snippets of code:

//* const totalMinutes = (movieLength - remainingSeconds) / 60;
// const totalHours = (totalMinutes - remainingMinutes) / 60;

Unfortunately, neither of these snippets contain a function call. They do contain brackets, but they are not function calls. These are being used the same way brackets are used in maths.

There is a function called in the code. Can you find it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just "console.log(result);" ?

// const totalHours = (totalMinutes - remainingMinutes) / 60;


// c) Using documentation on MDN, explain what the expression movieLength % 60 represents

// * movieLength % 60 represents the remaining seconds after dividing movieLenght by 60 (seconds)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense :)


// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

//*const totalMinutes = (movieLength - remainingSeconds) / 60; It calculates total minutes by dividing movieLength - remainingSeconds by 60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



// e) What do you think the variable result represents? Can you think of a better name for this variable?

//* formattedMovieLength
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice name


// f) Think about whether this program will work for all values of movieLength.
// Think of what values may cause problems for the code.
// Decide the result should be for those values, then test it out.
// Can you find any values of movieLength which don't give you what you'd expect?

//*This program will work for all positive integer values of movieLength. However, it may not handle negative values. Like movieLength = -8784
// for negative values we can add a conditional check and take the absolute value.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense However, taking the absolute value of the var might cause confusion. Normally, you'd either throw an exception or return an empty value. It's important to be predicatable where possible or at least clearly document that the function behaves in an odd way and why

23 changes: 20 additions & 3 deletions week-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ const penceStringWithoutTrailingP = penceString.substring(
penceString.length - 1
);

console.log(penceStringWithoutTrailingP);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

console.log(paddedPenceNumberString);

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
console.log(pounds);

const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

console.log(pence);

console.log(`£${pounds}.${pence}`);

Expand All @@ -27,3 +34,13 @@ console.log(`£${pounds}.${pence}`);
// To begin, we can start with

// 1. const penceString = "399p": initialises a string variable with the value "399p"
// const penceStringWithoutTrailingP = penceString.substring( 0,penceString.length - 1);
// This line creates a new string (penceStringWithoutTrailingP) by using the substring method to extract a portion of penceString.
// It takes characters from the beginning (index 0) up to the length of penceString - 1, effectively removing the trailing "p".
// Line 10 This line pads the penceStringWithoutTrailingP with leading zeros to ensure that it has at least three characters. The resulting string is stored in paddedPenceNumberString.
// Line 14 Extracts the pounds part from paddedPenceNumberString by taking characters from the beginning up to the length of paddedPenceNumberString - 2.
// Line 21 Extracts the last two characters (representing pence) from paddedPenceNumberString and then pads the result with trailing zeros using padEnd to ensure it has two characters.

// Line 25 Prints the final formatted string by combining pounds, a dot, and pence, with a pound sign (£) at the beginning.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!