When performing this task, the spread operator is not really necessary.
The question is slightly unclear about whether you specifically want the symbol ?
in the string or if you want to include the values from the array. Here are solutions for both scenarios:
If you require the symbol ?
(literally), you can achieve it using map
along with join
:
`the values are (${theArray.map(() => "?").join(", ")})`
var theArray = [1, 2, 3];
console.log(`the values are (${theArray.map(() => "?").join(", ")}`);
Alternatively, you could use String#repeat
together with slice
to remove the last delimiter:
`the values are (${"?, ".repeat(theArray.length).slice(0, -2)})`
var theArray = [1, 2, 3];
console.log(`the values are (${"?, ".repeat(theArray.length).slice(0, -2)})`);
If you want to display the actual values, you can simply use the join
method:
`the values are (${theArray.join(", ")})`
var theArray = [1, 2, 3];
console.log(`the values are (${theArray.join(", ")})`);