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