Collect Promise Errors
JavaScript
Hard
4 views
Problem Description
Given n and n integers. For each integer x, create a promise that rejects if x is odd, resolves if x is even. Wait all and print count of rejects.
Input Format
Line1: n. Line2: n integers.
Output Format
One integer rejectCount.
Official Solution
const fs=require('fs');const a=fs.readFileSync(0,'utf8').trim().split(/\\s+/).map(Number);let i=0;const n=a[i++];const arr=a.slice(i,i+n);const tasks=arr.map(x=>new Promise((res,rej)=>{if(x%2===0)res(x);else rej(new Error('ODD'));}));(async()=>{const r=await Promise.allSettled(tasks);let c=0;for(const it of r){if(it.status==='rejected')c++;}process.stdout.write(String(c));})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!