π±βπ»λ°°μ΄μ μ μ λ° νΉμ§
- λ°°μ΄μ μ¬μ©νλ©΄ λͺ μ² κ°μ λ³μλ₯Ό μμ±νμ§ μκ³ , νλμ λ³μμ λͺ¨λ λ°μ΄ν°λ₯Ό κ°κ³ μμ μ μμ΅λλ€.
- λ°°μ΄μ λκ΄νΈ(
[]
)λ‘ κ°μΈμ Έ μμ΅λλ€.
λ°μ΄ν°κ° μλ λ°°μ΄μ λ§λ€μ΄λ³΄κ² μ΅λλ€.let cities = ["μμΈ", "λμ ", "λꡬ", "λΆμ°", "κ΄μ£Ό", "μ μ£Όλ"]; let KOSPI = [2062.82, 2053.2, 2045.92, 2058.82, 2053.12, 2055.7];
Element
: "μμΈ", "λμ " κ³Ό κ°μ λ°°μ΄μ κ°, νλ νλλ₯ΌElement(μμ)
λΌκ³ λΆλ¦ λλ€.
- μμμ μμ μ¬μ΄μλ μΌνλ‘ κ΅¬λΆν©λλ€.
- μμ μ리μλ μ΄μ κΉμ§ λ°°μ΄
String
,Number
,Array
λͺ¨λ κ°λ₯ν©λλ€.μμ)
let anything = ["λμ ", 1987, ["νλ", "λ", 3]];
π±βπ» λ°°μ΄μ Index
- λ°°μ΄μ μμλ μμ(
index
)λ₯Ό κ°κ³ μμ΅λλ€.- μ£Όμν΄μΌ ν νΉμ§μ΄ μλ€λ©΄ μ΄ πβπ¨indexλ 1μ΄ μλλΌ 0λΆν° μμν©λλ€.
var anything = ["λμ ", 1987, ["νλ", "λ", 3]];
String
νμΈ "λμ "μ indexκ° 0Number
νμΈ 1987μ indexκ° 1Array
νμΈ ["νλ", "λ", 3]μ indexκ° 2μ λλ€.π±βπ» λ°°μ΄ λ°μ΄ν°μ μ κ·Ό
- indexλ₯Ό μ¬μ©νλ©΄ ν΄λΉ λ°°μ΄μ μμλ₯Ό κ°μ Έμ¬ μλ μμ΅λλ€.
μμ)
λ°°μ΄μ΄λ¦[index]
var anything = ["λμ ", 1987, ["νλ", "λ", 3]];
- μλ₯Όλ€μ΄,
anything[0]
μ "λμ "μ λλ€.anything[1]
μ 1987μ λλ€.console.log
λ‘ μμλ₯Ό μΆλ ₯ν΄λ΄ μλ€.console.log(anything[0]); //κ²°κ³Όλ "λμ " console.log(anything[1]); //κ²°κ³Όλ 1987 console.log(anything[2]); //κ²°κ³Όλ ["νλ", "λ", 3]
- μ΄λ° ννλ κ°λ₯νκ² λ€μ.
alert(anything[0] + "μ μ€μ κ²μ νμν©λλ€."); //κ²°κ³Όλ λμ μ μ€μ κ²μ νμν©λλ€.
π±βπ» Assignment
getElement
ν¨μμμ arr λ³μλ₯Ό μ μΈνμ΅λλ€.- arr λ³μλ λ°°μ΄μ ν λΉνκ³ μ, λ°°μ΄μλ λ€μν λ°μ΄ν° νμ μ μμκ° λ€μ΄μλ€μ!
- λ°°μ΄μ΄ λ΄κΈ΄ arr λ³μμ μ κ·Όνμ¬
getElement
ν¨μκ°"array"
λΌλ λ¬Έμμ΄μ return ν μ μλλ‘ ν΄μ£ΌμΈμ.function getElement() { let arr = [3, [4, ["array", 9], 2+3], [0]]; return [1],[1],[0]; }
νμ΄λ₯Ό ν΄λ³΄λ©΄,
return [1] = [4, ["array", 9], 2+3] μ΄ λ°°μ΄μ΄ λ°νλ κ²μ΄κ³ ,
return [1] = ["array", 9] μμ λ°°μ΄μμ 첫λ²μ§Έ μμμ μλ ["array", 9] κ° λ°νλ κ²μ΄κ³
return [0] = "array" κ·ΈλΌ 0λ²μ§ΈμΈ "array"λ₯Ό λ°νμν€λ©΄ λλ κ²μ΄λ€.
πβπ¨indexλ₯Ό μ¬μ©νλ©΄ ν΄λΉ λ°°μ΄μ μμλ₯Ό κ°μ Έμ¬ μλ μμ΅λλ€. λ₯Ό μ μκ°ν΄λ³΄λ©΄, ν΄λΉ μΈλ±μ€λ₯Ό ν΅νμ¬ μνλ μμ κ°μ ꡬν μ μκ² λλ κ²μ΄λ€.
addFirstAndLast
ν¨μ μμ μμ±ν΄μ£ΌμΈμ.addFirstAndLast
ν¨μμ μ£Όμ΄μ§ μΈμmyArray
λ μ«μ κ°μΌλ‘λ§ μ΄λ£¨μ΄μ§ array μ λλ€.addFirstAndLast
ν¨μμ μ£Όμ΄μ§ μΈμmyArray
μ 첫λ²μ§Έ elementμ λ§μ§λ§ elementμ κ°μ λν κ°μ 리ν΄ν΄μ£ΌμΈμ.- λ§μΌ
myArray
μ ν κ°μ μμλ§ μλ€λ©΄ ν΄λΉ μμμ κ°μ 리ν΄ν΄ μ£Όμκ³ μμκ° μλ λΉμ΄μλ arrayλΌλ©΄ 0μ 리ν΄ν΄μ£ΌμΈμ.- Hint) arrayμ κΈΈμ΄λ₯Ό ꡬνλ λ°©λ²μ λ€μμ μ°Έμ‘°νμΈμ
:arrayμ κΈΈμ΄λ₯Ό ꡬνλ λ°©λ²μ²μ λ΄ λ΅λ³:
function addFirstAndLast(myArray) { if (myArray[myArray.length] === 0){ return 0; } else if (myArray[myArray.length] === 1) { return myArray[0]; } else { return myArray[0] + myArray[myArray.length]; } }
κ²°κ³Όλ μ€ν¨. 쑰건문μ μμμΌλ‘λ ν΄λ³΄κ³ , 맀κ°λ³μ
myArray
λ₯Ό μμ°κ³myArray.length === 0;
μ΄λ°μμΌλ‘λ μ¨λ³΄κ³ , κ²°κ³Όκ° λμ¨λ€ μΆμΌλ©΄ κ³μ νμλ¦¬κ° λ μΆκ°λ κ²°κ³Όκ° λμλ€.
μλ₯Ό λ€μ΄addFirstAndLast([1,2]);
μ΄λ κ² νΈμΆνλ©΄
κ²°κ³Όλ 3μΌλ‘,[1,2,1]
addFirstAndLast([1,2,3,4,5]);
λΌκ³ νΈμΆνλ©΄ κ²°κ³Όλ 6μΌλ‘[1,2,3,4,5,1]
.. λ§μ μ λ§μμμ§ λͺ°λΌλ μλ¦Ώμμμ νμλ¦¬κ° νλ¬μ€ λλ κ²°κ³Όλ‘, κ³μν΄μ μ€λ₯κ° λ¬λ€.ꡬκΈλ§ν κ²°κ³Όμ λ΅λ³μ
function addFirstAndLast(myArray) { if(myArray.length === 0) { return 0; } else if(myArray.length === 1) { return myArray[0]; //κΈΈμ΄κ° 1μ΄λΌλ©΄ 맨 첫λ²μ§Έ indexμΈ 0μ 리ν΄νλ©΄ λλ κ². } else { return myArray[0] + myArray[myArray.length-1]; } } addFirstAndLast([1]); //κ²°κ³Όλ 1 addFirstAndLast([0]); //κ²°κ³Όλ 0 addFirstAndLast([1,2,3,4,5,10]); //κ²°κ³Όλ 11
myArray.length === 0
μ΄λΌκ³ μ°λ©΄ λμκ³ ..
무μ보λ€myArray[myArray.length-1]
μ΄κ²μ΄ ν΅μ¬μ΄μλ€.
μ μ λ§ μκ°λ³΄λ€ κ°λ¨ν κ±°μλ€.
λ°°μ΄μ 0λΆν° μμνκΈ° λλ¬Έμ΄λ€.
πβπ¨.length
λ λ°°μ΄μ κΈΈμ΄λ₯Ό λ°ννλ μμ±μ΄λ©°, 1λΆν° μΈκΈ° μμνλ€.
κ·Έλ κΈ° λλ¬Έμ λ°°μ΄μ λ§μ§λ§ μμμ κ°μ λ°°μ΄μ κΈΈμ΄μμ -1μ ν΄μΌ μ°Ύμ μ μλ κ²μ΄μλ€..
MDN : Array.length
μ½λ©ν©ν 리: .length