Wrap Errors with Context
JavaScript
Medium
3 views
Problem Description
Given a number x. If x is negative, throw an error 'NEG'. Catch it and print 'Bad input: NEG'. Otherwise print x*x.
Input Format
One integer x.
Output Format
One line output.
Constraints
x is integer.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const x=Number(s);try{if(x<0)throw new Error('NEG');process.stdout.write(String(BigInt(x)*BigInt(x)));}catch(e){process.stdout.write('Bad input: '+e.message);}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!