How to find if stringB contains all of the values of stringA?
Imagining you want to make a new string (stringB
) out of stringA
, when you take a value from stringA, it'll disappear from stringA, you can't use it twice.
So if stringB
has repeated values, but stringA
only has one of that value, then the program should return false
.
Example Input:
stringA = "A B C D"
stringB = "B B C D"
Example Output:
false
Because stringA
only has one "B".
Example Input:
stringA = "apple banana orange mango"
stringB = "banana orange"
Example Output:
true
Here is what I have, but it return true
when it should've returned false
can anyone tell me what is wrong with my logic or what should the solution be? Thanks!
let arrayContainsArray = (a, b) => {
let a_array = a.split(" ")
let b_array = b.split(" ")
for(let i = 0; i < b_array.length; i++) {
if (a_array.includes(b_array[i])) {
let index = a_array.indexOf(b_array[i])
a_array.splice(index, 1)
} else {
return false
}
return true
}
}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
How to find if stringB contains all of the values of stringA?
Imagining you want to make a new string (stringB
) out of stringA
, when you take a value from stringA, it'll disappear from stringA, you can't use it twice.
So if stringB
has repeated values, but stringA
only has one of that value, then the program should return false
.
Example Input:
stringA = "A B C D"
stringB = "B B C D"
Example Output:
false
Because stringA
only has one "B".
Example Input:
stringA = "apple banana orange mango"
stringB = "banana orange"
Example Output:
true
Here is what I have, but it return true
when it should've returned false
can anyone tell me what is wrong with my logic or what should the solution be? Thanks!
let arrayContainsArray = (a, b) => {
let a_array = a.split(" ")
let b_array = b.split(" ")
for(let i = 0; i < b_array.length; i++) {
if (a_array.includes(b_array[i])) {
let index = a_array.indexOf(b_array[i])
a_array.splice(index, 1)
} else {
return false
}
return true
}
}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
Share
Improve this question
edited Jul 10, 2018 at 3:47
Olivia
asked Jul 10, 2018 at 3:16
OliviaOlivia
1954 silver badges14 bronze badges
2
-
Your title doesn't quite match with your question. A string is not an array. What's the point of having an array with a single value? Or are
stringA
andstringB
actually strings and not arrays? – Felix Kling Commented Jul 10, 2018 at 3:27 -
What should be the output for
arrayContainsArray('B C D', 'B B C D')
?true
orfalse
? – Karan Commented Jul 10, 2018 at 4:26
3 Answers
Reset to default 3You can use every
to loop thru the array and check the condition and includes
to check if an array contains a certain element.
let arrayContainsArray = (a, b) => {
let a_array = a.split(" ")
let b_array = b.split(" ")
return a_array.every(o => b_array.includes(o));
}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('two times two is four', 'two times two is four'));
Another option is to make the b_array
into a new Set
. Use has
to check if a set includes a certain element.
let arrayContainsArray = (a, b) => {
let a_array = a.split(" ")
let b_array = b.split(" ")
let set = new Set(b_array);
return a_array.every(o => set.has(o));
}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('two times two is four', 'two times two is four'));
Your code does not work, because it always returns anything (if not false, then true) in first iteration (reason: return true statement is in for loop). Try this:
let arrayContainsArray = (a, b) => {
let a_array = a.split(" ")
let b_array = b.split(" ")
for (let i = 0; i < b_array.length; i++) {
if (a_array.includes(b_array[i])) {
let index = a_array.indexOf(b_array[i])
a_array.splice(index, 1)
} else {
return false
}
}
return true
}
console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('A B C D', 'B B C D'));
console.log(arrayContainsArray('apple banana orange mango', 'banana orange'));
Sharing my approach using jquery each and inArray.
// created function
function existAll(baseArray, arrayToCompare) {
var result = true;
$.each(baseArray, function (ix, data) {
if (!($.inArray(data, arrayToCompare) !== -1)) {
result = false;
return false;
}
});
return result;
}
// usage
alert(existAll([1,2,3,4],[1,2,3,5,4]));