'use strict'

// Array

// 1. Declaration
const arr1 = new Array();
const arr2 = [1, 2];

// 2. Index position
const fruits = ['🍏', '🍌', '🌭'];
const malza = new Array('(❁´◑`❁)', '(●\'β—‘\'●)', 'β•°(*Β°β–½Β°*)β•―');

console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);

console.clear();
// 3. Looping over an array

for(let i = 0.; i < fruits.length; i++){
    console.log(fruits[i]);
}

for(let fruit of fruits) {
    console.log(fruit);
}
console.clear();
// fruits.forEach(function (fruit, index, array) {
//     console.log(fruit, index, array); //보톡 arrayκΉŒμ§€λŠ” λ°›μ•„μ˜€μ§€ μ•ŠλŠ”λ‹€.
// });
🍏 0 (3) ["🍏", "🍌", "🌭"]
🍌 1 (3) ["🍏", "🍌", "🌭"]
🌭 2 (3)Β ["🍏", "🍌", "🌭"] //μœ„μ˜ 식은 μ΄λ ‡κ²Œ 좜λ ₯λœλ‹€.
// fruits.forEach((fruit, index, array) => {
//     console.log(fruit, index, array); 
// });

fruits.forEach((fruit) => console.log(fruit));

// 4. Addition, deletion, copy
// push: add an item to the end
fruits.push('πŸ₯¨');

console.log(fruits);
// pop: remove an item from the end
fruits.pop();
console.log(fruits);

//unshift: add an item to the beginning
fruits.unshift('πŸ”'); // 맨 μ•žμ—μ„œ λ„£λŠ”λ‹€.
console.log(fruits);

//shift: remove an item from the beginning
fruits.shift(); // 맨 μ•žμ—μ„œ λΊ€λ‹€.
console.log(fruits);

// note!! shift, unshift are slower than pop, push
// μ•žμ—μ„œ λΉΌκ±°λ‚˜ λ„£μœΌλ©΄ 그만큼 λ’€μ—μ„œ λ•‘κΈ°κ±°λ‚˜ λ°€μ–΄μ•Ό ν•˜λ‹ˆκΉŒ
// splice: remove an item by index position
fruits.push('πŸ•', '🍟'); 
console.log(fruits); // (5)Β ["🍏", "🍌", "🌭", "πŸ•", "🍟"]
fruits.splice(1, 2); 
console.log(fruits); // 1λ²ˆλΆ€ν„° 2개 μ§€μš°λ‹ˆκΉŒ (3)Β ["🍏", "πŸ•", "🍟"]
//fruits.splice(1); // λͺ‡κ°œ μ§€μšΈμ§€ μ•ˆ λ„˜κ²¨μ£Όλ©΄ 1λ²ˆλΆ€ν„° λ‹€ μ§€μ›Œλ²„λ¦°λ‹€.
fruits.splice(1, 1, 'πŸ–', 'πŸ—'); // 1번 λΆ€ν„° 1개 μ§€μš°κ³  κ·Έ μžλ¦¬μ— 뒀에것듀 λ„£μ–΄μ€˜
console.log(fruits); // (4)Β ["🍏", "πŸ–", "πŸ—", "🍟"]
//splice λŠ” μ‚­μ œλœ elem 을 return ν•΄μ€€λ‹€.

// combine two arrays
const fruits2 = ['πŸ₯—', 'πŸ₯™'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits); // 합쳐진 것이 생겼닀. // (6)Β ["🍏", "πŸ–", "πŸ—", "🍟", "πŸ₯—", "πŸ₯™"]

// 5. Searching
// find the index
console.clear();
console.log(fruits);
console.log(fruits.indexOf('πŸ–')); // 1 return ν•΄μ€€λ‹€.
console.log(fruits.includes('🍳')); // 쑴재 ν•˜λŠ”μ§€ μ—¬λΆ€. false return ν•΄μ€€λ‹€.
console.log(fruits.includes('πŸ–')); 
console.log(fruits.indexOf('🍳'));  // μ—†μœΌλ‹ˆκΉŒ -1 retrun ν•΄μ€€λ‹€.

// lastIndexof
console.clear();
newFruits.push('🍏');
console.log(newFruits.lastIndexOf('🍏'));  //λ§ˆμ§€λ§‰μ— λ“€μ–΄κ°”μœΌλ‹ˆκΉŒ λ§ˆμ§€λ§‰ idxλ₯Ό return 받을 것이닀.

console.log(malza);