英文:
How can I write Space between Characters with Javascript?
问题
我使用 Math.random();
生成了随机数。
但它们用逗号分隔,就像这样:
5,7,8,10
如何使用 JavaScript 在数字之间添加空格,就像这样:
5 7 8 10
这是我的代码:
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 = `我们的偶数是:${evenNumbersArray}`;
英文:
I generated random numbers using Math.random();
.
But they are separated by commas, like this:
5,7,8,10
How can I write a space between the numbers using JavaScript, like this:
5 7 8 10
Here's my code:
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}`;
答案1
得分: 1
这是更好的做法:
// 指定数组的长度
const length = 10;
// 指定最小和最大值
const min = 1;
const max = 100;
// 生成介于最小值和最大值之间的随机数(包括最小和最大值)
const randomNumber = () => Math.floor(Math.random() * (max - min + 1)) + min;
// 创建一个包含随机数的数组
const numbers = Array.from({ length }, randomNumber);
// 以空格分隔的形式打印数组内容
console.log(numbers.join(' '));
document.write(numbers.join(' '));
英文:
I think this is a better way to do this :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Specify the length of the array
const length = 10;
// Specify the minimum and maximum values
const min = 1;
const max = 100;
// Generate a random number between min and max (inclusive)
const randomNumber = () => Math.floor(Math.random() * (max - min + 1)) + min;
// Create an array filled with random numbers
const numbers = Array.from({ length }, randomNumber);
// Print the array contents separated by whitespace
console.log(numbers.join(' '));
document.write(numbers.join(' '));
<!-- end snippet -->
答案2
得分: -2
要删除 ',' 并将其替换为空格字符,您应该在字符串上使用 replaceAll
方法。
换句话说,您可以使用 .toString()
方法将数组转换为字符串,然后使用 .replaceAll('a', 'b')
来在整个字符串中替换 a 为 b。
有关 replace
和 replaceAll
方法的更多信息,请参考:https://www.w3schools.com/jsref/jsref_replace.asp
const 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),
];
const evenNumbersArray = [];
numbers.forEach(function (number) {
if (number % 2 === 0) {
evenNumbersArray.push(number);
}
});
const text = evenNumbersArray.toString().replaceAll(',', ' ');
// 然后将文本添加到 DOM:
document.getElementById("showEvenNumbers").innerText = `我们的偶数是: ${text}`;
<p id="showEvenNumbers"></p>
英文:
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
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const 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),
];
const evenNumbersArray = [];
numbers.forEach(function (number) {
if (number % 2 === 0) {
evenNumbersArray.push(number);
}
});
const text = evenNumbersArray.toString().replaceAll(',',' ');
// then add text to DOM:
document.getElementById("showEvenNumbers").innerText = `Our evenNumbers are : ${text}`;
<!-- language: lang-html -->
<p id="showEvenNumbers"></p>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论