'JavaScript - How do I print an input inside an array? [closed]
I already tried many ways to solve this, but it always returns undefined
. What should I do to solve this issue?
function printThreeFavoriteColours() {
const colours = [];
const colorUserFav1 = prompt("type your first favorite color");
const colorUserFav2 = prompt("now, type your second favorite color");
const colorUserFav3 = prompt("finally, type your third favorite color");
colours.push = (colorUserFav1, colorUserFav2, colorUserFav3);
console.log(colours);
}
Solution 1:[1]
You almost got it, you just need to remove =
from your example
function printThreeFavoriteColours() {
const colours = [];
const colorUserFav1 = prompt("type your first favorite color");
const colorUserFav2 = prompt("now, type your second favorite color");
const colorUserFav3 = prompt("finally, type your third favorite color");
colours.push(colorUserFav1, colorUserFav2, colorUserFav3);
console.log(colours);
}
Solution 2:[2]
colors.push( //put whatever you want to be pushed here)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Srikanth Kotnala |
Solution 2 | Tarek Salah |