繁体   English   中英

函数和if else语句不起作用

[英]Function and if else statement not working

我正在学习如何编码,我现在正在使用CodeWars进行实践的基础知识。 由于CodeWars允许您查看解决方案,因此我查看了一些指导,这很有帮助。 我已经使用这个网站作为指导,但无法弄清楚为什么我的功能不起作用。 它是用javascript编写的。 它只输出[]。 这是问题,代码和输出(按顺序如下):

问题

编写一个方法,将一个整数数组作为参数,并处理该数组中的每个数字。 返回一个新数组,处理每个输入数组的数字,如下所示:如果数字有一个整数平方根,请取此,否则将数字平方。 [4,3,9,7,2,1] - > [2,9,3,49,4,1]

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: '[]'

你的病情有缺陷。 更改

Number.isInteger(sqrt) == 'true'

Number.isInteger(sqrt) == true

Number.isInteger返回一个布尔值而不是一个字符串。 另外第二个if是多余的,如果isInteger返回false则只执行else部分而不是再次检查。 最后你需要返回newValues而不是newArray 希望能帮助到你。

更好的是不使用for循环,而array.map完全适用。

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

Array.map将每个项映射到给定的函数,并在最后返回一个新数组。

你犯了很多错误。

  1. 你没有返回正确的数组。 您应该返回newValues而不是newArray
  2. 您不应该使用引号来表示truefalse
  3. 使用if / else if不是true / false。 请改用if / else

这是工作解决方案:

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));

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

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

如果这个三元表达式让你困惑,这里是if / else语句的例子:

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM