I’ve been working on this problem for so long i feel I’ve gone off the deepend and completly corrupted my way of thinking about the problem any help is appreciated thank you !!
instruction:
Create a function that works as follows:
- The function name is ‘doesArrayIncludeItemsBetweenVals’
- given an array of numbers. You can use it as the arr for testing purposes
- The function take an arr (array), val1 (number) and val2 (number) as arguments.
- The function returns a boolean if array includes an item that is greater than val1 and less than val2
- The function MUST have 2 return statements: make an early return if the item is found and use the default return otherwise
- The function MUST be written with NAMED function syntax.
- doesArrayIncludeItemsBetweenVals([2, 4, 2], 3, 5) => true
- doesArrayIncludeItemsBetweenVals([2, 4, 2], 5, 10) => false
my answer so far :
function doesArrayIncludeItemsBetweenVals(arr,val1,val2){
if (val1 < arr.length || val2 > arr.length){
for (let element = 0; element < arr.length; element++){
if ( element > val1 && element < val2){
return true
} else return false
}
}
console.log(doesArrayIncludeItemsBetweenVals([2,4,2],3,5))
}
I tried to make a loop that was in the fuction to be executed so that when you set your parameters to be processed through the function it would output weather there was a value within the array that was also between the to choses values (val1 and val2) so far i’m achieving the exact opposite of what i’m trying to and its still not returning the desired boolean. I cant find an example of how to correctly apply the loop in a function any help is GREATLY appreciated
3
Answers
Or you use the already exiting function find.
You could take a single loop with a comparisons of the two values and return early.