-2

The challenge is:

  • If n is odd, print "Weird".
  • If n is even and in the inclusive range of 2 to 5, print "Not Weird".
  • If n is even and in the inclusive range of 6 to 20, print "Weird".
  • If n is even and greater than 20, print "Not Weird".

And My Code is:

<?php
$N=5;

if($N%2==0 && $N>20){
    echo "Not Weird";
}else{
    echo "Weird";
}
?> 

And the problem is:

When I rut it locally it's okay. But when I submit it to HackerRank then it fails their test when comes to $N=5; case. Do I have any problems in my conditional according to the challenge?

jotik
  • 17,044
  • 13
  • 58
  • 123
Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72

2 Answers2

1
<?php
$n=5;
if($n%2==1){
    echo 'Weird';
}else{
    if($n >= 2 && $n <= 5){
        echo 'Not Weird';
    }elseif($n > 5 && $n <= 20){
        echo 'Weird';
    }else{
        echo 'Not Weird';
    }
}
?> 
Niroj Adhikary
  • 1,775
  • 18
  • 30
1

I'm surprised that it works for you locally. It shouldn't. Your "Not weird" condition only works for even numbers which are greater then 20. You didn't include any condition that would cover unique cases (Weird for 6, 8, 10 etc).

One working solution would be:

<?php
if ($N%2 == 0 && ($N < 6 || $N > 20)) {
    echo "Not Weird";
} else {
    echo "Weird";
}

First condition displays Not Weird only when number is even, and is lower then 6, or greater then 20. For all other numbers - odd, and even from range 6 - 20 it displays Weird.

Deus777
  • 1,816
  • 1
  • 20
  • 18