admin管理员组文章数量:1332889
So I have this code where I have an array of sentences which are being prompted through a switch where the user is to ask how many sentences they want to display 1 - 5. it will then loop through and randomly display the number of sentences requested from the array.
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");
}
So I have this code where I have an array of sentences which are being prompted through a switch where the user is to ask how many sentences they want to display 1 - 5. it will then loop through and randomly display the number of sentences requested from the array.
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");
}
The code works fine, but when it returns, it displays the array of course: say if the person wanted 3 sentences:
(3) ["sentence two", "sentence four", "sentence five"]
I want to return the sentences itself and not the array so:
sentence two, sentence four, sentence five
I tried converting to a string with .length
but this then returns the number of characters in the string eg prompt entered was 5 so it returns:
sente
Anyone have any ideas, much appreciated :)
Share Improve this question edited Mar 27, 2018 at 0:44 Barmar 783k56 gold badges547 silver badges660 bronze badges asked Mar 27, 2018 at 0:35 Χρήστος ΙερείδηςΧρήστος Ιερείδης 231 gold badge1 silver badge3 bronze badges 5- 1 It's important to note first of all that your code isn't actually returning anything; it's just logging a representation of your array to the console – Hamms Commented Mar 27, 2018 at 0:40
-
1
If you want to log a prettier representation of your array, one good option is
array.join
– Hamms Commented Mar 27, 2018 at 0:40 -
.slice()
always returns an Array. If you're outputting to HTML you can just assign the Array to theElement.innerHTML
orElement.value
and it gives you a String without[]
anyways. – StackSlave Commented Mar 27, 2018 at 0:43 -
Why are you using
switch/case
? Justsentences.slice(0, random)
– Barmar Commented Mar 27, 2018 at 0:48 - "return array without the brackets". If it doesn't have the brackets, it's not an array. – Barmar Commented Mar 27, 2018 at 0:49
5 Answers
Reset to default 3You can do this by "joining" your array with the join method. For example, given an array of ["foo", "bar"]
, calling ["foo", "bar"].join(", ")
will output a string containing foo, bar
.
Here's an example:
> var foo = ['bar', 'bat'];
[ 'bar', 'bat' ]
> foo.join(', ');
'bar, bat'
You can use join (). The join() method joins all elements of an array (or an array-like object) into a string and returns this string.
In your code:
console.log((sentences.slice(0, 3)).join(', '));
Use .slice to strip the brackets before you display it.
https://www.w3schools./jsref/jsref_slice_array.asp
https://www.w3schools./jsref/jsref_slice_string.asp
Or alternatively you can use split:
https://www.w3schools./jsref/jsref_split.asp
OR you can use replace:
https://www.w3schools./jsref/jsref_replace.asp
Use join()
to concatenate all the values from the array with a string (like ', '
)
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(', '));
To achieve what you want, you can use the spread ...
operator in 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");
}
As a side note, the switch statement is repetitive and unnecessary:
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");
}
Here, +
casts the number string to a number. You can also explicitly cast using Number(prompt(..))
.
EDIT: I see that now you want to insert ma between elements.
You can use 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");
}
本文标签: How to return the array without the brackets javascriptStack Overflow
版权声明:本文标题:How to return the array without the brackets javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742311079a2450884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论