Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Inappropriate case returned of else statement in javascript

I used the code provided as an answer found on the question Here . very useful for me. But I am stucked in something really strange.

I use ajax to get some values from database and then save them in a dictionary etc. then with this code I check the values if are equal with the ones coming from database and change the tables cell color respectively.Everything seems fine except from " The first and always the first record that comes out from the database is not changing to the correct color, no matter how many records are there, The color should be #000 and not #ffff99". Both values that I am comparing are 12:00.

That seems to be pretty crazy because If I remove the else statement from the code. Everything works fine. But I need the else statement to do some other important stuff and by the way I don't think that not using the else statement is a good practice xD.

Below is my code.

    for(const [key, value] of Object.entries(data))
    {
     var table = document.getElementById('table'),rows = table.rows, rowcount = rows.length, r,cells, cellcount, c, cell;
          for( r=0; r<rowcount; r++)
          {
             cells = rows[r].cells;
             cellcount = cells.length;
             for( c=0; c<cellcount; c++)
             {
                 cell = cells[c];
                 var text = (cell.textContent || cell.innerText);
                 if( text == key.substring(0,5))
                 {
                   cell.style.backgroundColor="#000";
                   break; 
                 }
                 else
                 {
                    cell.style.backgroundColor="#ffff99";
                 }
                                          
              }
           }
     }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!