challenge - #3 if statements from hackerrank
1 min readSep 4, 2018
#condition
- N == odd , print out ‘weird’
2. n == even 2≤ n 5≤ , print out ‘not weird’
3. n==even and 6≤ n ≤20 , print ‘weird’
4. n==even and n >20 , print ‘not weird’
first of all, i need to figure out how to get an odd number.
isOdd = number % 2 if isOdd = 0, the given number is even, otherwise the odd The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
once i figured it out, i’ve wrote like below.
i’m a little bit ashamed of mine.
function printNum(n){
var num = Math.abs(n % 2);
if (num == 1){
console.log('weird')
}else if( 2 <= n && n <= 5 ){
console.log('not weird')
}else if (6 <= n && n <= 20){
console.log('weird')
}else{
console.log('not weird')
}
then I found out other’s codes that was simplified .
function isOdd(n) {
return n % 2 == 1;
}function between(low, n, high) {
return low <= n && n <= high;
}function main11() {
var n = parseInt(readLine());console.log(isOdd(n) || between(6, n, 20) ? "Weird" : "Not Weird");
}//orfunction printNum(n){
if ( n%2 == 1 || 6 <= n && n <= 20 ){
console.log('weird')
}else {
console.log('not weird')
}
}
concise code!
it’s hard for me to simplify and optimize the code.
간결한코드…쉽지않다.
홀수 or 6≤ n ≤20, print ‘weird’
#reference
https://www.hackerrank.com/challenges/30-conditional-statements/problem