in Education by
I have these cat breeds cats = [ 'Abyssinian', 'Balinese', 'Bombay', 'Birman']; Now the code written is generally something like this for(var i in cats) { ... do } Now I have to modify the same function in javascript. I can only modify to see if i is any of Balinese or Bombay .. then do these I am thinking for(var i in cats) { if(i == 'Balinese' || i == 'Bombay'){ ... do } } Instead I would like something like i contains Balinese,Bombay instead of writing the | clause in middle? for(var i in cats) { if(i.contains('Balinese', 'Bombay'){ //this would be better, suggestions? ... do } } How? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try this code, This adds the .indexOf() in non-supported browsers if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { "use strict"; if (this == null) { throw new TypeError(); } var t = Object(this); var len = t.length >>> 0; if (len === 0) { return -1; } var n = 0; if (arguments.length > 0) { n = Number(arguments[1]); if (n != n) { // shortcut for verifying if it's NaN n = 0; } else if (n != 0 && n != Infinity && n != -Infinity) { n = (n > 0 || -1) * Math.floor(Math.abs(n)); } } if (n >= len) { return -1; } var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); for (; k < len; k++) { if (k in t && t[k] === searchElement) { return k; } } return -1; } } Adding contains method in Array prototype Array.prototype.contains = function(){ for(var i=0,len=arguments.length;i<len;i++){ if(this.indexOf(arguments[i]) !== -1){ return true; } } return false; } Now, you can use like this, var cats = [ 'Abyssinian', 'Balinese', 'Bombay', 'Birman']; if(cats.contains('Balinese')){ console.log('Balinese'); } if(cats.contains('Balinese','Bombay')){ console.log('Balinese,Bombay'); } if(cats.contains('lines','Bombay')){ console.log('lines,Bombay'); } if(cats.contains('lines','mbay')){ console.log('lines,mbay'); } If you are using jQuery, this code can be made simpler :)

Related questions

0 votes
    What are the general description for loop statements and available loop types in C?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    The contacts with the ___________ check are those available to chat with or call in the Skype. Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    please see my code below, example_list = [ ['a','b','c'], ['f','g','h'], ['i','j','k'], ] my_string = ''' ' ... g','h'), ('i','j','k'); Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I want to include a batch file rename functionality in my application. A user can type a destination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    What is the best possible way to check if a string can be represented as a number in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    What is the best possible way to check if a string can be represented as a number in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a method to check how can I represent a string as a number in Python? I am using this ... please help me with it. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    String Slicing means: 1.to convert the string in capital 2.to check whether a string has more than one ... in reverse order computet Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Write a Java program to read two strings from command line arguments and check the equality of two strings using string function. Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Which of these methods of class String is used to check whether a given object starts with a particular ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How will you check in a string that all characters are decimal?...
asked Nov 26, 2020 in Technology by JackTerrance
...