내가 처음 쓴 코드
function findTheBug(word) {
let result;
for(let i= 0; i < word.length; i= i+1){
if(word.substr(i,1) === '#'){
result = word.indexOf('#')
}else{
result = undefined
}
}
return result
}
reference보고 고친 코드
function findTheBug(word) { for(let i= 0; i < word.length; i= i+1){ if(word.substr(i,1) === '#'){ return word.indexOf('#') } } return undefined }
원래코드로 치면 왜 console.log로 쳐도 '#lizard'를 치면 0이라고 잘 나오는데 undefined가 나오는걸까??
break를 쓰니까 바로 test에 다 통과했다
반복문은 임의로 멈추지 않는이상 끝까지 돌아간다고 한다. 반복문이 끝까지 돌아가면서 else문이 실행되는 것이라고 한다. break에 대해서 공부를 해야겠다.