Promise Chain Without Nesting
JavaScript
Medium
2 views
Problem Description
Input has n numbers. Apply steps on each number: +1 then *2 then -3. Use async/await and print final numbers.
Input Format
Line1: n. Line2: n integers.
Output Format
One line outputs.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/).map(Number);let i=0;const n=a[i++];let arr=a.slice(i,i+n);const add1=async x=>{await Promise.resolve();return x+1;};const mul2=async x=>{await Promise.resolve();return x*2;};const sub3=async x=>{await Promise.resolve();return x-3;};(async()=>{let out=[];for(const x of arr){let v=await add1(x);v=await mul2(v);v=await sub3(v);out.push(String(v));}process.stdout.write(out.join(' '));})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!