how to typecheck in 자바스크립트
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number(1) === 'number'; // but never use this form!
typeof 42n === 'bigint';
// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String("abc") === 'string'; // but never use this form!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // but never use this form!
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Objects
typeof {a:1} === 'object';
// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
// The following is confusing. Don't use!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';
// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
how to tell if an object is an array in 자바스크립트
How to check if an object is an array? - Stack Overflow
<배열>
06_getAllWords
문자열을 입력받아 문자열을 구성하는 각 단어를 요소로 갖는 배열을 리턴해야 합니다.
newStr(새로운 문자열)을 선언해주면서 ('I love coding')
-> ('I','love','coding')
newStr=str.split(' ')
newArr=[];
for 문을 사용하여서 newStr의 값들을 newArr에 넣는다.
newArr.push(newStr[i])
function getAllWords(str) {
let newStr = str.split(' ')
let newArr =[];
if(str.length === 0) {
return newArr;
}
for (let i =0; i<newStr.length; i++) {
newArr.push(newStr[i]);
}
return newArr;
}
08_getLargestElement
배열을 입력받아 가장 큰 요소를 리턴해야 합니다.
function getLargestElement(arr) {
let num = 0
for (let value of arr) {
비교하는 대상은 value : 1 , num : 0, value > num ,num = value
value : 4 , num : 1, value >num , num =value : 4
value : 3 , num : 4, value >num , num = num : 4
if (value > num) {
num = value
}
}
return num
}
function getLargestElement(arr) {
let result =0
for (let i =1; i < arr.length; i++) {
if(arr[i]> result){
result = arr[i]; --> 치환한다
}
}
return result;
}
09_getLongestWord
문자열을 입력받아 문자열에서 가장 긴 단어를 리턴해야 합니다.
function getLongestWord(str) {
let newStr = str.split(' ');
let start= newStr[0]
for (let i =1; i <newStr.length; i++) {
if (newStr[i].length > start.length) {
start = newStr[i]
}
}
return start;
}
10_getEvenNumbers
수를 요소로 갖는 배열을 입력받아 짝수만을 요소로 갖는 배열을 리턴해야 합니다. --> filter와 비슷하다고 본다.
function getEvenNumbers(arr) {
let newArr=[];
for (let i=0; i<arr.length; i++) {
if(arr[i]%2 ===0) {
return newArr.push(arr[i]);
}
}
return newArr;
}
11_addToFront
배열과 요소를 입력받아 주어진 요소를 배열의 맨 앞에 추가하고 해당 배열을 리턴해야 합니다.
js how to add to front in array
15_getElementsUpTo
배열과 인덱스를 입력받아 주어진 인덱스 이전의 요소들을 갖는 새로운 배열을 리턴해야 합니다.
function getElementsUpTo(arr,n) {
let newArr =[];
if (n >=arr.length) {
return newArr;
}
return newArr.concat(arr.slice(0,n));
}///arr.slice(0,n) --> 배열로 변환된다 ////