https://programmers.co.kr/learn/courses/30/lessons/17683
function solution(m, musicinfos) {
var answer = '';
var answerSet =[];
// *** #제거
var newM = m.replace(/(C#)/g, 'c')
.replace(/(D#)/g, 'd')
.replace(/(F#)/g, 'f')
.replace(/(G#)/g, 'g')
.replace(/(A#)/g, 'a');
var newMusicInfos = musicinfos.map(str=>str.replace(/(C#)/g, 'c')
.replace(/(D#)/g, 'd')
.replace(/(F#)/g, 'f')
.replace(/(G#)/g, 'g')
.replace(/(A#)/g, 'a')
);
//문자열=>배열화
var musicInfos = newMusicInfos.map(info=> info.split(","));
// console.log(musicInfos);
//음악의 길이 = 악보정보.length;
//재생멜로디 = 재생시간만큼 문자열 반복(문자열연결)
for(var i=0; i<musicInfos.length; i++){
var HH = musicInfos[i][1].substring(0,2)-musicInfos[i][0].substring(0,2);
var MM = musicInfos[i][1].substring(3,5)-musicInfos[i][0].substring(3,5);
var length = HH*60 + MM;
var played = "";//***
//악보정보 배열화
var note = musicInfos[i][3].split(""); //array
//재생멜로디
for(var j=0; j<length; j++){
played = played + note[j%note.length];
}
// console.log(played);
// 문자열 포함관계확인해서 곡제목 도출 - 정규식
if(played.match(newM)){ // *** 예외처리
var obj = {};
obj[musicInfos[i][2]] = length;
answerSet.push(obj); //제목, 재생시간
}
}
console.log(answerSet);
if(answerSet.length === 1){ // ***예외처리
return Object.keys(answerSet[0])[0];
} else if (answerSet.length === 0){
return '(None)';
} else { // *** 여러개일 때
answerSet.sort((a,b)=> {
if(Object.values(a)[0]===Object.values(b)[0]){
return a.index - b.index;
}else{
return Object.values(b)[0]-Object.values(a)[0];
}
// console.log(Object.values(a)[0]);
})
// console.log('hi');
return Object.keys(answerSet[0])[0];
}
}
2.채점 시 20, 21, 30 실패 => 중복되는 경우 예외처리를 하지 않음.
```javascript
function solution(m, musicinfos) {
var answer = '';
var answerSet =[];
// *** #제거
var newM = m.replace(/(C#)/g, 'c')
.replace(/(D#)/g, 'd')
.replace(/(F#)/g, 'f')
.replace(/(G#)/g, 'g')
.replace(/(A#)/g, 'a');
var newMusicInfos = musicinfos.map(str=>str.replace(/(C#)/g, 'c')
.replace(/(D#)/g, 'd')
.replace(/(F#)/g, 'f')
.replace(/(G#)/g, 'g')
.replace(/(A#)/g, 'a')
);
//문자열=>배열화
var musicInfos = newMusicInfos.map(info=> info.split(","));
// console.log(musicInfos);
//음악의 길이 = 악보정보.length;
//재생멜로디 = 재생시간만큼 문자열 반복(문자열연결)
for(var i=0; i<musicInfos.length; i++){
var HH = musicInfos[i][1].substring(0,2)-musicInfos[i][0].substring(0,2);
var MM = musicInfos[i][1].substring(3,5)-musicInfos[i][0].substring(3,5);
var length = HH*60 + MM;
var played = "";//***
//악보정보 배열화
var note = musicInfos[i][3].split(""); //array
//재생멜로디
for(var j=0; j<length; j++){
played = played + note[j%note.length];
}
// console.log(played);
// 문자열 포함관계확인해서 곡제목 도출 - 정규식
if(played.match(newM)){ // *** 예외처리
return musicInfos[i][2]; //제목, 재생시간
}
}
return '(None)';
}
function solution(m, musicinfos) {
var answer = '';
//문자열=>배열화
var musicInfos = musicinfos.map(info=> info.split(","));
// console.log(musicInfos);
//음악의 길이 = 악보정보.length;
//재생멜로디 = 재생시간만큼 문자열 반복(문자열연결)
for(var i=0; i<musicInfos.length; i++){
var HH = musicInfos[i][1].substring(0,2)-musicInfos[i][0].substring(0,2);
var MM = musicInfos[i][1].substring(3,5)-musicInfos[i][0].substring(3,5);
var length = HH*60 + MM;
var played = "";//***
//악보정보 배열화
var note = musicInfos[i][3].split(""); //array
//재생멜로디
for(var j=0; j<length; j++){
played = played + note[j%note.length];
}
console.log(played);
// 문자열 포함관계확인해서 곡제목 도출 ***
//정규식
if(played.match(m)){
if(played[played.indexOf(m)+m.length] !== "#"){//다음글자 #이 아닌 경우에만
// console.log(played.indexOf(m)+m.length);
return musicInfos[i][2];
}
}
}
//example 3 실패 -> #을 고려하지 않음..
// return answer;
}