I made a Random numbers with math.random();
but they have , between like :
5,7,8,10
How can I write Space between Characters with Javascript?
like this:
5 7 8 10
I’m sorry! My English is very poor.
All my code is here:
let numbers = [
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
Math.round(Math.random() * 100 - 1 + 1),
];
console.log(numbers);
let evenNumbersArray = [];
let evenNumbers = numbers.forEach(function (number) {
if (number % 2 === 0) {
console.log(number);
evenNumbersArray.push(number);
}
});
evenNumbersArray.toString();
document.getElementById(
"showEvenNumbers"
).innerText = `Our evenNumbers are : ${evenNumbersArray}`;
2
Answers
To remove ‘,’ and replace them with space character, you should use replaceAll method on the string.
In another words, You convert your array to string with
.toString()
method, then use.replaceAll('a', 'b')
to replace a with b in whole string.More info about replace and replaceAll method: https://www.w3schools.com/jsref/jsref_replace.asp
I think this is a better way to do this :