JS 중간시험

younghyun·2022년 2월 12일
0

getFind 함수를 작성하세요.

문자와 문자열이 주어졌을때, getFind 함수는 주어진 문자열에서 주어진 문자가 나타나는 첫번째 위치를 반환합니다.

Notes: 문자열의 첫번째 문자는 인덱스 값 0 을 가집니다. 만약 문자열에 해당 문자가 여러번 나타나면, 첫번째로 나타나는 위치를 반환해야 합니다. 만약 문자가 문자열에 존재하지 않는다면, -1 을 반환해야 합니다.

중요!! indexOf 함수를 사용하지 마세요.

const output = getFind('a', 'I am a hacker')
console.log(output) // --> 2

function getFind(filter, sentence) { 
  // 아래 코드를 작성해주세요.
  const findWord = sentence.search(filter);

  return findWord;

}

const output = getFind('a', 'I am a hacker')
console.log(output) // --> 2

// 아래의 코드는 절대로 수정하거나 삭제하지 마세요.
module.exports = {getFind}

search()함수

문자열에서 조건 문자열을 찾아서 몇 번째 위치에 있는지 확인해줌.
첫 번째로 매치 되는 인덱스를 반환하게 되지만, 찾지 못하면 -1 반환.

find_longest_word 함수를 만들어 주세요.
주어진 리스트안에 있는 단어중 가장 긴 단어를 찾을수 있도록 함수를 완성해주세요.

console.log(find_longest_word(["PHP", "Exercises", "Backend"])) 
// --> "Exercises"
function find_longest_word(arr) {
  // 아래 코드를 구현해주세요.
  let length = arr.map(word => word.length)
  console.log(length);
  let maxLength = Math.max(...length); // 전개구문 ( ... ) 활용해 배열 요소 나열해서 최대 값 찾음.
  console.log(maxLength)
  let maxLengthPosition = length.indexOf(maxLength); 
  return arr[maxLengthPosition];  
}

console.log(find_longest_word(["PHP", "Exercises", "Backend"])) 

// 아래의 코드는 절대로 수정하거나 삭제하지 마세요.
module.exports = {find_longest_word}
profile
선명한 기억보다 흐릿한 메모

0개의 댓글