/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] undefined check

Hi, in my code, I want to check “undefined”. But it failed.
Can you please take a look?
arg is a string array which will be passed into the function createTable.

[CODE]function createTable(arg) {
// get the reference for the body
var body = document.getElementsByTagName(“body”)[0];

// creates a <table> element and a <tbody> element
var tbl = document.createElement(“table”);
var tblBody = document.createElement(“tbody”);
var rowCount = Math.round (arg.length / 8)+1;
// creating all cells
for (var j = 0; j < rowCount; j++) {
// creates a table row
var row = document.createElement(“tr”);

for (var i = 0; i < 8; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement(“td”);
var textFromDatabase = arg[i + 8 * j];
var cellText = document.createTextNode(textFromDatabase);
if (cellText.data == “undefined”) {
cellText = “”;
cell.appendChild(cellText);
}
else {
cell.appendChild(cellText);
}
row.appendChild(cell);
}

// add the row to the end of the table body
tblBody.appendChild(row);
}

// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute(“border”, “2”);
}[/CODE]

Thanks

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@PadonakAug 05.2010 — may be this will work?

[CODE]if(typeof(textFromDatabase) == 'undefined')[/CODE]
Copy linkTweet thisAlerts:
@zhshqzycauthorAug 05.2010 — No, type mismatch error.
Copy linkTweet thisAlerts:
@FangAug 06.2010 — var cellText = document.createTextNode(textFromDatabase);
if (cellText.data.length == 0) {
Copy linkTweet thisAlerts:
@Declan1991Aug 06.2010 — I would use Fang's solution, but undefined is not a string. The test would have been (although it doesn't work I think) this.if(typeof(textFromDatabase) == undefined)
Copy linkTweet thisAlerts:
@FangAug 06.2010 — I assumethe [I]textFromDatabase[/I] will return a string. It could never be [I]undefined[/I] as createTextNode would then cause a script error and fail.
Copy linkTweet thisAlerts:
@zhshqzycauthorAug 06.2010 — [CODE]var cellText = document.createTextNode(textFromDatabase);
if (cellText.data.length == 0) {[/CODE]

[CODE]if(typeof(textFromDatabase) == "undefined")[/CODE]
Both code above are not working.
Copy linkTweet thisAlerts:
@FangAug 06.2010 — What is the value of [I]textFromDatabase[/I]? It must be a string or createTextNode will fail.
Copy linkTweet thisAlerts:
@zhshqzycauthorAug 06.2010 — [CODE]var textFromDatabase = arg[i + 8 * j];
[/CODE]

This only happened when i+8*j > arg.length.

Which means that the the index is beyond the size of the array.

I don't know what is the value of textFromDtabase in this situtation.
Copy linkTweet thisAlerts:
@FangAug 06.2010 — It must be a string or createTextNode will fail[/QUOTE]var textFromDatabase = (i+8*j &gt; arg.length)? "" : arg[i + 8 * j];
×

Success!

Help @zhshqzyc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...