You can create a buffer that stores the numbers while you calculate the result:
let n = 5,
t = 1,
q = document.querySelector('.n'),
b = '';
for (let i = n; i > 0; i--) {
if (i !== 1) {
b += i + ' x ';
} else {
b += i;
}
t *= i;
}
q.innerHTML = (n + '! = ' + b + ' = ' + t);
<span class="n"></span>
Or you can use ES6 functions
const n = 5;
const array = Array.from({length: n}, (x,i) => i+1);
const process = array.reverse().join(' x ');
const factorial = array.reduce((x, y) => x*y, 1)
const result = `${n}! = ${process} = ${factorial}`;
document.getElementById('result').innerHTML = result;
<span id="result"></span>
Documentation:
Array.from()- Create an array from certain parameters
You can create a buffer that stores the numbers while you calculate the result:
Or you can use ES6 functions
Documentation:
Array.from()
- Create an array from certain parametersArray.reverse()
- Turn the arrangementArray.join()
- Bind the array to a stringArray.reduce()
- Execute a reducer function on each element of the arrayYou can concatenate the value and then use the substring to remove the last concatenated value which in this case would be X