Function and if else statement not working

Viewed 360

I am just learning how to code and I am working on fundamentals right now using CodeWars for practice. Since CodeWars allows you to look at solutions, I looked at a few for guidance, which was helpful. I have used this site for guidance but can't figure out why my function isn't working. It is written in javascript. It outputs only []. Here is the problem, code, and the output (in order below):

Problem

Write a method, that will get an integer array as parameter and will process every number from this array. Return a new array with processing every number of the input-array like this: If the number has an integer square root, take this, otherwise square 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 Answers
Related