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)

你可能感兴趣的:(#,Edabit,javascript,算法,前端,开发语言)