본문 바로가기
반응형

JavaScript44

[javascript] string 에서 문자 위치 바꾸기 연습 function solution1(string, index1, index2) { let answer = ''; let num1 = index1; let num2 = index2; if (index1 > index2) { num1 = index2; num2 = index1; } for (let i = 0; i < string.length; i++) { if (i == num1) { answer += string[num2]; } else if (i == num2) { answer += string[num1]; } else { answer += string[i]; } } return answer; } function solution2(string, index1, index2) { let answer = .. 2023. 9. 21.
[javascript] 대소문자 바꾸기 for...of, toLowerCase(), toUpperCase() for...of 명령문은 반복가능한 객체 (Array, Map, Set (en-US), String, TypedArray, arguments 객체 등을 포함)에 대해서 반복하고 각 개별 속성값에 대해 실행되는 문이 있는 사용자 정의 반복 후크를 호출하는 루프를 생성합니다. const array1 = ['a', 'b', 'c']; for (const element of array1) { console.log(element); } // Expected output: "a" // Expected output: "b" // Expected output: "c" toLowerCase() 메서드는 문자열을 소문자로 변환해 반환합니다. const sentence = 'The quick brown fox jumps o.. 2023. 9. 17.
[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] 거듭제곱, **, << 거듭제곱 연산자(**)는 왼쪽 피연산자를 밑, 오른쪽 피연산자를 지수로 한 값을 구합니다. 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.
반응형