1. 문자열 4 ~ 6

2020. 6. 28. 15:48Retrospection/Sprint

4

Given a word, "isOddLength" returns whether the length of the given word is odd. 

 

function isOddLength(word) {
  // your code here
}
function isOddLength(word) {
    if(word.length % 2 === 1){
        return true;
    }
  return false;
}

 

홀수를 찾기위해 2로 나누었을때 나머지가 1일 경우로 조건문을 짰다


 

5.

Given a word, "isEvenLength" returns whether the length of the word is even. 

 

function isEvenLength(word) {
  // your code here
}
function isEvenLength(word) {
    if(word.length % 2 === 0){
        return true;
    }return false;
}

 

 

짝수를 찾기 위해 2로 나눈 나머지가 0일 경우를 조건문을 만들었다. 


6.

Given 3 strings, "findShortestOfThreeWords" returns the shortest of the given strings. (문자열 3개가 주어졌을때, "findShortestOfThreeWords" 함수는 주어진 문자열 중 가장 짧은 문자열을 반환해야 합니다.)

Notes:

  • If there are ties, it should return the first word in the parameters list. (만약 동률이 있다면, 그 중 앞에 있는 문자열을 반환해야 합니다.)
function findShortestOfThreeWords(word1, word2, word3) {
  // your code here
}
function findShortestOfThreeWords(word1, word2, word3) {
    let shortestWord = word1 
    if(word1.length > word2.length){
        shortestWord = word2;
    }else if( word3.length < word1.length){
        shortestWord = word3
    }return shortestWord;
}

위에가 내가 푼 방법. 

이 문제 어떻게 해야할지 감도 안잡혀서 레퍼런스 보고 참고했다. 

테스트는 통과함. 

근데 레퍼런스랑 되게 달라서 당황스럽다.. 아직 내가 왜 통과했는지 납득이 안되는데 천천히 생각해보자.

1. 가장 짧은 문자를 word1으로 설정해놨다.

2.가장 짧은 문자인 word 1보다 word 2 가 짧을때와  - shortestWord = word2 

3.word1보다 word3가 짧을때 - shortestWord = word3

4. 2번 3번 조건문을 만들었고 

5. shortestWord를 리턴해주었더니 

6. word1, word2, word3 각각 리턴되는 함수가 되었다 

 

근데 여기서 의문점 

1- "만약 동률이 있다면 그중 앞에있는 문자열을 반환해야한다 " 라는 부분을 전혀 충족시키지 못함 . (근데도 테스트가 통과했다. 저부분을 좀더 코딩해봐야 할거같음 )

        가장짧은 단어를 word1 으로 설정한 이유가 있었다. 동률이면 앞의 단어가 나오는 거니까. 

       조건문을 만들때 <= 를 사용하지 않은 이유도 , 만약 같은 값이 나오는 경우 앞전에 선언해놓은 word1 이 가장짧은 단어가 되는거기때문에. 이해함.

2-경우의 수가 더 많을거같은데 ??? 가장 짧은 단어를 word2 , word3 하나씩 더 설정해서 조건문을 각각 만들었어야 하는거 아닐까?? 

           2번은 차근차근 생각해보니 이해가 되었다. 굳이 그렇게 조건을 각각 만들지 않아도 일단 가장짧은 걸 하나 설정해놓고 그것보다 짧았을때 라는 조건을 설정한거여서 가장 짧은 길이의 단어를 추출해내는 경우의수는 다충족됨. 

~경우의수~ 

word1 <(2 or 3 순서는 모름) 

word2 < word1< word3 / word2= word1 <word3

word3< word1< word2  /word3= word1 <word2

 

 

 

 

 

일단 레퍼런스는 

function findShortestOfThreeWords(word1, word2, word3) {
  let shortestWord = word1

  // 첫 번 째 글자가 2번째 글자보다 길 때
  if (word1.length > word2.length) {
    shortestWord = word2
    // word 2 랑 word 3을 비교
    if (word2.length > word3.length) {
      shortestWord = word3
    }
  } else {
    // word1 과 word3 을 비교
    if (word1.length > word3.length) {
      shortestWord = word3
    }
  }
  return shortestWord
}```

이거였다. 하나하나 분석해보자 .

1. 젤 작은 값이 word1 선언

2.if 문 사용 word1.length > word2.length 일때 word2 가 젤 짧은값 

3.그 안에 if문을 하나더 만들어 word2길이가 word3길이보다 길때 word3가 젤 짧은값 .

4.2번 if 문을 중괄호로 닫고 else 안에 if 문 만들어줌  , word1 길이가 word3보다 길때 word3가 젤 짧은값 .

5. 그리고 return 

 

 

위의 레퍼런스를 참고하여 shortestWord 를 word3 로 설정해놓고 다시 코딩을 해보았다 . 

function findShortestOfThreeWords(word1, word2, word3) {
    let shortestWord = word3 ;
    if(word2.length <= word3.length){
        shortestWord = word2;
        if(word1.length <= word2.length){
            shortestWord = word1;
        }
    }else if(word1.length <= word3.length){
        shortestWord = word1
    }
    return shortestWord;
}

위의 방법은 통과했다. 

아직 좀 이해가 안가는 부분이 있어서 help desk에 문의했다. 

'Retrospection > Sprint' 카테고리의 다른 글

2. 수학 - 1~3.  (0) 2020.07.02
1. 문자열 7 , 8  (0) 2020.06.28
1. 문자열 1~3  (0) 2020.06.28
0. 조건문 9 , 10  (0) 2020.06.28
0. 조건문 7, 8  (0) 2020.06.27