I have a function that searches for a specified number in an array and returns the value that was searched for, the length of the array and the index in which the array was in.
I have 3 questions:
- Is there a way to make a function that calls my console.log's so I don't have to write them every time I want to make a new array?
- How do I get the first value of the array in this case? (The 'first array element' in my console.log)
- Is there a way where I can make my array valued let's say 1 to "number" without typing out all those values in between? Because if I had a range from 1-400, typing all those values are going to hurt my fingers ;(.
Code:
function include(arr, obj) {
for(var i=0; i< arr.length; i++) {
if (arr[i] == obj)
return [i, obj, arr.length];
}
}
var a = include([1,2,3,4,5,6,7,8,9,10,11,12,13,14], 6); // true
console.log("You searched for the value: " + a[1]);
console.log("The length of the array is: " + a[2]);
console.log("The Index of the value(" + a[1] + ") from " + "'first array element'" + " to " + (a[2]) + " is: " + a[0]);
console.log(include([1,2,3,4], 6)); // undefined