1、使用Array.includes处理多种条件

function test(fruit) {if (fruit == 'apple' || fruit == 'strawberry') {console.log('red');
}}
function test(fruit) {const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];if (redFruits.includes(fruit)) {console.log('red');}
}

2、减少嵌套,尽早return

function test(fruit, quantity) {const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];if (fruit) {if (redFruits.includes(fruit)) {console.log('red');if (quantity > 10) {console.log('big quantity');}}} else {throw new Error('No fruit!');}
}
test(null); // error: No fruits
test('apple'); // print: red
test('apple', 20); // print: red, big quantity
function test(fruit, quantity) {const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];if (!fruit) throw new Error('No fruit!');if (redFruits.includes(fruit)) {console.log('red');if (quantity > 10) {console.log('big quantity');}}
}
function test(fruit, quantity) {const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];if (!fruit) throw new Error('No fruit!'); if (!redFruits.includes(fruit)) return; console.log('red');if (quantity > 10) {console.log('big quantity');}
}

3、使用函数默认参数和解构

function test(fruit, quantity) {if (!fruit) return;const q = quantity || 1; console.log(`We have ${q} ${fruit}!`);
}
test('banana'); // We have 1 banana!
test('apple', 2); // We have 2 apple!
function test(fruit, quantity = 1) { if (!fruit) return;console.log(`We have ${quantity} ${fruit}!`);
}
test('banana'); // We have 1 banana!
test('apple', 2); // We have 2 apple!
function test(fruit) { if (fruit && fruit.name)  {console.log (fruit.name);} else {console.log('unknown');}
}
test(undefined); // unknown
test({ }); // unknown
test({ name: 'apple', color: 'red' }); // apple
function test({name} = {}) {console.log (name || 'unknown');
}
test(undefined); // unknown
test({ }); // unknown
test({ name: 'apple', color: 'red' }); // apple
// 在执行test(undefined)这行代码时就会报错:无法解析’undefined’或’null’的属性name,因为undefined没有name这个属性
function test(fruit) {console.log(__.get(fruit, 'name', 'unknown');
}
test(undefined); // unknown
test({ }); // unknown
test({ name: 'apple', color: 'red' }); // apple

4、对所有或部分规则使用Array.every和Array.some

const fruits = [{ name: 'apple', color: 'red' },{ name: 'banana', color: 'yellow' },{ name: 'grape', color: 'purple' }];
function test() {let isAllRed = true;for (let f of fruits) {if (!isAllRed) break;isAllRed = (f.color == 'red');}console.log(isAllRed); // false
}
const fruits = [{ name: 'apple', color: 'red' },{ name: 'banana', color: 'yellow' },{ name: 'grape', color: 'purple' }];
function test() {const isAllRed = fruits.every(f => f.color == 'red');console.log(isAllRed); // false
}
const fruits = [{ name: 'apple', color: 'red' },{ name: 'banana', color: 'yellow' },{ name: 'grape', color: 'purple' }
];
function test() {const isAnyRed = fruits.some(f => f.color == 'red');console.log(isAnyRed); // true
}

Judge Simple(判断-简单)相关推荐

  1. 用Python条件判断简单制作一个12星座速配工具

    今天是学习Python语言第二天,今天主要学习的是Python的条件判断,感觉和php基本上还是比较类似的.感觉Python的输出函数比较实用,在学习Python条件判断的过程中,突发奇想我可以借助P ...

  2. JAVA 判断简单密码算法_十道简单算法题二【Java实现】

    前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...

  3. UVALive 3351 Easy and Not Easy Sudoku Puzzles 位运算~判断简单数独

    题意:给定一个9*9的数独,要求判断是否为简单数独. 数独:对于每一行每一列或者子方格内,只能填1~9这几个数,并且每个数字只能出现一次,比如说: 如果一个9*9的数独是简单数独的话,这个数独的解是独 ...

  4. HDU-4145 A Simple Problem 简单数论

    这题给定一个N,求最小的x^2满足y^2 = x^2 + N; 将x^2移到左边,得到(y+x)*(y-x) = N,于是我们分解N成两个约数相乘的形式,于是我们从sqrt(N)到1开始枚举约数,得到 ...

  5. Net设计模式之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction)        简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实 ...

  6. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  7. 基于微信小程序的五子棋小程序(含简单人机)

    基于微信小程序的五子棋小程序(含简单人机) 运行截图 项目结构目录 基本思路 实现过程 棋盘的生成 落子 判断胜负 悔棋 人机对战的实现 权值表 机器人落子逻辑 改进胜负判断方法 更多功能 结语 运行 ...

  8. 【java简单小项目】勇者斗魔王小游戏

    项目的github链接 简要介绍 该项目主要是在控制台实现了回合制的勇者斗魔王的小游戏,涉及到的主要知识为java的类和对象,采用了三层模式设计,可供java初学者学习简单小型项目的架构设计,和具体的 ...

  9. [kuanbin带我飞]简单搜索

    今天做了几道kuanbin专题,今天做的算是搜索入门题吧,不过自己确实做得够呛(黑脸) 迷宫问题 POJ - 3984 这道题就是bfs的模板题了,题意很简单,就找到从左上角到右下角的最短路线,这又让 ...

最新文章

  1. PyTorch版YOLOv4更新了,不仅适用于自定义数据集,还集成了注意力和MobileNet
  2. 玻璃质感_他的玻璃质感让人佩服的五体投地
  3. 服务器系统由32位的吗,云服务器有Windows32位系统吗
  4. Hive SQL优化之 Count Distinct
  5. 课时85.层叠性(掌握)
  6. Matlab一个窗口中绘制多个图形
  7. 请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合?...
  8. 关于 django 的时区设置与MySQL 时间相差8小时
  9. ARM学习篇 中断定时理解
  10. 安装JDK失败,再次安装时出现已经安装过了的,解决办法
  11. CountDownLatch和CyclicBarrier 举例详解
  12. 《运维工程师成长之路》一2.2 小结
  13. WIN10配置JAVA环境变量
  14. SQL Server 2008R2密钥
  15. html静态页面作业 我的家乡网站设计——我的家乡-杭州(7页) HTML+CSS+JavaScript 大学生家乡网页作品 老家网页设计作业模板 学生网页制作源代码下载
  16. 第四章 软件总体设计 1
  17. 明明有本事,为什么难升职?
  18. 上帝 谭卓 马加爵等人的对话
  19. 生鲜行业渠道商经销管理系统:加强生鲜渠道连接,提升销售转化
  20. win7网络中计算机少,Win7网络不稳定如何解决?

热门文章

  1. 2016版系统集成项目管理工程师考试题型举例
  2. 200个c语言程序(由简单到复杂),200个c语言程序(由简单到复杂)
  3. java中socket类_Java中的Socket的用法
  4. python selenium 文件上传_python-selenium -- 文件上传操作
  5. jmeter5实现mysql数据库值提取--单sql提取
  6. (python)查看糗事百科文字 点赞 作者 等级 评论
  7. mvn本地库导入jar包
  8. [Android]BaseExpandableListAdapter实现可折叠列表
  9. 微信第三方平台定时接收component_verify_ticket
  10. war,jar包是啥