-
Notifications
You must be signed in to change notification settings - Fork 0
Javascript javascript1 week3 #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
7be8aed
939453a
67e6204
67ad54e
3cd6938
9bd9ac0
e60fddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>javasSript1-week3</title> | ||
| </head> | ||
| <body> | ||
| <script src="warmUp.js"></script> | ||
| <script src="travelTime.js"></script> | ||
| <script src="seriesDuration.js"></script> | ||
| <script src="notes.js"></script> | ||
| <script src="smartPhoneUsage.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| const notes = []; | ||
|
|
||
| function saveNote(content, id) { | ||
| notes.push({ content, id }); | ||
| } | ||
|
|
||
| function getNote(id) { | ||
| if (typeof id !== "number") { | ||
| console.log("Error: Invalid ID."); | ||
| return; | ||
| } | ||
|
|
||
| for (let note of notes) { | ||
| if (note.id === id) { | ||
| return note; | ||
| } | ||
| } | ||
|
|
||
| console.log("Error: Note not found."); | ||
| return null; | ||
| } | ||
|
|
||
| function logOutNotesFormatted() { | ||
| for (let note of notes) { | ||
| console.log(`The note with id: ${note.id}, has the following note text: ${note.content}`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| saveNote("Pick up groceries", 1); | ||
| saveNote("Do laundry", 2); | ||
|
|
||
| console.log(getNote(1)); | ||
|
|
||
| logOutNotesFormatted(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||
| const seriesDurations = [ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the series array is empty it will show 0%. Maybe add a check that if the array is empty, it will display for example, “There are no series”? |
||||||
| { | ||||||
| title: "Modern Family", | ||||||
| days: 4, | ||||||
| hours: 8, | ||||||
| minutes: 10, | ||||||
| }, | ||||||
| { | ||||||
| title: "Squid Game", | ||||||
| days: 0, | ||||||
| hours: 20, | ||||||
| minutes: 10, | ||||||
| }, | ||||||
| { | ||||||
| title: "Attack on Titan", | ||||||
| days: 1, | ||||||
| hours: 10, | ||||||
| minutes: 48, | ||||||
| }, | ||||||
| { | ||||||
| title: "Demon Slayer", | ||||||
| days: 1, | ||||||
| hours: 0, | ||||||
| minutes: 9, | ||||||
| }, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ]; | ||||||
|
|
||||||
| function logSeriesTime(seriesArray) { | ||||||
| const lifespanInMinutes = 80 * 365 * 24 * 60; // 80 years in minutes | ||||||
| let totalPercentage = 0; | ||||||
|
|
||||||
| seriesArray.forEach((series) => { | ||||||
| const seriesMinutes = | ||||||
| series.days * 24 * 60 + series.hours * 60 + series.minutes; | ||||||
| const percentage = (seriesMinutes / lifespanInMinutes) * 100; | ||||||
| totalPercentage += percentage; | ||||||
|
|
||||||
| console.log(`${series.title} took ${percentage.toFixed(6)}% of your life.`); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Numbers with .toFixed(6) precision look too long |
||||||
| }); | ||||||
|
|
||||||
| console.log( | ||||||
| `In total, you have spent ${totalPercentage.toFixed(6)}% of your life watching series.` | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| logSeriesTime(seriesDurations); | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||
|
|
||||||
| let activities = []; | ||||||
|
|
||||||
|
|
||||||
| let usageLimit = 100; | ||||||
|
|
||||||
|
|
||||||
| function addActivity(date, activity, duration) { | ||||||
| if (!date || !activity || !duration) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||
| console.log("All parameters are required!"); | ||||||
| } | ||||||
| activities.push({ date, activity, duration }); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| function setUsageLimit(limit) { | ||||||
| if (limit < 0) { | ||||||
| console.log("Limit cannot be negative!"); | ||||||
| } | ||||||
| usageLimit = limit; | ||||||
| console.log(`Usage limit set to ${limit} minutes.`); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| function showStatus() { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to undefined in duration, the reduce method produces NaN, which breaks the output. For example, the current code counts 30 + undefined + 40 + 40 = NaN |
||||||
| if (activities.length == 0) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| console.log("Add some activities before calling showStatus"); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| let totalDuration = activities.reduce((sum, act) => sum + act.duration, 0); | ||||||
|
|
||||||
| console.log(`You have added ${activities.length} activities. They amount to ${totalDuration} min. of usage.`); | ||||||
|
|
||||||
| if (totalDuration >= usageLimit) { | ||||||
| console.log("You have reached your limit, no more smartphoning for you!"); | ||||||
| } | ||||||
|
|
||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| addActivity("23/7-18", "Youtube", 30); | ||||||
| addActivity("23/7-18", "Spotify"); | ||||||
| addActivity("23/7-18", "Goodreads", 40); | ||||||
| addActivity("23/7-18", "Instagram", 40); | ||||||
|
|
||||||
| showStatus(); | ||||||
|
|
||||||
|
|
||||||
| setUsageLimit(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job, your function calculates the time correctly and outputs “8 hours and 38 minutes”. The logic with |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const travelInformation = { | ||
| speed: 50, | ||
| destinationDistance: 432, | ||
| }; | ||
|
|
||
| function calculateTravelTime(travelInfo) { | ||
| const time = travelInfo.destinationDistance / travelInfo.speed; | ||
| const hours = Math.floor(time); | ||
| const minutes = Math.round((time - hours) * 60); | ||
|
|
||
| return `${hours} hours and ${minutes} minutes`; | ||
|
|
||
| } | ||
|
|
||
| const travelTime = calculateTravelTime(travelInformation); | ||
| console.log(travelTime); // 8 hours and 38 minutes |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||
|
|
||||||
| const names = [ | ||||||
| "Peter", | ||||||
| "Ahmad", | ||||||
| "Yana", | ||||||
| "Rasmus", | ||||||
| "Samuel", | ||||||
| "Katrine", | ||||||
| "Tala", | ||||||
| ]; | ||||||
|
|
||||||
| const nameToRemove ="Ahmad" | ||||||
| const index = names.indexOf("Ahmad"); | ||||||
| if (name >-1){ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| names.splice(index,1); | ||||||
| } | ||||||
| console.log(names); | ||||||
| console.log(names[names.length]); | ||||||
|
|
||||||
|
|
||||||
| /* //another way? | ||||||
|
|
||||||
| const nameToRemove = "Ahmad"; | ||||||
| delete names[1]; | ||||||
|
|
||||||
| console.log(names); */ | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write here "return undefined" for clarity
And the error message can be made a little more specific (for example: "ID must be a number").