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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Kireeti K
Kireeti K
9,675 Points

Please review my code to Search Student Records [JavaScript]

Please feel free to express your opinion on my code. I am looking to know whether my code is in good standards or not. Any suggestions are most welcome.

 var student, counter;
var students = [ 
  { 
   name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
];    
do {
  var search = prompt("Search for a student record or type 'quit' to exit");
  counter = 0; //Counter increments when record does not match through out the loop and is used to generate "Student record not found message"

    for(i=0; i<students.length; i++){
        student = students[i];   // Storing the index of Students array in variable student
        var studentk = student.name.toLowerCase();
        var searchk = search.toLowerCase();

        if(searchk == studentk){
          document.write( '<h2>Student: ' + student.name + '</h2>');
          document.write ('<p>Track: ' + student.track + '</p>');
          document.write( '<p>Points: ' + student.points + '</p>');
          document.write( '<p>Achievements: ' + student.achievements + '</p>');
          break;
        } else if(searchk == 'quit'){
          alert("Exiting the Search Function");
          break;
        } else if(searchk!= studentk){
          document.write("Searching...   ");
          counter+= 1;
        }  
    } //End of  For Loop

  if(searchk == 'quit'){
    break;
  }else if(counter == students.length){
    alert("Sorry no student record found");
  }
} while(true);    

That looked pretty awesome to me! I'm guessing you came from a different discipline. I noticed you used the == instead of === that Dave and Andrew stress. I found it unsettling at first when I did some php they used == instead of === like there is no Santa clause or more that one lassie or no one has ever really been to the moon. So, maybe the == is no big deal :D

2 Answers

Jeremy Castanza
Jeremy Castanza
12,081 Points

Just wanted to comment on John's comment regarding double equal and triple equal... This is definitely a "gotcha" type of issue. Believe it or not, it does make a difference in some cases, depending upon what type of comparison that you're making. A good example is with strings and numbers.

For example:

var a = 2; // number variable with value 2
var b = 2; // number variable with value 2
var c = "2"; // string variable with value 2

a === b // evaluates to true
a == b // evaluates to true
a === c // evaluates to FALSE
a == c // evaluates to TRUE

Long story short... be careful with this. It does make a difference. For more information, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

Kireeti K
Kireeti K
9,675 Points

Hello Jeremy, Thanks for the reply. What you and John mentioned is correct. I will keep a note about the topic. :)

Jeremy, Thank you for clarifying that.

Kireeti K
Kireeti K
9,675 Points

Hey John, Thanks for the review. I will keep note of the suggestion about "=="

Thanks!