Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Abhijit Das
Abhijit Das
5,022 Points

Need Help on a small JS App

Please fix my problem. The goal of the app is if someone type any website name, the respective site's home-link will show as an alert message. If the link isn't include inside the database then another alert message will pop up as "the link is not in our database". my code is here https://codepen.io/Abhijeet_dasdezine/pen/bRbXOW?editors=0010 However, In this app the "the link is not in our database" message is keep generating in spite of the link found, because I keep it outside of the loop, I know my approach isn't perfect but, if i stick with this approach, is there any solution to get rid off the "the link isn't in our database", once the link found?

2 Answers

Steven Parker
Steven Parker
231,236 Points

You could use a boolean to indicate if anything was found.

Something like this:

var foundLink = false;  // assume link was not found
for (let i = 0; i < keyList.length; i++) {
  if (userInput === keyList[i]) {
    alert("The link is " + siteList[userInput]);
    foundLink = true;   // remember we found one
    break;              // no need to check any more
  }
}
if (!foundLink)         // print message only if link was not found
  alert("The " + userInput + " site isn't in our database");
Abhijit Das
Abhijit Das
5,022 Points

wow that's great help Steven Parker. <thankyou>:D</thankyou>