function processArray(arr, callback) {
let result = [];
for (let i = 0; i < arr.length; i++) {
result.push(callback(arr[i]));
}
return result;
}
console.log(processArray([1, 2, 3], num => num * 10)); // [10, 20, 30]
3. Promises
Problem 3.1 - Simple Promise Handling
function loadData() {
return new Promise(resolve => {
setTimeout(() => resolve("Data Loaded"), 2000);
});
}
loadData().then(console.log);
Problem 3.2 - Simulate an API Call
function fakeFetch(url) {
return new Promise(resolve => {
setTimeout(() => resolve(`Data fetched from ${url}`), 3000);
});
}
fakeFetch("https://api.example.com").then(console.log);