객체의 key는 보기에는 ""가 없지만 string 타입이다.
break - 반복문에서 더 이상 반복문을 진행하지 않고 종료하고자 할 때 사용
continue - 반복문에서 현재 실행중인 반복문을 멈추고 다음 반복문으로 바로 진행할 때 사용
Object.entries()
는 key와 value로 이루어진 배열을 리턴한다.
const obj = {
a: 'somestring',
b: 42
};
for (el of Object.entries(obj)) {
console.log(el);
}
//["a", "somestring"]
//["b", 42]
for문에서 관용적으로 쓰는 i는 index를 말한다.
function mostFrequentCharacter(str) {
let obj = {'mostFrequent' : '' , 'mostCount' : 0}
for(let i = 0 ; i < str.length ; i++){
if(str[i] === ' '){
continue
}
if(obj[str[i]] === undefined){
obj[str[i]] = 0
}
obj[str[i]]++
if(obj[str[i]] > obj['mostCount']){
obj['mostFrequent'] = str[i]
obj['mostCount'] = obj[str[i]]
}
}
return obj['mostFrequent']
}
// 변수 obj를 선언한다. {'mostFrequent' : '' , 'mostCount' : 0} 을 할당한다.
//for문을 만든다. i = 0 ; i < str.length ; i++
//만약 ' ' 스페이스바가 나오면
//무시한다!
//만약 obj[str[i]]가 undefined 라면 (obj에 'a'가 없다면)
// obj[str[i]] = 0 해준다.
//obj[str[i]]++ 해주면 obj뒤에 {'a' : 6 , 'b': 2...} 이런식으로 배열이 만들어진다.
//만약 obj[str[i]]가 obj['mostCount']보다 크다면
//obj['mostCount']의 value는 obj[str[i]] 이 된다.
//obj['mostFrequent']의 value는 str[i]가 된다.
//obj['mostFrequent']를 리턴한다.