0% found this document useful (0 votes)
4 views1 page

Miner New Easy

The document contains a JavaScript code snippet that manages user statuses, toggles their active state, and fetches data from an API. It also includes a counter that increments every second and stops after five iterations, as well as a function to calculate the sum of provided numbers. The code demonstrates basic operations such as filtering active users, handling promises, and using setInterval.

Uploaded by

larefer842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Miner New Easy

The document contains a JavaScript code snippet that manages user statuses, toggles their active state, and fetches data from an API. It also includes a counter that increments every second and stops after five iterations, as well as a function to calculate the sum of provided numbers. The code demonstrates basic operations such as filtering active users, handling promises, and using setInterval.

Uploaded by

larefer842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

let users = [

{ id: 1, name: 'John', active: true },


{ id: 2, name: 'Jane', active: false },
{ id: 3, name: 'Alice', active: true }
];

const isActiveUser = (user) => user.active;

function toggleActiveStatus(userId) {
const user = users.find(u => u.id === userId);
if (user) {
user.active = !user.active;
console.log(`${user.name}'s status updated to: ${user.active ? 'Active' :
'Inactive'}`);
} else {
console.log('User not found!');
}
}

const activeUsers = users.filter(isActiveUser);


console.log('Active Users:', activeUsers);

function fetchData(url) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (url === 'https://api.example.com') {
resolve({ data: 'Fetched data successfully!' });
} else {
reject('Error: Unable to fetch data');
}
}, 1500);
});
}

fetchData('https://api.example.com')
.then(response => console.log(response.data))
.catch(error => console.error(error));

let counter = 0;
const intervalId = setInterval(() => {
counter++;
console.log(`Counter: ${counter}`);
if (counter === 5) clearInterval(intervalId); // Stops after 5 iterations
}, 1000);

const calculateSum = (...numbers) => numbers.reduce((sum, num) => sum + num, 0);
console.log('Sum of numbe

You might also like