美文网首页
关于js知识点的杂文

关于js知识点的杂文

作者: Molly6943 | 来源:发表于2017-07-11 10:46 被阅读0次

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知识点的杂文

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

  • 2018-08-22

    今天简单了解了一下关于js对象的知识点。包括JS 数字 JS 字符串 JS 日期 JS 数组 JS 逻辑 JS 算...

  • 来自一位react新手的react入门须知

    前言:自己刚刚总结的关于react的知识点,比较简单,大家可以粗略看看。 一:关于react的一些知识点 1,Js...

  • js小知识点

    js小知识点 整理 js小知识点 (一):获取元素 1:document.getElementById('id名'...

  • 关于杂文

    今早一醒来,看到亲给我的留言,她准备在群里讲杂文写作。我们简短地讨论后,我做了点功课,算是课前预习。 一、什么是杂...

  • js的浅拷贝与深拷贝

    js的浅拷贝与深拷贝 涉及知识点: js中的数据类型(基本类型,引用类型) js中的存储数据的堆栈相关知识点; 下...

  • 不读杂文,就写不出好杂文

    今天给大家分享一个关于杂文写作的一个事情。 谈到杂文,会想起鲁迅先生。其笔下的杂文数不胜数,当时在特定的时代,所以...

  • 小知识点集汇一

    小知识点集汇一 小知识点 node接口模拟返回 js接口模拟数据 mockjs mocha js 测试框架--教程...

  • 前端知识温习

    js知识点 温习js css知识点 温习css jquery温习 自己实现一个jquery vue框架温习 温习v...

  • JS框架 - 收藏集 - 掘金

    关于js、jq零碎知识点 - 掘金写在前面: 本文都是我目前学到的一些比较零碎的知识点,也是相对偏一点的知识,这是...

网友评论

      本文标题:关于js知识点的杂文

      本文链接:https://www.haomeiwen.com/subject/crmphxtx.html