Skip to content
Sign upLog in
← Back to Community

Why isn’t my code returning anything? Please help. Thx

Profile icon
lyntechi

function csWhereIsBob(names) {
names.filter((value,index)=>{
if(value === "Bob"){
return index
}
else return -1
})
}
csWhereIsBob(["Jimmy", "Layla", "Anna"])

Voters
Profile icon
lyntechi
Comments
hotnewtop
Profile icon
realTronsi
function csWhereIsBob(names) { return names.indexOf("Bob") } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna"]))
Profile icon
realTronsi

@realTronsi
you basically described the default function indexOf, which does exactly what you want

Profile icon
Coder100

You forgot to console.log the value lol

function csWhereIsBob(names) { return names.findIndex((value, index) => { if(value === "Bob") { return true; } }) } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna"]));
Profile icon
realTronsi

@Coder100
no wtf

Profile icon
realTronsi

@Coder100
btw better version:

function csWhereIsBob(names) { return names.findIndex((value, index) => value==="Bob") } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna", "Bob"]));
Profile icon
Coder100

true

@realTronsi

Profile icon
Coder100

@realTronsi

function csWhereIsBob(names) { return names.findIndex(value => value === "bob"); } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna"]));
Profile icon
realTronsi

@Coder100
yes

Profile icon
realTronsi

array.filter filters out all the elements that satisfy the condition, and will return a new array with the items. If you return within the scope of the array.filter, it won't return anything for the main function.

Instead you need to use array.find

function csWhereIsBob(names) { if(names.find(e=>e==="Bob"))return names.indexOf(names.find(e=>e==="Bob")) return -1 }
Profile icon
realTronsi

@realTronsi

function csWhereIsBob(names) { let exists = names.find(e=>e==="Bob") if(exists)return names.indexOf(exists) return -1 } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna", "Bob"]))

^^^ more concise version

Profile icon
Coder100

how about findIndex

@realTronsi

Profile icon
realTronsi

@Coder100
oh right

function csWhereIsBob(names) { let exists = names.indexOf("Bob") if(exists!=-1)return exists return -1 } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna", "Bob"]))
Profile icon
Coder100

imagine not using findIndex

@realTronsi

Profile icon
realTronsi

@Coder100
OH wtf thats a thing? indexOf() is same thing right

function csWhereIsBob(names) { return names.indexOf("Bob") } console.log(csWhereIsBob(["Jimmy", "Layla", "Anna", "Bob"]))
Profile icon
realTronsi

@Coder100
indexOf is the same thing I believe, why is findIndex a thing

Profile icon
Coder100

no, indexof doesn't work for objects

@realTronsi

Profile icon
realTronsi

@Coder100
he only wants the name though which are strings

Profile icon
Coder100

well we don't know what his assignment says

@realTronsi

Profile icon
realTronsi

@Coder100
also indexOf does work for objects

Profile icon
Coder100

give example

@realTronsi

Profile icon
Coder100

@realTronsi

image

unproved

Profile icon
realTronsi

@Coder100
no you cant directly do that, you need to reference the object though

object = new Foo() objects.push(object) console.log(objects.indexOf(object)) // 0
Profile icon
realTronsi

@Coder100
I don't think you know how js nor objects work

Profile icon
Coder100

@realTronsi

image

unproved

Profile icon
realTronsi

@Coder100
no you didnt unprove, read my post again smh

Profile icon
realTronsi

@Coder100
read my example again

Profile icon
RYANTADIPARTHI

So apparently first of all to return anything, you have to print the function out.

document.write(csWhereIsBob(['jimmy', etc]), and you will see undefined, because index is undefined, and has nothing. Try naming it something, then it will work.

Profile icon
realTronsi

@RYANTADIPARTHI
wtf? read your answers before you post it?

Profile icon
RYANTADIPARTHI

@realTronsi
please don't use profanity. Thanks!

Profile icon
realTronsi

@RYANTADIPARTHI
well you need to either thoughtfully answer questions or stop giving clearly low effort answers and then begging for cycles

Profile icon
lyntechi

I’m trying to return the index of Bob, if a string exist of “Bob” if it’s not there I want to return -1. I’m just not sure why it won’t return anything.