0

I am trying to create an if statement for this code to only allow records with experience level 1-3 to show but not sure how to do it? this is partial code if need more let me know.

var table = document.getElementById('blueteam');

        table.innerHTML = ""; //Clears table
    if(explevel < 4){
        for (var i = 0; i < firstnames.length; i += 1) {
            var row = table.insertRow(0);
            var cell = row.insertCell(0);
            cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
        }
    }
    };
3
  • What is explevel? Where is it defined? Commented Apr 8, 2015 at 3:56
  • I think you need to display more of the source code "Relevant source" I have no idea where explevel is being defined or firstnames. Commented Apr 8, 2015 at 3:57
  • try if(+explevel[i] < 4){ the if should also be in the for loop Commented Apr 8, 2015 at 3:59

3 Answers 3

3

Try the following code:

var table = document.getElementById('blueteam');

    table.innerHTML = ""; //Clears table

    for (var i = 0; i < firstnames.length; i += 1) {
        if(explevel[i] >= 1 && explevel[i] <= 3){
            var row = table.insertRow(0);
            var cell = row.insertCell(0);
            cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
        }
    }
};

You were calling if explevel < 4 but since explevel is a list (explevel[i]), you cannot do that. Instead, check each iteration of explevel.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the logical AND (&&) operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

var table = document.getElementById('blueteam');

table.innerHTML = ""; //Clears table
for (var i = 0; i < firstnames.length; i++) {    
    if((explevel[i] >= 1) && (explevel[i] <= 3)){
        var row = table.insertRow(0);
        var cell = row.insertCell(0);
        cell.innerHTML = firstnames[i] + " " + lastnames[i] + " Exp:" + explevel[i];
    }
}

Comments

1
var table = document.getElementById('blueteam');

    table.innerHTML = ""; //Clears table
    var count = records.length;
    var record;
    for (var i = 0; i < count; i++) {
        record = records[i];
        if (reecord.explevel < 4) {
            var row = table.insertRow(0);
            var cell = row.insertCell(0);
            cell.innerHTML = record.firstname + " " + record.lastname + " Exp:" + record.explevel;   
        }
    }

I modified the data. I am not sure of firstnames, lastnames and explevel. I hope those should be present inside the record.

Sample Data

var records = [{
    "firstname": "",
    "lastname": "",
    "explevel": ""
}];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.