codewarsチャレンジシリーズ(三):The Feast of Many Best and…


1.The Feast of Many Beasts
/**
                  
   
*/
//            
function feast(beast, dish) {
  return beast[0] === dish[0] && beast[beast.length-1] === dish[dish.length-1]
}

// slice  substr  startsWith            
function feast(beast, dish) {
  return beast.charAt(0)===dish.charAt(0)&&beast.substr(-1)===dish.substr(-1)
}
}
2.The Supermarket Que
/**
      ,          ,      ,            ,     。           。

  :        ,              。                ,    0,   n。   customers            arr,          。
*/
function queueTime(customers, n) {
  //TODO
  if(customers.length <= 0) return 0
  let arr = new Array(n);
  arr.fill(0);
  do {
    arr[arr.indexOf(Math.min.apply(null, arr))] += customers.shift()
  } while (customers.length > 0)
  return Math.max.apply(null, arr)
}

//     , customers  ,           ;              
function queueTime(customers, n) {
  let arr = new Array(n).fill(0);
  for (let i of customers) {
    arr[arr.indexOf(Math.min(...arr))] += i;
  }
  return Math.max(...arr);
}
3.Build a Car
/**
    Car    ,  body  、chassis      ,    component  ,          。
    " "+"_".repeat(n)      ,   length-2;
    "|" + "[]".repeat(n) + " ".repeat(n) + "[]".repeat(n) + "\\"      ,   length-1;
    "-o".repeat(n) + "-".repeat(n) + "o-".repeat(n) + "'"      ,   length。
    _____________          ______          __________
   |[][]   [][][]\        |[][][]\        |        []\
   -o-o-o-----o-o-'       -o----o-'       -o-o------o-'
      length doors    ,           。     first、second、third     ,         
function Car(length, doors) {
 if(length<7 || doors === 0 || doors*2 > length-3){
    throw new Error()
  }
  let first = second = third = ""
  this.body = {
    component:  first + "
" + second + "
" } this.chassis = { component: third } } // first, , "_" first = ' '+'_'.repeat(length-3) // second, "[]" , , doors/2 let secondDoors = ['[]'.repeat(doors/2|0),'[]'.repeat(Math.ceil(doors/2))]; second = "|" + secondDoors[0] + " ".repeat(length-3-doors*2) + secondDoors[1] + "\\"; //third, let axlesElse = length-12 > 0 ? Math.floor((length-12)/2)+1 : 0; for(let i = 0; i < axlesElse; i++){ thirdAxles[(i%2)]+="o-" } let third = thirdAxles[0] + '-'.repeat(length-1-2*axlesElse-3*2) + thirdAxles[1] + "'"; */ // function Car(length, doors) { if(length<7 || doors === 0 || doors*2 > length-3){ throw new Error() } let thirdAxles = ["-o-","-o-"]; let axlesElse = length-12 > 0 ? Math.floor((length-12)/2)+1 : 0; for(let i = 0; i < axlesElse; i++){ thirdAxles[(i%2)]+="o-" } let third = thirdAxles[0] + '-'.repeat(length-1-2*axlesElse-3*2) + thirdAxles[1] + "'"; this.body = { component: " " + "_".repeat(length-3) + "
|" + '[]'.repeat(doors/2|0) + " ".repeat(length-3-doors*2) + '[]'.repeat(Math.ceil(doors/2)) + "\\
" } this.chassis = { component: thirdAxles[0] + '-'.repeat(length-1-2*axlesElse-3*2) + thirdAxles[1] + "'" } }