Nullish Fallback
JavaScript
Easy
5 views
Problem Description
Two JSON values are given on two lines: a and b. Print the value of (a ?? b) as JSON (stringified). Remember: only null and undefined trigger the fallback, not 0 or empty string.
Input Format
Line1: JSON value a. Line2: JSON value b.
Output Format
One line JSON output.
Constraints
Inputs are valid JSON (so a cannot be undefined).
Official Solution
const fs=require('fs');const lines=fs.readFileSync(0,'utf8').split(/\
?\
/);const aLine=(lines[0]??'').trim();const bLine=lines.slice(1).join('\
').trim();if(!aLine||!bLine){process.exit(0);}const a=JSON.parse(aLine);const b=JSON.parse(bLine);const out=(a??b);process.stdout.write(JSON.stringify(out));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!