Retry Until Non-Zero
JavaScript
Medium
4 views
Problem Description
You get attempts and a list of values returned by a flaky API (length attempts). The first non-zero value means success. Print the value, or FAIL.
Input Format
Line1: attempts. Line2: attempts integers.
Output Format
Value or FAIL.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/);let i=0;const attempts=Number(a[i++]);let vals=[];for(let j=0;j<attempts;j++)vals.push(BigInt(a[i++]));let idx=0;const api=async()=>{await Promise.resolve();return vals[idx++];};(async()=>{let ans=null;for(let t=0;t<attempts;t++){const v=await api();if(v!==0n){ans=v;break;}}process.stdout.write(ans===null?'FAIL':ans.toString());})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!