challenge - #3 if statements from hackerrank

blossom0417
Sep 4, 2018 · 1 min read

#condition

  1. 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

https://jsfiddle.net/blossom/961fknh7/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade