I have two arrays
first= [1,1,1,1,1,2,2,2,2]
second =[1,1,1,1,1]
The javascript function should return True only when all elements in second array is present in first array and if second =[1,1,1,1] or [1,1,1],[1,1],[1],[] it should return false.
Thanks in advance! Cheers
I tried this but doesn’t work for other instances.
function allElementsPresent(first, second) {
return second.every((element) => first.includes(element));
}
3
Answers
First conditional expression should be checking length
Then, try re-arranging the array in order.
using an ordering algorithm
Then with two loop check if each element of the same index matches each array like below
Hope this helps
I believe I understand what you want to accomplish. You want to see if the number of occurrences in the second array matches the first. If that’s the case, I’ve used this answer as a basis
simple JS-magic, (if I understand the problem):
Sometimes dynamic types really simplify things by treating data generically; use that strength where you can.
If the second array is not allowed to be uniform elements, but could be all non-1 digits, then it’s more complicated:
as a function: