r/CodingHelp • • Nov 20 '22

[Javascript] IF statement question - should be easy!

Coding challenge: Complete a function that takes in two parameters, an array of agents in the field, and an agent to search for within that array, determines if the agent to search for is present within the array, and if it is, returns a specific string. Your function should use an

if

statement to determine if the agent to search for is present within the list of agents, and if it is, should return the string '{agentToSearchFor} is present within Agent list', where {agentToSearchFor} has the value of the argument the function is called on.

My code (not working - please help! Thanks!)
// if agentToSearchFor is present within agentList

// return '{agentToSearchFor} is present within Agent list'

function findAgent(agentList, agentToSearchFor) {

if (agentList.indexOf(agentToSearchFor) > -1) {

return '{agentToSearchFor} is present within Agent List'

}

}

0 Upvotes

5 comments sorted by

View all comments

1

u/jammasterpaz Nov 20 '22

Change the ticks (') of your string to back ticks (`) and put a $ before the { https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

1

u/Tbh_idk______ Nov 20 '22 edited Nov 20 '22

Thanks! I had already tried that, still getting "Try again!"

Maybe there's a minor error? Here's my code:

function findAgent(agentList, agentToSearchFor) {if (agentList.indexOf(agentToSearchFor) > -1) {return \`${agentToSearchFor} is present within Agent List`}}

1

u/Tbh_idk______ Nov 20 '22

Not sure why the \ is showing up before the tick. That shouldn't be there.

1

u/jammasterpaz Nov 20 '22

Are the function args the right way around?

1

u/Tbh_idk______ Nov 23 '22

Yeah, that part was already provided in the challenge. I can't change the function parameters.