본문 바로가기
반응형

전체 글79

[javascript] 배열 원소별 개수 세기 let string = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus eligendi ut ullam, temporibus non repellat, veniam nam voluptatibus beatae pariatur ea et sapiente. Nulla eveniet tenetur dolores libero facilis fuga.'; let object = string.split('').reduce((acc, cur) => { return (acc[cur] = (acc[cur] || 0) + 1), acc; }, {}); console.log(object); 결과 2023. 10. 16.
[javascript] 마우스 드래그 스크롤 만들기 let slider1 = document.querySelector('#original_list'); // scroll 이 적용된 id or class 선택 let isDown1 = false; let startX1; let scrollLeft1; slider1.addEventListener('mousedown', e => { isDown1 = true; startX1 = e.pageX - slider1.offsetLeft; scrollLeft1 = slider1.scrollLeft; }); slider1.addEventListener('mouseleave', () => { isDown1 = false; }); slider1.addEventListener('mouseup', () => { isDown1 =.. 2023. 10. 16.
[javscript] 약수 구하기 let num = [4, 6, 21, 24, 25, 32]; function solution(num) { let divisors = []; for (let i = 1; i 2023. 10. 13.
[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] 한 번만 등장한 문자 찾기, reduce(), indexOf(), lastIndexOf(), filter(), match() reduce() 메서드는 배열의 각 요소에 대해 주어진 리듀서 (reducer) 함수를 실행하고, 하나의 결과값을 반환합니다. arr.reduce(callback[, initialValue]) const array1 = [1, 2, 3, 4]; // 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array1.reduce((accumulator, currentValue) => accumulator + currentValue, initialValue); console.log(sumWithInitial); // Expected output: 10 indexOf() 메서드는 배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고 존재.. 2023. 9. 20.
[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.
반응형