• 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
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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
    }
    
    • 1
    • 2
    • 3
    Examples
    function legalAge(age) {
      if(age >= 18) {
        return true
      }
      else {
        return false
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    次氯酸双光子聚集诱导发光型分子探针/聚集诱导发光环状多烯类分子/氮杂环聚集诱导发光分子的研究
    springboot 点滴(2)springboot AOP
    Java 类集(下)
    [自定义 Vue 组件] 小尾巴 Logo 组件 TailLogo
    联发科MTK(3G,4G,5G)核心板/芯片简介
    Netty面试题(三)
    Linux之进度条
    北斗+5G 织就精确定位的“天罗地网”
    【备忘/shell】hadoop 常见shell 与相关进程操作命令 ing
    JDK中字体的高度信息ascent/descent/leading是怎么计算的
  • 原文地址:https://blog.csdn.net/avenccssddnn/article/details/134090791