Convert parameter of a function automatically to string

0

I would like to create a function that has as parameters the name of an array and any string. The goal is that just by entering the names of both things, the function logue the index of the item, if it exists within the array (no need to use quotes when specifying the item when I call the function) . Here I leave the code of what more or less I wanted to achieve

function findId (array, item) {
    let stringedItem = String(item)
    let index = array.indexOf(stringedItem)
    console.log(index)
}

const array1 = ['objeto1', 'objeto2', 'objeto3'];
findId(array1, objeto3)

pd. I am new with the programming, I say it in case this doubt is very basic or something like that haha. Thanks to whoever can answer me:) ... Oh, and I found string() in an online blog, I honestly do not know exactly what he does, I only understand that he can convert everything to a string

    
asked by PrayTheCookie 15.09.2018 в 15:32
source

1 answer

0
function findId (array, item) {
    let stringedItem = String(item)
    let index = array.indexOf(stringedItem)
    console.log(index)
}

const array1 = ['objeto1', 'objeto2', 'objeto3'];
var word="objeto3";
findId(array1, word);

The only possible solution that occurs to me is to store the word in a variable so you avoid using quotes in the parameters. in my opinion it does not seem to be efficient

    
answered by 15.09.2018 / 15:55
source