Challenge: Access Array Elements
Task
Create an array called
stars
with the following star names:"Sirius";
"Alpha Centauri";
"Betelgeuse";
"Polaris".
Log individual star names using different indices (0, 1, 2, and 3) using
console.log
9
1
2
3
4
5
6
const stars = ___;
console.log("First element:", ___); // Output: Sirius
console.log("Second element:", ___); // Output: Alpha Centauri
console.log("Third element:", ___); // Output: Betelgeuse
console.log("Forth element:", ___); // Output: Polaris
123456const stars = ___; console.log("First element:", ___); // Output: Sirius console.log("Second element:", ___); // Output: Alpha Centauri console.log("Third element:", ___); // Output: Betelgeuse console.log("Forth element:", ___); // Output: Polaris
Expected output:
python91234First element: SiriusSecond element: Alpha CentauriThird element: BetelgeuseForth element: Polaris
Use square brackets
[]
to create an array.Separate each element with the comma
,
.Use
console.log()
to log the value of the array element at the specified index.
9
1
2
3
4
5
6
const stars = ["Sirius", "Alpha Centauri", "Betelgeuse", "Polaris"];
console.log("First element:", stars[0]); // Output: Sirius
console.log("Second element:", stars[1]); // Output: Alpha Centauri
console.log("Third element:", stars[2]); // Output: Betelgeuse
console.log("Forth element:", stars[3]); // Output: Polaris
123456const stars = ["Sirius", "Alpha Centauri", "Betelgeuse", "Polaris"]; console.log("First element:", stars[0]); // Output: Sirius console.log("Second element:", stars[1]); // Output: Alpha Centauri console.log("Third element:", stars[2]); // Output: Betelgeuse console.log("Forth element:", stars[3]); // Output: Polaris
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 4. Luku 2
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme