코플릿 기록/JavaScript
배열 1번
뭉지야
2023. 1. 23. 13:29
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