Function and if else statement doesn't work

I am just learning how to code and am currently working on the basics using CodeWars to practice. Since CodeWars allows you to search for solutions, I looked at a few tips that were helpful. I have used this site for guidance, but cannot figure out why my function is not working. It is written in javascript. It only outputs []. Here is the problem, code and output (in the order below):

Problem

Write a method that takes an integer array as a parameter and processes each number from that array. Returns a new array processing each number in the input array as follows: If the number has an integer square root, take that, otherwise the square of the number. [4,3,9,7,2,1] → [2,9,3,49,4,1]

CODE

function squareOrSquareRoot(array) {

    var newValues = []; // new array for new values

    for (i = 0; i < array.length; i++){ // for loop to look through the values

        var initial = array[i]; // extracting one value from the array to evaluate
        var sqrt = Math.sqrt(initial); // finding the square root of initial
        if (Number.isInteger(sqrt) == 'true'){ // determining if sqrt is an integer 
                                            // and if so .....
            newValues.push[sqrt];
        } // .... adding sqrt to the newValues array
        else if (Number.isInteger(sqrt) == 'false') { // determining if sqrt is not 
                                                      // an integer
            newValues.push[initial*initial];  // then multiplying initial by itself 
                                           //and adding to newValues
        }
    }
    return newArray; // returning newValues onto the screen
}

      

OUTPUT

Expected: '[2, 9, 3, 49, 4, 1]', instead got: '[]'

Expected: '[10, 10201, 25, 25, 1, 1]', instead got: '[]'

Expected: '[1, 4, 9, 2, 25, 36]', instead got: '[]'

      

+3


source to share


3 answers


You do a lot of things wrong.

  • You are not returning the correct array. You should return newValues

    instead newArray

    .
  • You shouldn't use quotes for true

    orfalse

  • Use if / else if not required for true / false. Use if / else instead

Here's a working solution:

function squareOrSquareRoot(array) {

    var newValues = []; 
    for(var i = 0 ; i<array.length ;i++) {
        Number.isInteger(Math.sqrt(array[i]))?newValues.push(Math.sqrt(array[i])):newValues.push(array[i]*array[i]);
    }
    return newValues;
}

var a = [3,4,5,9,7,16,36,11];
console.log('Input : ' + a);
console.log('Output: ' + squareOrSquareRoot(a));

      



Input: [3,4,5,9,7,16,36,11]

Output: [9,2,25,3,49,4,6,121]

If this ternary expression confuses you, here is an example with an if / else expression:

function squareOrSquareRoot(array) {

    var newValues = []; 
    for(var i = 0 ; i<array.length ;i++) {
        var initial = array[i];
        var sqrt = Math.sqrt(initial);
        if(Number.isInteger(sqrt)) 
            newValues.push(sqrt);
        else
            newValues.push(array[i]*array[i]);
    }
    return newValues;
}

var a = [3,4,5,9,7,16,36,11];
console.log('Input : ' + a);
console.log('Output: ' + squareOrSquareRoot(a));

      

-2


source


Your condition is wrong. Change

Number.isInteger(sqrt) == 'true'

      

to



Number.isInteger(sqrt) == true

      

Number.isInteger

returns boolean, not string. Also the second, if redundant, if isInteger returns false then just do the else part and don't check again. Finally, you need to return newValues

not newArray

. Hope it helps.

+3


source


Better yet, don't use a for loop, but be array.map

fully applicable.

  function squareOrSquareRoot(array) {
    return array.map(function(int) {
      var sqrt = Math.sqrt(int);
      return Number.isInteger(sqrt) ? sqrt : int * int;
    })
  }

      

array.map

maps each element to a given function and returns a new array at the end.

-2


source







All Articles