[javascript]Section2 Daily Coding 11,12,13,14 RECAP

KoEunseo·2022년 9월 25일
0

Daily_Coding

목록 보기
14/21

11

function removeExtremes(arr) {
  //['where', 'is', 'the', 'longest', 'word']
  let shortest = { leng : 20, idx : undefined }
  let longest = { leng : 0, idx : undefined }
  

  arr.map((el, idx) => {
    if(el.length <= shortest.leng){
      shortest.leng = el.length;
      shortest.idx = idx;
    } else if(el.length >= longest.leng){
      longest.leng = el.length;
      longest.idx = idx;
    }
  })

  let result = [];
  //가장 짧은것과 긴것을 0으로 대체(idx가 바뀌는 것 방지)
  arr.splice(shortest.idx, 1, 0);
  arr.splice(longest.idx, 1, 0);
  //0 없애기  
  return arr.filter(el => el !== 0);
}

12

function findBugInApples(arr) {
  let result = [];
  for(let i = 0; i < arr.length; i++) {
    arr[i].map((el, idx) => {
      if(el === 'B') { 
        result.push(i);
        result.push(idx);
      }
    })
  }
  return result;
}

13

function readVertically(arr) {
 let result = '';
 let max = 0;
 arr.forEach(el => {
   if(el.length > max) max = el.length;
 })

 for(let i = 0; i < max; i++){
   for(let j = 0; j < arr.length; j++){
     if(arr[j][i] !== undefined) result += arr[j][i];
   }
 }
 return result; 
}

이상하게 이 문제가 안풀려서 검색의 힘을 좀 받았다.
나중에 다시한번 풀어봐야지 될 것 같음...!!!

14

function superIncreasing(arr) {
  let before = arr[0]
  for(let i = 1; i < arr.length; i++){
    if(before < arr[i]){
      before = before + arr[i];
    } else {
      return false;
    }
  }
  return true;
}
profile
주니어 플러터 개발자의 고군분투기

0개의 댓글