w3resource

JavaScript: Test whether an array of integers of length 2 does not contain 1 or a 3

JavaScript Basic: Exercise-78 with Solution

Check if Array Does Not Contain 1 or 3

Write a JavaScript program to test whether an array of integers of length 2 does not contain 1 or 3.

Visual Presentation:

JavaScript: Test whether an array of integers of length 2 does not contain 1 or a 3.

Sample Solution:

JavaScript Code:

// Function checks if the array does not contain the values 1 or 3
function is13(nums) {
  // Check if index of 1 is -1 (not found) and index of 3 is -1 (not found)
  if (nums.indexOf(1) == -1 && nums.indexOf(3) == -1) {
    // If both values are not found, return true
    return true;
  } else {
    // If at least one of the values is found, return false
    return false;
  }
}

// Example usage
console.log(is13([7, 8]));  // true, as neither 1 nor 3 is present
console.log(is13([3, 2]));  // false, as 3 is present
console.log(is13([0, 1]));  // false, as 1 is present


Output:

true
false
false

Live Demo:

See the Pen JavaScript - Test whether an array of integers of length 2 does not contain 1 or a 3-basic-ex-78 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Test whether an array of integers of length 2 does not contain 1 or a 3

ES6 Version:

// ES6 version using arrow function
const is13 = (nums) => {
  return nums.indexOf(1) === -1 && nums.indexOf(3) === -1;
};

// Example usage
console.log(is13([7, 8]));
console.log(is13([3, 2]));
console.log(is13([0, 1]));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to test whether an array of integers of length 2 contains 1 or a 3.
Next: JavaScript program to test whether a given array of integers contains 30 and 40 twice. The array length should be 0, 1, or 2.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-78.php