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
15 changes: 15 additions & 0 deletions javascript/javascript1/week3/index.html
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>
36 changes: 36 additions & 0 deletions javascript/javascript1/week3/notes.js
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;
Copy link

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

Suggested change
return;
return undefined;

And the error message can be made a little more specific (for example: "ID must be a number").

}

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();

46 changes: 46 additions & 0 deletions javascript/javascript1/week3/seriesDuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const seriesDurations = [
Copy link

Choose a reason for hiding this comment

The 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”?
This is not necessary, but will make the code more reliable

{
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,
},
Copy link

Choose a reason for hiding this comment

The 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.`);
Copy link

Choose a reason for hiding this comment

The 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);
47 changes: 47 additions & 0 deletions javascript/javascript1/week3/smartPhoneUsage.js
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) {
Copy link

Choose a reason for hiding this comment

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

The if (!date || !activity || !duration) prints a message, but does not stop the object from being added

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() {
Copy link

Choose a reason for hiding this comment

The 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) {
Copy link

Choose a reason for hiding this comment

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

Suggested change
if (activities.length == 0) {
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!");
}

Copy link

Choose a reason for hiding this comment

The 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();
16 changes: 16 additions & 0 deletions javascript/javascript1/week3/travelTime.js
Copy link

Choose a reason for hiding this comment

The 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 Math.floor and Math.round for hours and minutes works perfectly

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
29 changes: 29 additions & 0 deletions javascript/javascript1/week3/warmUp.js
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){
Copy link

@l0rians l0rians Feb 21, 2025

Choose a reason for hiding this comment

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

Suggested change
if (name >-1){
if (index >-1){

names.splice(index,1);
}
console.log(names);
console.log(names[names.length]);


/* //another way?

const nameToRemove = "Ahmad";
delete names[1];

console.log(names); */