생각기록
코테 옹알이 사렬줘 쉬ㅠㅠㅠ 본문
js
https://velog.io/@kwb020312/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%98%B9%EC%95%8C%EC%9D%B4-2
function solution(babbling) {
// 해당 풀이는 StarBlitz님의 풀이를 해석하였음을 미리 밝힙니다.
let count = 0;
// 연속되는 경우
let dup = ['ayaaya', 'yeye', 'woowoo', 'mama'];
while (babbling.length) {
let b = babbling.pop();
// 연속되는 발음은 패스
if (dup.some(v=>b.includes(v))) continue;
// 가능한 발음을 모두 숫자로 바꾸어
b = b.replaceAll('aya','1').replaceAll('ye','2').replaceAll('woo','3').replaceAll('ma','4');
// 숫자는 공백으로 변환
b = b.replace(/[1234]/g, '');
// 모두 가능한 발음의 경우 count++
if (b.length === 0) count++;
}
return count;
}
특정 문자열 포함 개수 추출
match함수
forEach
배열 순회
1. 익명함수, 콜백함수, 매개변수 필요
numbers.forEach(function( number ) {
console.log( number ) ;
});
2. 화살표 함수 표시
numbers.forEach( number => console.log(number) );
3. 매개변수
numbers.forEach( ( number, index ) => {
console.log( 'Index : ' + index + 'value : ' + number);
});
4. 배열
numbers.forEach ((number, index, array ) => {
console.log(array);
});
var array=[1,2,3]
array.forEach(x=>{
console.log(x)
})
1
2
3
https://hianna.tistory.com/377
split() 함수
기본 구조
string.split (separator, limit )
문자열 'separator'로 잘라서 > 문자열을 잘라 줄 구분자 ( 문자열 or 정규식 / 값이 없으면 문자열 전체 배열 담아 리턴 )
'limit' 크기 이하의 > 최대 분할 갯수
배열에 잘라진 문자열을 저장하여 리턴한다
const arr = str.split();
apple banana orange 1
const arr = str.split(" ");
apple banana orange 3
const arr = str.split("");
a b c 5
Array.join()
join() 매서드는 배열의 모든 요소를 연결해 하나의 문자열로 만든다.
arr.join([separator])
separator은 매개변수 > 배열의 각 요소를 구분할 문자열
이 구분자는 필요한 경우 문자열로 변환되고, 생략하면 배열의 원소들의 쉼표로 구분된다.
const arr = ['바람', '비', '물'];
console.log(arr.join());
// 바람,비,물
console.log(arr.join(''));
// 바람비물
console.log(arr.join('-'));
// 바람-비-물
replace()
str_text.replace("찾을 문자열", "변경할 문자열")
(a,b)
a를 b로 바꾼다
다만 replace 특성이 먼저 앞부분만 변환 가능
모두 바꾸려면 정규식 사용
var test = "가나다라 마바사 가나";
var result = test.replace( /가/gi, '나');
console.log(result);
나나다라 마바사 나나
// 포함된 모든 "가"는 "나"로 변환되었음
여기서 꼭 알아야 될 점은 나누기(/)표시안에 넣는 텍스트의 따옴표는 없어야 한다는 점입니다. 그리고 뒤의 gi는 다음을 의미합니다.
g : 전체 모든 문자열을 변경 global
i : 영문 대소문자를 무시, 모두 일치하는 패턴 검색 ignore
function solution(babbling) {
var answer = 0;
for(let s of babbling) {
let temp ="";
s = s.replace("ayaaya","x");
s = s.replace("yeye","x");
s = s.replace("woowoo","x");
s = s.replace("mama","x");
do {
temp = s;
s = s.replace("aya","");
s = s.replace("ye","");
s = s.replace("woo","");
s = s.replace("ma","");
} while(temp != s)
if (s === "") {
answer++;
}
}
return answer;
}
Array.prototype.some()
some()은
배열의 각 엘리먼트에 대해서 테스트 함수의 반환 값이 하나라도 true가 있는지 확인합니다.
하나라도 true가 발생하면 true를 반환합니다.
모두 false인 경우만 false를 반환합니다.
.includes()
문자열이 특정 문자열을 포함하는지 확인하는 메서드이다. IE는 Edge부터 지원한다.
string.includes( searchString, length )
searchString : 검색할 문자열로 필수 요소이다. 대소문자를 구분한다.
length : 검색을 시작할 위치이다. 선택 요소로, 값이 없으면 전체 문자열을 대상으로 한다.
'abzcd'.includes( 'z' )
true를 반환
match()
특정 텍스트 안에 검색할 단어, 찾고 싶은 단어가 있는경우 해당 텍스트가 문구에 포함되어 있는지 확인할 수 있습니다. 또한 단어뿐만 아니라 정규표현식을 사용하여 특정 패턴을 검색하는 것 역시 가능합니다. 아래는 match() 함수를 사용하는 간단한 방법입니다.
map()함수
indexof()
원하는 문자열 찾는
string.indexOf(searchvalue, position)
indexOf 함수는, 문자열(string)에서 특정 문자열(searchvalue)을 찾고,
검색된 문자열이 '첫번째'로 나타나는 위치 index를 리턴합니다.
찾는 문자열이 없으면 -1을 리턴합니다.
문자열을 찾을 때 대소문자를 구분합니다.
function solution(babbling) {
const arr = ["aya", "ye", "woo", "ma"];
let answer = babbling.map((str) => {
let text = str;
arr.forEach((str2) => {
if (str.indexOf(`${str2}${str2}`) !== -1) {
text = text.replace(str2, '');
} else {
text = text.replaceAll(str2, '');
}
});
return text;
}).filter((str) => !str).length;
return answer;
}