Try Parse JSON Else Number Else String
JavaScript
Medium
3 views
Problem Description
One line text is given. First try JSON.parse. If it fails, try Number() and if finite print NUMBER . Else print STRING .
Input Format
One line text.
Output Format
One line output.
Constraints
Input length up to 2e5.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8');const t=raw.replace(/\
?\
$/,'');if(!t){process.exit(0);}try{const v=JSON.parse(t);process.stdout.write('JSON '+JSON.stringify(v));}catch(e1){const n=Number(t.trim());if(Number.isFinite(n))process.stdout.write('NUMBER '+String(n));else process.stdout.write('STRING '+t);}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!