휴먼교육센터 개발자과정 66일차

조하영·2022년 11월 8일
0

자바스크립트 배열

  1. for(let i=0; i<a2.length; i++){
    console.log([i]+a2[i]);
    }
    0apple
    1banana
    2apple
    3grape
    4orange

for(let i=0;i<a3.length;i++){
if(max<a3[i]){
max = a3[i];
}
}
console.log(max);
74

한줄로 푸시오 . a3.sort().reverse()[0];

var string = "abcde";
위와 같은 문자열이 있다고 가정하고 출력해보면

console.log(string.length);
// 결과는 5
문자열의 길이

즉, 결과는 5가 출력됩니다.

배열의 경우도 마찬가지로

var arr_data = ['aaaa', 'bbbb', 'cccc', 'dddd'];
위와 같은 배열이 있다고 가정하고 출력해보면

console.log(arr_data.length);
// 결과는 4
이처럼 배열의 크기가 나온다는 사실!

javascript length 함수는 거의 기본적으로 쓰이니 꼭 알고 가세요!

배열의 합을 구할 때는
const num10 = [1,2,3,4,5,6,7,8,9,10];
1.let sumsum = 0;
num10.forEach( (item) => {
sumsum += item;
});
document.write('forEach item : ', sumsum);

  1. const sum = num10.reduce((a,b) => (a+b));
    document.write('reduce : ', sum);

var a2=['apple','banana','apple','grape','orange']; //배열선언
var findword='apple'; //찾을단어
var cnt=0; //카운트
for(let i=0; i<a2.length;i++){
if(a2[i]==findword){
cnt++;
console.log(findword+"가 위치한 인덱스: "+i);
}
}
console.log(findword+"는 "+cnt+"회 나왔습니다");

  1. a배열의 오름차순으로 정렬하세요
    a.sort()
    (7) [13, 23, 32, 35, 36, 45, 45]

2.a배열에서 최저점을 출력하시오.
a.sort()[0]
13

  1. a배열의 모든 점수를 출력하세요(for문)
    for(let i=0; i<a.length; i++){
    console.log(a[i]);
    }
    13
    23
    32
    35
    36
    2 45

  2. a배열의 모든 점수를 출력하세요.(forEach)
    a.forEach(function(e){
    console.log(e);
    });

  3. filter를 사용하여 a배열에서 40점 이하는 모두 삭제하시오.,
    let result = a.filter(values => values>=40);
    console.log(result);
    (2) [45, 45]
    6.배열a와 b를 합치시오
    var c= a.concat(b);
    console.log(c);
    [13, 23, 32, 35, 36, 45, 45, 45, 45, 56, 56, 57, 78]
    7.5번 결과에서 퇴고점을 몇점입니까
    c.sort().slice(-1);
    [78]

profile
공부하는 개발자

0개의 댓글