// 배열 도서관
// 책1
// title
// ISBN
// content
// type
// rating: 점수
// 책이 하나만 있지는 않다.
// 여러가지 책의 정보를 컴퓨터가 관리하려면 어떻게 해야 하는가
// [책1, 책2, 책3, 책4, 책5]
// 비슷한 설징을 가진 여러 데이터를 관리하기에 용이하다.
const word = 'hello world';
word.length; // 속성
word.slice(0, 5); // 메서드
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// 참조 자료형
// 속성과 메서드
// console.log('배열의 길이', arr.length);
console.log('배열의 타입', typeof arr);
console.log('객체, object중 배열인지 아닌지 확인하는 방법', Array.isArray(arr));
// console.log('빈 배열인지 아닌지 확인하는 방법', arr.length === 0);
let emptyArr = [];
console.log('emptyArr === []', emptyArr === []);
let i = 2
console.log('배열의 i번째 요소 조회하는법', arr[i])
// 자바스크립트에서 i번째 ... => zero-indexing, 0번째 부터 시작한다.
const words = ['hello', 'world', 'codestates'];
console.log('world', words[1]);
// 순서가 있는 자료(데이터)
const newArr = ['안녕하세요', '코드스테이츠']
// '안녕하세요' => newArr의 0번째 요소(element)
// '안녕하세요' => 0 => newArr의 '안녕하세요' 요소는 index는 0이다.
// 메서드
// mutator method (mutable): pop(), push(), shift(), unshift(), splice()
// 원본 배열을 변경합니다.
// method (immutable): slice(), concat()
// 원본 배열을 변경하지 않습니다.
let newNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// newNumArr.pop();
// [0, 1, 2, 3, 4, 5, 6, 7, 8];
console.log('newNumArr.pop() 리턴값', newNumArr.pop())
console.log('arr.pop()', newNumArr);
console.log('newNumArr.push() 리턴값', newNumArr.push(100))
console.log('newNumArr.push() 이후 newNumArr 값', newNumArr)
console.log('newNumArr.shift() 리턴값', newNumArr.shift())
console.log('arr.shift()', newNumArr);
console.log('newNumArr.unshift() 리턴값', newNumArr.unshift(100))
console.log('newNumArr.unshift() 이후 newNumArr 값', newNumArr)
// slice() // 얕은 복사, 깊은 복사
let secondNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let slice1 = secondNumArr.slice(0, 4);
let wholeSlice = secondNumArr.slice();
console.log(secondNumArr);
console.log(secondNumArr.length);
console.log(slice1);
console.log(slice1.length);
console.log('wholeSlice secondNumArr.push(45); 전', wholeSlice)
console.log('secondNumArr secondNumArr.push(45); 전', secondNumArr)
secondNumArr.push(45);
console.log('wholeSlice secondNumArr.push(45); 후', wholeSlice)
console.log('secondNumArr secondNumArr.push(45); 후', secondNumArr)
let thirdNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(thirdNumArr)
console.log(thirdNumArr.reverse());
console.log(thirdNumArr)
let fourthNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(fourthNumArr);
console.log(fourthNumArr.concat(1, 2, 3, 4)); // concatenation: 이어붙이기
console.log(fourthNumArr);
let fifthNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(fifthNumArr.indexOf(4));
console.log(fifthNumArr.indexOf(100));
console.log(fifthNumArr.includes(4));
console.log(fifthNumArr.includes(100));
let sixthNumArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function arrToString(arr) {
let result = 0;
for (let i = 0; i < arr.length; i++) {
if(i % 2 === 0) {
result = result + arr[i];
}
}
return result;
}
console.log(arrToString(sixthNumArr))