First and Last Digit
Programming Basics · JavaScript
TASK
Problem
Given positive integer 1..999999, print its first and last digits separated by space.
EXAMPLE
Example
40782
4 2
LIMITS
Constraints
`n` is a positive integer from `1` to `999999`, so it always has a well-defined first and last decimal digit.
LEARN
Theory for this problem
+
### First and Last Digit
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.', 'Do not mix the loop counter with the accumulated answer; those variables serve different roles.']
ANSWER
Solution
+
const fs = require('fs');
const n = Number(fs.readFileSync(0, 'utf8').trim());
const text = String(Math.abs(n));
console.log(Number(text[0]), Number(text.at(-1)));