Percentage of a Number
Programming Basics · JavaScript
TASK
Problem
Given `value` and percentage `p`, print `p` percent of `value`.
EXAMPLE
Example
240 25
60.0
LIMITS
Constraints
All numeric input values fit in JavaScript `Number`.
LEARN
Theory for this problem
+
### Percentage of a Number
JavaScript arithmetic works with numeric values stored in variables. A clear solution reads the needed values, computes the formula with `+`, `-`, `*`, `/` or `**`, and prints only the final result.
NEED HELP?
Hints
+
['Write the formula using named intermediate values if it contains more than one operation.', 'Match each input value type to the operation performed on it: JavaScript string and numeric behavior differ significantly.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [value, p] = input.trim().split(/\s+/).filter(Boolean).map(Number);
let result = (value * p / 100);
console.log(Number.isInteger(result) ? result.toFixed(1) : result);