First Digit of a Three-Digit Number
Programming Basics · JavaScript
TASK
Problem
Given a positive three-digit integer, print its first digit.
EXAMPLE
Example
583
5
LIMITS
Constraints
100 ≤ n ≤ 999
LEARN
Theory for this problem
+
### First Digit of a Three-Digit Number
Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.
NEED HELP?
Hints
+
['Decide which part comes from division and which part comes from the remainder.', 'Separate input parsing, result computation, and output into distinct steps so errors are easier to spot.']
ANSWER
Solution
+
const fs = require('fs');
const text = fs.readFileSync(0, 'utf8').trim();
const firstDigit = Number(text[0]);
console.log(firstDigit);