如何使用JavaScript在字符之间添加空格?

huangapple go评论96阅读模式
英文:

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 = () =&gt; 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(&#39; &#39;));
document.write(numbers.join(&#39; &#39;));

<!-- end snippet -->

答案2

得分: -2

要删除 ',' 并将其替换为空格字符,您应该在字符串上使用 replaceAll 方法。
换句话说,您可以使用 .toString() 方法将数组转换为字符串,然后使用 .replaceAll('a', 'b') 来在整个字符串中替换 a 为 b。

有关 replacereplaceAll 方法的更多信息,请参考: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(&#39;a&#39;, &#39;b&#39;) 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(&#39;,&#39;,&#39; &#39;);

// then add text to DOM:
document.getElementById(&quot;showEvenNumbers&quot;).innerText = `Our evenNumbers are :  ${text}`;

<!-- language: lang-html -->

&lt;p id=&quot;showEvenNumbers&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 23:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定