승표의 코드

1) indexOf를 활용하는 방법
let str = window.prompt('문자열을 입력해주세요.');
let findStr = window.prompt('찾을 단어를 입력해주세요');
console.log(str.indexOf(findStr));

2) 2중 for문 사용하는 방법
const str = window.prompt('문자열을 입력해주세요.');
const findStr = window.prompt('찾을 단어를 입력해주세요');
const leng = findStr.length;

let checkStr = '';
/*
입력
pineapple is yummy
출력
apple

입력
aaapap
출력
pap
*/

for (let i = 0; i < str.length; i++) {
  if (str[i] === findStr[0]) {
    checkStr += str[i];
    for (let j = 1; j < findStr.length; j++) {
      checkStr += str[i + j];
    }
    if (checkStr === findStr) {
      console.log(i);
    }
  }
  checkStr = '';
}

나경의 코드


수지의 코드