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 4 exercises | Week 4 #177

Open
wants to merge 19 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
Binary file added .DS_Store
Binary file not shown.
3,616 changes: 3,616 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{

"name": "week-4",
"description": "",
"scripts": {
"test": "jest"
},

"devDependencies": {
"jest": "^29.7.0"
}
}
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
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;
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.


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

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";

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

// 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.charAt(0) + middleName.charAt(0) + lastName.charAt(0)}`
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}`);


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.
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
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.
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 :
// 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;

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

// 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.
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
// 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;
// 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)

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


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

//* formattedMovieLength

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


16 changes: 14 additions & 2 deletions week-3/debug/format-as-12-hours.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
let timeFormat= (Number(time.slice(0, 2)) - 12) < 10
if (timeFormat){
return `0${(Number(time.slice(0, 2)) - 12)}:${(time.slice(3, 5))} pm`;
}
return `${(Number(time.slice(0, 2)) - 12)}:${(time.slice(3, 5))} pm`;
}
return `${time} am`;
}
Expand All @@ -18,13 +22,21 @@ const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
"current output: %s, target output: %s",
"current output: %s target output: %s",
currentOutput2,
targetOutput2
);


const currentOutput3 = formatAs12HourClock("17:42");
const targetOutput3 = "05:42 pm";
console.assert ( currentOutput3 === targetOutput3,
`Current output: ${currentOutput3}, target output ${targetOutput3}`
);

// formatAs12HourClock currently has a 🐛

// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42"
// b) Check the assertion output and explain what the bug is
// c) Now fix the bug and re-run all your assertions

23 changes: 23 additions & 0 deletions week-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,26 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"


function getAngleType(angle){
if (angle === 90) {
return `${"Right Angles"}`;
}
else if (angle < 90){
return `${"Acute angle"}`;
}
else if (angle>90 && angle<180){
return `${"Obtuse angle"}`;
}
else if (angle === 180){
return `${"Straight angle"}`;
}
else if (angle>180 && angle<360){
return `${"Reflex angle"}`;
}

return "This is not an angle"
}

console.log(getAngleType(275));
Loading