I'm using javascript to show my data in a table which shows the columns of Name, Sex, Looks and Age.
For Looks column, I have written a script so that if the gender is male, the column should show the value of Looks as "handsome" and if the gender is female the column should show the value of Looks as "beautiful" in the value.
But it is showing only "handsome" for both male and female gender.
Following is my code:
var myObj = [{name: "Taimoor", sex: "male", age: "Thirty One"},{name:
"Nida", sex: "female", age: "Twenty Nine"}];
var table = "<tr><td>Name</td><td>Sex</td><td>Looks</td><td>Age</td>
</tr>";
function myLooks() {
for (ii in myObj) {
var looks = "";
if (myObj[ii].sex == "male") {looks = "handsome"}
else {looks = "beautiful"};
return looks;
}
}
for (i=0; i <myObj.length; i++) {
table += "<tr><td>" + myObj[i].name + "</td><td>" + myObj[i].sex + "
</td><td>" + myLooks() + "</td><td>" + myObj[i].age + "</td></tr>"
document.getElementById("demo").innerHTML = table;