Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length will be greater than 4.





Following are the test cases

Test cases Result Status
arrayFront9({1,2,9,3,4}) true OK
arrayFront9({9,2,3,4,56}) true OK
arrayFront9({2,3,4,5}) false OK

Java solution


package test;

public class Program12 {
	public static void main(String[] args) {
		int a[] = {1,2,9,3,4};
		int b[] = {9,2,3,4,56};
		int c[] = {2,3,4,5};
		Program12.arrayFront9(a);
		Program12.arrayFront9(b);
		Program12.arrayFront9(c);
	}
	public static boolean arrayFront9(int[] nums) {
		for (int i = 0; i < 4; i++) {
			if (nums[i] == 9) {
				return true;
			}
		}
		return false;
	}
}

Javascript solution


const arrayFront9 = (array) => {
    const index = array.indexOf(9);
    if (index == -1 || index > 4) {
        return false;
    }
    return true;
}

console.log(arrayFront9([1,2,9,3,4])); //true
console.log(arrayFront9([9,2,3,4,56])); //true
console.log(arrayFront9([2,3,4,5])); //false




About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *