所以我有这个代码,我有一个句子数组,通过一个开关提示用户要询问他们想要显示多少句子1 - 5.然后它将循环并随机显示的数量从阵列中请求的句子。
var sentences = ["sentence one", "sentence two", "sentence three",
"sentence 4", "sentence 5"];
function shuffle(a) {
for (i = a.length - 1; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
var random = prompt("how many sentences do you want?");
switch (random) {
case "1":
shuffle(sentences)
console.log(sentences(0, 1));
break;
case "2":
shuffle(sentences)
console.log(sentences.slice(0, 2))
break;
case "3":
shuffle(sentences)
console.log(sentences.slice(0, 3))
break;
case "4":
shuffle(sentences)
console.log(sentences.slice(0, 4))
break;
case "5":
shuffle(sentences)
console.log(sentences.slice(0, 5))
break;
default:
console.log("incorrect number");
}

代码工作正常,但是当它返回时,它会显示当然的数组: 如果这个人想要3个句子:
(3) ["sentence two", "sentence four", "sentence five"]
我想返回句子本身而不是数组:
sentence two, sentence four, sentence five
我尝试使用.length
转换为字符串,但这会返回字符串中的字符数,例如输入的提示为5,因此返回:
sente
任何人都有任何想法,非常感谢:)
答案 0 :(得分:0)
在显示之前使用.slice去除括号。
https://www.w3schools.com/jsref/jsref_slice_array.asp
https://www.w3schools.com/jsref/jsref_slice_string.asp
或者你可以使用split:
https://www.w3schools.com/jsref/jsref_split.asp
或者您可以使用替换:
答案 1 :(得分:0)
你可以通过"加入"你的数组有join method。例如,给定["foo", "bar"]
数组,调用["foo", "bar"].join(", ")
将输出包含foo, bar
的字符串。
以下是一个例子:
> var foo = ['bar', 'bat'];
[ 'bar', 'bat' ]
> foo.join(', ');
'bar, bat'
答案 2 :(得分:0)
要实现您的目标,您可以在ES6中使用传播...
运算符。
var sentences = ["sentence one", "sentence two", "sentence three",
"sentence 4", "sentence 5"];
function shuffle(a) {
for (i = a.length - 1; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
var random = prompt("how many sentences do you want?");
switch (random) {
case "1":
shuffle(sentences)
console.log(...sentences(0, 1));
break;
case "2":
shuffle(sentences)
console.log(...sentences.slice(0, 2))
break;
case "3":
shuffle(sentences)
console.log(...sentences.slice(0, 3))
break;
case "4":
shuffle(sentences)
console.log(...sentences.slice(0, 4))
break;
case "5":
shuffle(sentences)
console.log(...sentences.slice(0, 5))
break;
default:
console.log("incorrect number");
}
作为旁注,switch语句是重复且不必要的:
var random = +prompt("how many sentences do you want?");
if (0 <= random <= 5) {
shuffle(sentences);
console.log(...sentences.slice(0, random));
} else {
console.log("invalid number");
}
这里,+
将数字字符串转换为数字。您还可以使用Number(prompt(..))
显式转换。
编辑:我看到现在你想在元素之间插入逗号。
您可以使用Array.prototype.join
。
var random = +prompt("how many sentences do you want?");
if (0 <= random <= 5) {
shuffle(sentences);
console.log(sentences.slice(0, random).join(', ');
} else {
console.log("invalid number");
}
答案 3 :(得分:0)
使用join()
将数组中的所有值与字符串连接起来(如', '
)
var sentences = ["sentence one", "sentence two", "sentence three",
"sentence 4", "sentence 5"];
function shuffle(a) {
for (i = a.length - 1; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
var random = prompt("how many sentences do you want?");
shuffle(sentences);
switch (random) {
case "1":
sentences = sentences.slice(0, 1);
break;
case "2":
sentences = sentences.slice(0, 2);
break;
case "3":
sentences = sentences.slice(0, 3);
break;
case "4":
sentences = sentences.slice(0, 4)
break;
case "5":
sentences = sentences.slice(0, 5);
break;
default:
console.log("incorrect number");
}
console.log(sentences.join(', '));
&#13;
答案 4 :(得分:0)
您可以使用join ()。 join()方法将数组的所有元素(或类数组对象)连接成一个字符串并返回该字符串。
在您的代码中:
console.log((sentences.slice(0, 3)).join(', '));