본문 바로가기
반응형

전체 글79

[javascript] 전개연산자, 정규식, reduce 연습 전개 구문을 사용하면 배열이나 문자열과 같이 반복 가능한 문자를 0개 이상의 인수 (함수로 호출할 경우) 또는 요소 (배열 리터럴의 경우)로 확장하여, 0개 이상의 키-값의 쌍으로 객체로 확장시킬 수 있습니다. function sum(x, y, z) { return x + y + z; } const numbers = [1, 2, 3]; console.log(sum(...numbers)); // Expected output: 6 console.log(sum.apply(null, numbers)); // Expected output: 6 [^0-9] : 0부터 9까지 포함하지 않음 reduce() 메서드는 배열의 각 요소에 대해 주어진 리듀서 (reducer) 함수를 실행하고, 하나의 결과값을 반환합니다. .. 2023. 9. 14.
[javascript] 모음제거, replaceAll(), replace(), 정규식 연습 연습 function solution1(string) { let letter = ['a', 'e', 'i', 'o', 'u']; letter.map((e) => { string = string.replaceAll(e, ''); }); return string; } function solution2(string) { return string.replace(/[aeiou]/g, ''); } let string = ['Did you receive my purchase order?', 'Yes. I checked the e-mail just now.']; for (let i = 0; i < string.length; i++) { console.log('solution1', solution1(string[i])).. 2023. 9. 13.
[javascript] 가장 큰수 2개 찾기, sort(), Math.max(), splice(), indexOf() sort() 메서드는 배열의 요소를 적절한 위치에 정렬한 후 그 배열을 반환합니다. 정렬은 stable sort 가 아닐 수 있습니다. 기본 정렬 순서는 문자열의 유니코드 코드 포인트를 따릅니다. const months = ['March', 'Jan', 'Feb', 'Dec']; months.sort(); console.log(months); // Expected output: Array ["Dec", "Feb", "Jan", "March"] const array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); // Expected output: Array [1, 100000, 21, 30, 4] Math.max() 함수는 입력값으로 받은 .. 2023. 9. 12.
[javascript] 제곱수 판별하기, 정수 판별하기, Number.isInteger(), Math.sqrt() Number.isInteger() 메서드는 주어진 값이 정수인지 판별합니다. function fits(x, y) { if (Number.isInteger(y / x)) { return 'Fits!'; } return 'Does NOT fit!'; } console.log(fits(5, 10)); // Expected output: "Fits!" console.log(fits(5, 11)); // Expected output: "Does NOT fit!" Math.sqrt() 함수는 숫자의 제곱근을 반환합니다. Math.sqrt(9); // 3 Math.sqrt(2); // 1.414213562373095 Math.sqrt(1); // 1 Math.sqrt(0); // 0 Math.sqrt(-1); // .. 2023. 9. 4.
[javascript] 거듭제곱, **, << 거듭제곱 연산자(**)는 왼쪽 피연산자를 밑, 오른쪽 피연산자를 지수로 한 값을 구합니다. BigInt 도 피연산자로 받을 수 있다는 점을 제외하면 Math.pow() 와 같습니다. console.log(3 ** 4); // Expected output: 81 console.log(10 ** -2); // Expected output: 0.01 console.log(2 ** (3 ** 2)); // Expected output: 512 console.log((2 ** 3) ** 2); // Expected output: 64 왼쪽 시프트 ( 2023. 9. 3.
[javascript] 특정 문자 제거하기, split(), join(), replaceAll() split() 메서드는 String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눕니다. const str = 'The quick brown fox jumps over the lazy dog.'; const words = str.split(' '); console.log(words[3]); // Expected output: "fox" const chars = str.split(''); console.log(chars[8]); // Expected output: "k" const strCopy = str.split(); console.log(strCopy); // Expected output: Array ["The quick brown fox jumps over the lazy dog."] jo.. 2023. 9. 2.
반응형