1

I am new to javascript and taking some tests on codewars, I am writing a code that checks for perfect numbers i.e. if a number has a squareroot that is a whole number it should return true. Here is my code and it only works for 0 and 1, from here it returns false even for numbers that are perfect squares. I need to understand why my code is not working and i cannot see where my problem is.

var isSquare = function(n){
      for ( var i=0; i>=0; i++){

      var product= i*i;
      if( product === n ) 
      return true;

      else if(product !==n)
      return false;

      }
    }
cherucole
  • 560
  • 7
  • 15

2 Answers2

3

Your return false is running every time the input n is not equal to the tested product. That is, on the first iteration, it will return false if n is not 0. You should probably only return false if n is smaller than the tested product, and leave out the iteration condition because the test is being done in the loop body:

var isSquare = function(n) {
  for (var i = 0;; i++) {
    var product = i * i;
    if (product === n) return true;
    else if (product > n) return false;
  }
}
console.log(isSquare(9));
console.log(isSquare(10));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • you don't need else if you can move it outside loop since if it is not returning true it will return false – Basil Battikhi Aug 06 '18 at 09:32
  • I don't think so, because otherwise, what would determine when the loop terminates? I know *usually* you can do that, but this isn't one of those cases unless one wanted to re-write much of the code – CertainPerformance Aug 06 '18 at 09:33
  • i forgot that you didn't put an expression, if you did then it will terminate when the loop is end – Basil Battikhi Aug 06 '18 at 09:34
  • Thank you so much for pointing out where my issue is, your code worked and i have understood where my problem is;) – cherucole Aug 06 '18 at 09:49
1

Your problem is with else if since you don't need else if, move the return outside the loop, it will keep checking until your loop get false, and you need also to rewrite your boolean expression

 var isSquare = function(n){
          for ( var i=0; i<n; i++){
    
          var product= i*i;
          if( product === n ) 
          return true;
          }
         return false;
        }
        console.log(isSquare(9))
        console.log(isSquare(4))
        console.log(isSquare(12))
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
  • Thank you so much, your code worked and i have understood where my problem is;) – cherucole Aug 06 '18 at 09:47
  • Remeber to vote up accept answer if it is really help you – Basil Battikhi Aug 06 '18 at 09:50
  • I would have loved to but my reputation is less than 15 so I cannot upvote answers at the moment. I will when I get to that level though – cherucole Aug 06 '18 at 10:53
  • actually @CertainPerformance's answer has more performance, since my loop stop until i reach n while his loop will stop until i*i reach n so my loop will stop after 1000 iteration while his loop will stop after 10 iterations – Basil Battikhi Aug 09 '18 at 09:57