1

I want to check if all of the audio file names in unorderedPhrases array exist in the result array that contains the URLs. if there are all exist return true and if-else return false.

Here is what I've tried. I don't know why it returns false all the times!?

let result = [

  "https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to protect you.mp3",

];

const unorderedPhrases = [

    'i was sent',
    'to earth',
    'to protect you'

];

function checkResults(){
	
    return unorderedPhrases.every(r=> result.includes(r));

 
}

console.log(checkResults())

The above code should return true because all the audio files in unorderedPhrases exist in the result array.

if we have this array then it should return false because there is a item in unorderedPhrases that doesn't exist in result :

let result = [
  "https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
];

2 Answers 2

3

If you want to ignore the folder structure and only consider what comes at the very end, you can remove the folder part from each result item first, turning it into another array, then when iterating over the phrases, check if the array without the folder part includes the phrase plus .mp3:

let result = [
  "https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to protect you.mp3",
];

const resultWithoutFolders = result.map(str => str.split('/').pop());

const unorderedPhrases = [
  'i was sent',
  'to earth',
  'to protect you'
];

function checkResults() {
  return unorderedPhrases.every(
    phrase => resultWithoutFolders.includes(phrase + '.mp3')
  );
}

console.log(checkResults())

Sign up to request clarification or add additional context in comments.

15 Comments

for const unorderedPhrases = [ 'i was sent', 'to earth', ]; it returns true!
Both of those exist in one of the result array items, so it returns true. I want to check if all of the audio file names in unorderedPhrases array exist in the result array that contains the URLs If you added a phrase that didn't exist in any of the result strings, like foo, it'd return false
Your example result conflicts with the problem description, can you edit the question to clarify the logic you want?
Ok for let result = [ "https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3", "https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3", ]; it returns true also! and it shouldn't.
Why? All of the phrases are included in one of the substrings there. i was sent is present in the first item, and to earth is also present in the first item
|
0

It returns false because result is a 2D array. That means your result has 3 elements, which are those 3 strings, and each string is an array consisting of characters.

EDIT: Sorry for the mis-assumption. T.J. Crowder has pointed out for me. It's not an 2D array. The strings act like array too. So it can be accessed like a 2D array

This would be a more correct snippet:

let result = [

  "https://example.com/test/unordered/i was sent to earth to protect you_A/i was sent.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to earth.mp3",
  "https://example.com/test/unordered/i was sent to earth to protect you_A/to protect you.mp3",

];

const unorderedPhrases = [

    'i was sent',
    'to earth',
    'to protect you'

];

function checkResults(){
	
    return unorderedPhrases.every(r => result[0].includes(r))

 
}

console.log(checkResults())

If you want to check all the 3 strings in result you have to loop over it. At each time, check the unorderedPhrases like above

4 Comments

"It returns false because result is a 2D array." No, it isn't. It's a 1D array. An array of strings.
My bad for that. Strings are also array. I forgot
Strings aren't arrays.
My trivial experience. It acts like arrays, not arrays. Thanks T.J Crowder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.