[javascript] exam ๐Ÿ˜ณ

๋‚˜์ง€์€ยท2020๋…„ 8์›” 29์ผ
0

get_findํ•จ์ˆ˜

๋ฌธ์ž์™€ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„๋•Œ, "get_find" ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์ฒซ๋ฒˆ์งธ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Notes:
๋ฌธ์ž์—ด์˜ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ์ธ๋ฑ์Šค ๊ฐ’ 0 ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
๋งŒ์•ฝ ๋ฌธ์ž์—ด์— ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜ํƒ€๋‚˜๋ฉด, ์ฒซ๋ฒˆ์งธ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, -1 ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
indexOf ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”.

๐Ÿ‘‰๐Ÿป ๋‚˜์˜ ์†”๋ฃจ์…˜

function get_find (character, string) {
  alphabet=string.split("")
  for (let i=0; i<alphabet.length; i++){
    if (character===alphabet[i]){
      return i
    } //๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1 ๋ฐ˜ํ™˜
  }return -1
}

๐Ÿ‘‰๐Ÿป ๋ชจ๋ธ ์†”๋ฃจ์…˜

function get_find(filter, sentence) {
  for (let i = 0; i < sentence.length; i++) {
    if (sentence[i] === filter) {
      return i;
    }
  }
  
  return -1;
}
  • string์„ splitํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.๐Ÿ˜ข
  • ์ฃผ์–ด์ง„ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ -1์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” for๋ฌธ ๋ฐ–์— return -1์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ์ƒ๊ฐํ•ด๋‚ด์งˆ ๋ชปํ–ˆ๋‹ค!
profile
์ฆ๊ฑฐ์›€์„ ์ฐพ๋Š” ๊ฐœ๋ฐœ์ž๐Ÿฏ

0๊ฐœ์˜ ๋Œ“๊ธ€