skip to Main Content

I’m trying to wave out all the bad situations with the elements of my array. But unfortunately my JavaScript code doesn’t work as one piece: it works correctly with a separate "IF" selectors but not all together. Could anybody explain me whats the issue? Thanks a lot in advance!
Here is the code:

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

2

Answers


    1. The usage of filter method inside the conditionals doesn’t work as intended. It doesn’t directly return a boolean value. Instead, it returns a new array containing the elements that satisfy the condition. So, the conditions involving filter will always be truthy.
    2. The condition Math.min(…mas) <= 0 will return true if there are negative numbers in the array, not just zeros.
    const parbaude = (mas) => {
        var response = [true, "... No problem found!"];
        
        if (mas.length == 0) {
            response = [false, "... Your array is empty!"];
        }
        
        if (mas.some(item => typeof item !== 'number')) {
            response = [false, "... Only numbers are allowed!"];
        }
        
        if (mas.some(item => !Number.isInteger(item))) {
            response = [false, "... Enter integer only!"];
        }
        
        if (mas.some(item => item <= 0)) {
            response = [false, "... Only positive numbers are allowed!"];
        }
        
        if (Math.max(...mas) > 1000) {
            response = [false, "... The number is too large: n < 1000!"];
        }
        
        return response;
    };
    Login or Signup to reply.
  1. Here is the corrected JS:

    First, the return should not be in the else because it will not be processed if one of the above conditions (if) is met.

    Second, your condition for number and integer. You should check every item in the array if all are met. Move the ! before the statement to indicate the opposite (false).

    Lastly, though you can confirm this one. Your condition for negative is <=0 but 0 is not negative so I removed =. You can keep it though depending on your requirement.

    Here is the working code:

    /** 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.every(item => typeof item === 'number')) {
            response = [false, "... Only numbers are allowed!"];
        } else if (!mas.every(Number.isInteger)) {
            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!"];
        }
        // Return the test result
        return response;
    };
    //  !!! Try each of these options !!!
    let mas;
    mas = [];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    mas = [3, 'd', 7, 9];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    mas = [3, 4.6, 7, 9];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    mas = [3, -4, 7, 9];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    mas = [3, 4000, 7, 9];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    mas = [3, 4, 7, 9];
    document.getElementById("izvade").innerHTML += parbaude(mas) + '<br>';
    <span id="izvade"></span>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search