用于检查数组元素值的测试函数,而不是删除

问题描述 投票:0回答:0

我正在尝试用数组的元素消除所有不好的情况。但不幸的是,我的 JavaScript 代码不能作为一个整体工作:它可以与单独的“IF”选择器一起正常工作,但不能全部一起工作。有人可以向我解释一下这是什么问题吗?预先非常感谢! 这是代码:

/** Check the elements of the array on:
* The array is empty
* Array element/s is/are not a numbers
* Among elements there is not an integers
* Among elements there is a negative number/s
* Among elements there is a too large numbers
*/
const parbaude = (mas) => {
    var response = [true, "... No problem found!"];
    if (mas.length == 0) {
        response = [false, "... Your array is empty!"];
    } else if (mas.filter(item => typeof item !== 'number')) {
        response = [false, "... Only numbers are allowed!"];
    } else if (mas.filter(item => !Number.isInteger(item))) {
        response = [false, "... Enter integer only!"];
    } else if (Math.min(...mas) <= 0) {
        response = [false, "... Only positive numbers are allowed!"];
    } else if (Math.max(...mas) > 1000) {
        response = [false, "... The number is too large: n < 1000!"];
    } else {
        // Return the test result
        return response;
    }
};
//  !!! Try each of these options !!!
//const mas = [];
//const mas = [3, 'd', 7, 9];
//const mas = [3, 4.6, 7, 9];
const mas = [3, -4, 7, 9];
//const mas = [3, 4000, 7, 9];
//const mas = [3, 4, 7, 9];
document.getElementById("izvade").innerHTML = parbaude(mas);
javascript
© www.soinside.com 2019 - 2024. All rights reserved.