본문 바로가기
코플릿 기록/JavaScript

배열 1번

by 뭉지야 2023. 1. 23.
728x90

#문제

임의의 값을 입력받아 타입을 리턴해야 합니다.

자바스크립트에서 array, null 타입은 존재하지 않지만, 이 둘을 구분하여 출력합니다.


#예시

let output = getType('hello');
console.log(output); // --> 'string'

output = getType(10);
console.log(output); // --> 'number'

output = getType(true);
console.log(output); // --> 'boolean'

output = getType({ name: 'Steve' });
console.log(output); // --> 'object'

output = getType([100, 200, 300]);
console.log(output); // --> 'array'

#정답

function getType(anything) {
if (Array.isArray(anything)){
return 'array';
}
else if (anything === null){
return 'null';
}
else{
return (typeof anything);
}
}
728x90

'코플릿 기록 > JavaScript' 카테고리의 다른 글

배열 9번 getLongestWord  (0) 2023.01.23
배열 8번 getLargestElement  (0) 2023.01.23
데일리코딩 7번 convertListToObject  (0) 2023.01.22
반복문9번★★  (0) 2023.01.16
반복문 8번★★  (0) 2023.01.16