1

I'm looking for a way to check any text string (boolean) if a word is present using an array. Currently, I'm using include() method for each keyword i'm after, thus creating a lot of inefficiencies. I figured using a for loop will help streamline the process but I'm running into a snag, by where my return value is multi-line.

I can't seem to figure out why I can't convert the iteration result to a text string. I've tried .Replace(/\r?\n|\r/), toString() and join(), but have no luck.

function searchForKeywords() {
    let textString = 'This is a sample text string that contains a bunch of words, such as Semi and Metal. Happy searching!'
    let keywords = ['Clear', 'Metal', 'Silver', 'Semi']

    for (let i = 0; i < keywords.length; i++) {
        let a = (textString.includes(keywords[i]));
        let result = a.toString()
        console.log(result);
    }
}

searchForKeywords();

The result i'm getting from the above code is as follows:
false
true
false
true

The desired result will be: false, true, false, true

Just to clarify, when I replace toString() with join() and .Replace(/\r?\n|\r/), I get

TypeError: a.join is not a function || TypeError: a.join is not a function

I'm not sure why I get the error and as a noob and learning all this without someone to ask in real-time is quite frustrating. I'm sure a lot of people go through this... so please be gentle with me :-)

Any help is greatly appreciated. Thank you in advance.

2
  • please add the wanted result. Commented Nov 29, 2018 at 19:44
  • sorry about that... updated it in my original post. Commented Nov 29, 2018 at 19:46

2 Answers 2

3

You could map the results of the check and join the values to a string with a spacer.

function searchForKeywords() {
    let textString = 'This is a sample text string that contains a bunch of words, such as Semi and Metal. Happy searching!',
        keywords = ['Clear', 'Metal', 'Silver', 'Semi']

    return keywords
        .map(function (k) { return textString.includes(k); })
        .join(', ');
}

console.log(searchForKeywords());

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

2 Comments

I'm so sorry, but unfortunately i'm using Nashorn and it doesn't have arrow function capability so it doesn' work on my end :-( This is the result i get: java.lang.AsertioError: Failed generating bytecode for <eval> :5. Is there a way you can re-write the code using nashorn capable language? Again, I'm a noob at this and not really sure what the heck is going on. SMH. Thank you
I have to change .includes to .contains for it to work! Thank you. I'm gonna have to understand what you did there. Thank you again.
1

Instead of outputting each iteration, push the values into a result array, and then at the end you can format that array with join:

let textString = 'This is a sample text string that contains a bunch of words, such as Semi and Metal. Happy searching!'
let keywords = ['Clear', 'Metal', 'Silver', 'Semi']

let result = [];
for (let i = 0; i < keywords.length; i++) {
    let a = textString.includes(keywords[i]);
    result.push(a)
}
console.log(result.join(", "));

2 Comments

What you provided also works! Thank you so much. Can I give a check mark to your answer too?
That's too bad because you deserve it too! Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.