Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,940 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>