1.
for(let i =0; i < 100;)
console.log((++i % 3 ? "": "fizz") + (i % 5 ? "" : "buzz") || i);
output:
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
......98
2. Pick the correct option – how does the console.log differ between these two functions?
const axios = require('axios');
async function getDataForEach(ids) {
listForData = [];
ids.forEach(async (id) => {
const result = await axios.get("https://reqbin.com/echo/get/json");
listForData.push(result.data);
});
listForData.forEach((item) => console.log(item));
}
async function getDataFor(ids) {
listForData = [];
for(i =0; i< ids.length; i++){
const result = await axios.get("https://reqbin.com/echo/get/json")
listForData.push(result.data);
}
listForData.forEach((item) => console.log(item))
}
getDataFor([1,2,3]).then(console.log('done'));
getDataForEach([1,2,3]).then(console.log("done"));
output:
done
done
crash
forEach expects a synchronous function and won't do anything with the return value. It just calls the function and goes on. For loop will wait on the result of the execution of the function.
“Done” is logged first while the request is pending and then the list is
executed. As the request has changed and now returns the numbers 1,2,3
will be printed after “Done”.