当前位置: 首页 > article >正文

0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean

0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean

conditions language_fundamentals logic validation

Instructions

In this series we’re going to see common redundancies and superfluities that make our code unnecessarily complicated and less readable, and we’re going to learn how to avoid them.

In line with the spirit of the series, we can summarize the general rules of minimalist code in two simple principles:

  • Keep your code clean and readable.
  • While not violating the first principle: get rid of everything superfluous.

In order to achieve this you should:

  • Deepen your knowledge of logics.
  • Deepen your understanding of the particular language you’re coding with.

I would also add: observe and learn from the pros. Make a habit of checking the Solutions tab after solving a challenge on Edabit. There is absolutely nothing wrong in assimilating features of someone else’s coding style, especially if yours is not yet fully developed.

Goal

In the Code tab you will find a code that is missing a single character in order to pass the tests. However, YOUR GOAL is to submit a function as minimalist as possible. Use the tips in the Tips section down below.

Write a function that returns true if the given integer is even, and false if it’s odd.

Tips

Using an if statement in order to return boolean or to set a variable to a boolean is redundant.

A function that returns true if a person’s age is 18 or greater and false otherwise, could be written as:

function legalAge(age) {
  if(age >= 18) {
      return true
  }
  else {
      return false
  }
}

Notice that age >= 18 will already give us a boolean (true or false). This means that the function can be written in a much simpler and cleaner way:

function legalAge(age) {
 return age >= 18
}
Examples
function legalAge(age) {
  if(age >= 18) {
    return true
  }
  else {
    return false
  }
}
Notes
  • his is an open series: there isn’t a definite list of features for the challenges.
Solutions
// issue code
function isEven(n) {
	if n % 2 === 0 {
		return true
	}
	else if n % 2 === 1 {
		return false
	}
}
// correct it !!
function isEven(n) {
    return (n % 2)==0
}
TestCases
let Test = (function(){
    return {
        assertEquals:function(actual,expected){
            if(actual !== expected){
                let errorMsg = `actual is ${actual},${expected} is expected`;
                throw new Error(errorMsg);
            }
        },
        assertSimilar:function(actual,expected){
            if(actual.length != expected.length){
                throw new Error(`length is not equals, ${actual},${expected}`);
            }
            for(let a of actual){
                if(!expected.includes(a)){
                    throw new Error(`missing ${a}`);
                }
            }
        }
    }
})();
Test.assertEquals(isEven(2), true)
Test.assertEquals(isEven(3), false)
Test.assertEquals(isEven(10), true)
Test.assertEquals(isEven(31), false)
Test.assertEquals(isEven(666), true)
Test.assertEquals(isEven(777), false)
Test.assertEquals(isEven(3482034), true)
Test.assertEquals(isEven(3482035), false)

http://www.kler.cn/news/107401.html

相关文章:

  • RK3588开发笔记-USB3.0接口调试
  • VMware打开共享虚拟机后找不到/mnt/hgfs/文件夹,以及不能拖拽/复制粘贴等操作,ubuntu不能安装VMware tools
  • 3台Centos7快速部署Kafka集群
  • 如何在Node.js中使用环境变量或命令行参数来设置HTTP爬虫ip?
  • 【Proteus仿真】【Arduino单片机】PWM电机调速
  • Mysql的JDBC知识点
  • 【C++的OpenCV】第十四课-OpenCV基础强化(二):访问单通道Mat中的值
  • 轻量级仿 Spring Boot=嵌入式 Tomcat+Spring MVC
  • Qt下实现支持多线程的单例模式
  • Redis进军磁盘存储
  • Spring常见面试题
  • 大数据采集技术与预处理学习一:大数据概念、数据预处理、网络数据采集
  • 一文5000字从0到1使用Jmeter实现轻量级的接口自动化测试(图文并茂)
  • 167. 两数之和 II - 输入有序数组、Leetcode的Python实现
  • 有一个带头结点的单链表L,设计一个算法使其元素递增有序
  • pytorch 入门 (五)案例三:乳腺癌识别识别-VGG16实现
  • Unity的live2dgalgame多语言可配置剧情框架
  • 10月份程序员书单推荐
  • vscode下ssh免密登录linux服务器
  • PostgreSQL基于Patroni方案的高可用启动流程分析
  • Centos使用war文件部署jenkins
  • [量化投资-学习笔记003]Python+TDengine从零开始搭建量化分析平台-Grafana画K线图
  • 【2023.10.25练习】数据库-函数1
  • 【CSS】包含块
  • 【2024秋招】2023-9-16 贝壳后端开发一面
  • 【Java网络原理】 五
  • canvas基础3 -- 交互
  • 【网络安全 --- 任意文件下载漏洞(1)】任意文件下载漏洞
  • [SQL开发笔记]DELETE 语句:删除表中的行
  • C++ 构造函数