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 trialJose Perez Lopez
9,304 PointsLooking for a matching user in an array of object.
I have been working in this basic website in which in the first page, it requires the user to enter their log in info, I'm having trouble in finding a way of verifying their input. So far this is my code to verify it:
const users = [ {name:'Jose', password: 1234}, {name:'Perez', password: 1235} ];
const userLookUp = (array, user) =>{ for(let i = 0; i < array.length; i++){ let index = array[i]; if(user.toUpperCase() === index.name){ return index; } } };
const passWordLookUp = (looking, found) =>{ if(parseInt(looking) === found.password){ return true; }else{ return false; } }
this works a little, however, every time I put an else after the if in the useLookUp function, the for loops seems to stop looking for users after the first loop, help please. Also, since im returning the index if the user is found, I have no way to return an error message if the user entered the wrong user name.
1 Answer
Steven Parker
231,269 PointsYou wouldn't put an "else" after the test, but you can put something after the loop. If the loop ends, that means nothing was found and you can issue a message there. Or, you could return something like -1 and the code that called it can check the return value and do the message.
Jose Perez Lopez
9,304 PointsJose Perez Lopez
9,304 PointsThank you so much, that solved my problem