关于js知识点的杂文

Math.floor()

Math.floor() 返回小于或等于一个给定数字的最大整数,俗话说就是向下取整。

example:
Math.floor(-44.9)
// -45
Math.floor(44.9)
// 44
Math.floor(44)
// 44
Math.floor(-44)
// -44
编程实例:

Sam has opened a new sushi train restaurant - a restaurant where sushi is served on plates that travel around the bar on a conveyor belt and customers take the plate that they like.

Sam is using Glamazon's new visual recognition technology that allows a computer to record the number of plates at a customer's table and the colour of those plates. The number of plates is returned as a string. For example, if a customer has eaten 3 plates of sushi on a red plate the computer will return the string 'rrr'.

Currently, Sam is only serving sushi on red plates as he's trying to attract customers to his restaurant. There are also small plates on the conveyor belt for condiments such as ginger and wasabi - the computer notes these in the string that is returned as a space ('rrr r' //denotes 4 plates of red sushi and a plate of condiment).

Sam would like your help to write a program for the cashier's machine to read the string and return the total amount a customer has to pay when they ask for the bill. The current price for the dishes are as follows:

Red plates of sushi ('r') - $2 each, but if a customer eats 5 plates the 5th one is free.
Condiments (' ') - free.
Input: String
Output: Number

Examples:

Input: 'rr' Output: 4
Input: 'rr rrr' Output: 8
Input: 'rrrrr rrrrr' Output: 16

这段英文虽然比较长,但是幸好的是,都是简单的英语,相信大家都能看懂,我就不翻译啦~

答案:

function totalBill(str) {
  let total = 0;
  for(let i = 0; i < str.length; i++) {
    if (str[i] === 'r') total++;
  }
  return Math.floor(total/5) * 8 + (total % 5 * 2);
}

你可能感兴趣的:(关于js知识点的杂文)