개인공부/Javascript 7

[Javascript] 퀴즈2

숫자 배열이 주어졌을 때 10보다 큰 숫자의 갯수를 반환하는 함수를 만드세요. function countBiggerThanTen (numbers) { } const count = countBiggerThanTen([1, 2, 3, 5, 10, 20, 30 ,40 ,50, 60]); console.log(count); // 5 정답 let count = 0; numbers.forEach(n => { if (n > 10) { count++; } }); return count; return numbers.filter(n => n > 10).length; return numbers.reduce((acc, current) => { if (current > 10) { return acc + 1; } else { r..

[Javascript] 배열 내장함수

forEach 각 원소들에 대하여 반복적으로 호출 const numbers = ['a', 'b', 'c', 'd', 'e']; numbers.forEach(number => { console.log(number); }); a b c d e map 모든 원소를 다른 형태로 변환해야 할 때 const array = [1, 2, 3, 4, 5, 6, 7,8]; const square = n => n * n; const squared = array.map(square); console.log(squared); (8) [1, 4, 9, 16, 25, 36, 49, 64] indexOf 특정 값이 어디에 있는지 알아낼 때 const alphabets = ['a', 'b', 'c', 'd'] const index =..

[Javascript] 퀴즈

퀴즈 아래의 숫자로 이루어진 배열에서 3보다 큰 숫자들로 이루어진 새로운 배열을 만들어 반환해라 function biggerThanThree(numbers) { } const numbers = [1, 2, 3, 4, 5, 6, 7]; console.log(biggerThanThree(numbers)); *힌트: 빈배열, push 정답 function biggerThanThree(numbers) { let array = []; for (let i = 0; i 3 ) { array.push(numbers[i]); } } return array; } const numbers = [1, 2, 3, 4, 5, 6, 7]; console.lo..

[Javascript] 콜백함수

콜백함수는 함수의 매개변수로 함수를 넘겨준 것으로 나중에 호출되는 함수 아래의 예제로 function callbackFunc(morning){ console.log(morning); } undefined callbackFunc 함수에 morning을 매개변수로 넣어주고 function hi(callback){ var text = "good morning"; callback(text); } undefined hi 함수에 callback을 매개변수로 넣어준 또다른 함수를 만든다. hi(callbackFunc); good morning hi 함수에 callbackFunc를 인자로 넣어주고 호출을 한다. 그러면 hi 함수가 실행되고 callback(text) -> callbackFunc(text)가 되면서 ca..

자바스크립트 기초

변수 값을 저장, 값의 재사용을 위해 사용 let hello = "하이"; "하이"를 hello에 할당 변수의 이름은 값의 형식에 따라 작성 = > 시맨틱 주석 // /* */ 정수, 실수 자바스크립트는 정수와 실수를 구분하지 않는다. 1과 1.0 모두 1로 간주한다. 1.2 + 1 = 1.2 실수로 계산한다. 상수 변경할 수 없는 값 시맨틱선언으로 대문자를 사용 let TEN = 10; 데이터 타입 Number, String, Undefined, Null, Boolean, Object 연산자 할당 연산자 단일 연산자 = 복합 연산자 +=, -=, *=, /= compile=> 자바스크립트 엔진이 해석(자바스크립트 언어로) 산술 연산자 +, -, *, /, % 숫자 + 문자 = 문자 1 + "A" = 1..