codewars挑战系列(三):The Feast of Many Beasts 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 Queue

/**
超市排队结算,第一个参数为序列数组,元素表示时长,第二个参数为收银台的个数,不允许插队。计算出所有人结算的时间。

思路:要计算最后的时间,就要比较得出耗时最长的收银台。可以定义数组存放每个收银台的耗时,初始都为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 + "\n" + second + "\n"
  }
  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) + "\n|" + '[]'.repeat(doors/2|0) + " ".repeat(length-3-doors*2) + '[]'.repeat(Math.ceil(doors/2)) + "\\\n"
  }
  this.chassis = {
    component: thirdAxles[0] + '-'.repeat(length-1-2*axlesElse-3*2) + thirdAxles[1] + "'"
  }
}

你可能感兴趣的:(javascript,codewars,string)