diff --git a/javascript/javascript1/week3/index.html b/javascript/javascript1/week3/index.html new file mode 100644 index 00000000..c3ab94e4 --- /dev/null +++ b/javascript/javascript1/week3/index.html @@ -0,0 +1,15 @@ + + + + + + javasSript1-week3 + + + + + + + + + diff --git a/javascript/javascript1/week3/notes.js b/javascript/javascript1/week3/notes.js new file mode 100644 index 00000000..511ca72e --- /dev/null +++ b/javascript/javascript1/week3/notes.js @@ -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(); + diff --git a/javascript/javascript1/week3/seriesDuration.js b/javascript/javascript1/week3/seriesDuration.js new file mode 100644 index 00000000..5122c02a --- /dev/null +++ b/javascript/javascript1/week3/seriesDuration.js @@ -0,0 +1,46 @@ +const seriesDurations = [ + { + 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, + }, + ]; + + 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.`); + }); + + console.log( + `In total, you have spent ${totalPercentage.toFixed(6)}% of your life watching series.` + ); + } + + logSeriesTime(seriesDurations); \ No newline at end of file diff --git a/javascript/javascript1/week3/smartPhoneUsage.js b/javascript/javascript1/week3/smartPhoneUsage.js new file mode 100644 index 00000000..0be02a6a --- /dev/null +++ b/javascript/javascript1/week3/smartPhoneUsage.js @@ -0,0 +1,47 @@ + +let activities = []; + + +let usageLimit = 100; + + +function addActivity(date, activity, duration) { + if (!date || !activity || !duration) { + 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() { + if (activities.length == 0) { + 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!"); +} + +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(); diff --git a/javascript/javascript1/week3/travelTime.js b/javascript/javascript1/week3/travelTime.js new file mode 100644 index 00000000..fe722ce1 --- /dev/null +++ b/javascript/javascript1/week3/travelTime.js @@ -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 \ No newline at end of file diff --git a/javascript/javascript1/week3/warmUp.js b/javascript/javascript1/week3/warmUp.js new file mode 100644 index 00000000..be813277 --- /dev/null +++ b/javascript/javascript1/week3/warmUp.js @@ -0,0 +1,29 @@ + +const names = [ + "Peter", + "Ahmad", + "Yana", + "Rasmus", + "Samuel", + "Katrine", + "Tala", +]; + +const nameToRemove ="Ahmad" +const index = names.indexOf("Ahmad"); +if (name >-1){ + names.splice(index,1); +} +console.log(names); +console.log(names[names.length]); + + +/* //another way? + +const nameToRemove = "Ahmad"; +delete names[1]; + +console.log(names); */ + + +