/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] HashTable prototype modify statement

I have further modified the script below from some referenced code I found after some help from ‘astupidname’.

Earlier resolved post was: [url]http://www.webdeveloper.com/forum/showthread.php?t=218910[/url]
This current script is just a protion of a test program to make sure all functions perform as expected.

All works well except for one small area associated with the HashTable.prototype.modify().

If you run the following script without the checkbox checked, all works fine.
With the checkbox checked, an additional statement is executed to modify the ‘Gasoline Alley’ entry to change the date display.
It does modify the record correctly with either version of the modify() function,
EXCEPT, the display after the last record shows an ‘undefined’ that is NOT EXPECTED.

Any reason for the ‘undefined’ display after the ‘Daffy Duck’ record display when the earlier record is modified?
?
Obviously, something is different, but I’ll be darned if I can see it.
I’ve highlighted the areas that I suspect are causing the problem,
but I don’t see what to change (assuming I’m looking in the right area).
Does anyone have an explaination or just a better set of eyes to see the problem?

Here’s the test code I’m working with:

[code]
<html>
<head>
<title>HashTable Sample</title>
<script type=”text/javascript”>
// From: http://erik.eae.net/archives/2005/06/06/22.13.54/

function HashTable() { this._hash = {}; }
HashTable.prototype.add = function (key, val) { this._hash[key] = val; };

[COLOR=”Red”]<!– possible problem function –>[/COLOR]
HashTable.prototype.modify = function (key, val) { // problem function ???
this.add(key,val); // alternative to line below
// this.remove(key); this.add(key,val);
} // either 2 lines above act as a value modify statement

HashTable.prototype.append = function (key, val, sep) {
var value = this.getItem(key);
this._hash[key] = value+sep+val;
} // add to current value with ‘sep’ char

HashTable.prototype.remove = function (key) { delete this._hash[key]; };
HashTable.prototype.getItem = function (key) {
if (this._hash.hasOwnProperty(key)) { return this._hash[key]; }
return undefined;
}
HashTable.prototype.containsKey = function (key) { return this._hash.hasOwnProperty(key); };

// —————————————————- start of special needs functions
HashTable.prototype.containsValue = function (val) {
var res = [];
var keys = this.getKeys();
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]).split(‘|’); // uses ‘|’ as seperator for multiple values
// vals.pop();
if (vals.in_array(val)) { res.push(keys[k]+’:’+val); }
}
return res;
}
HashTable.prototype.countValue = function (val) {
var res = [];
var cnt = 0;
var keys = this.getKeys();
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]).split(‘|’); // uses ‘|’ as seperator for multiple values
// vals.pop();
cnt = vals.in_array_Count(val);
if (cnt > 0) { res.push(keys[k]+’:’+val+’:’+cnt); }
}
return res;
}
HashTable.prototype.getKeyValues = function() {
var res = [];
var keys = this.getKeys();
// keys = keys.sort(); // optional – display in sorted order
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]); // uses ‘|’ as seperator for multiple values
res.push(keys[k]+’:’+vals);
}
return res;
}
HashTable.prototype.getKeys = function () {
var res = [];
for (var k in this._hash) { if (this._hash.hasOwnProperty(k)) { res.push(k); } }
return res;
};
HashTable.prototype.getValues = function () {
var res = [];
for (var k in this._hash) { if (this._hash.hasOwnProperty(k)) { res.push(this._hash[k]); } }
return res;
}

// search array for value: returns true or false;
Array.prototype.in_array = function(p_val) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] == p_val) { return true; } // checks for absolute match (not appended values)
}
return false;
}
// search array for value: return count of occurances (0=none, n=…)
Array.prototype.in_array_Count = function(p_val) {
var cntr = 0;
for (var i = 0, l = this.length; i < l; i++) { // checks for absolute match (not appended values)
if(this[i] == p_val) { cntr++; }
}
return cntr;
}
// —————————————————- end of special needs code
</script>

<script type=”text/javascript”>
function testHashTable() {
var str = ”;
myHash = new HashTable();

myHash.add(‘Gasoline Alley’,’08/16/1947′);
myHash.add(‘Betty Boop’,’07/30/1951′);
myHash.add(‘Cache Cow’,’05/29/1970′);
myHash.add(‘Daffy Duck’,’08/03/1971′);

str += ‘nFOLLOWING EFFECTED BY MODIFY RECORD CHECKBOX CHANGE’;
// ==================================================
// unexplained ‘undefined’ in following display ???
str += ‘nnAll Key:Value pairsn’+myHash.getKeyValues().join(‘n’);

// caused by the insertion of either of the following lines (not both)
// seems to effect the output of the previous line when following line(s) are used
/* [COLOR=”Red”]only difference in the display[/COLOR] */
if (document.getElementById(‘cbModify’).checked == true) {
// str += myHash.add(‘Gasoline Alley’,’09/16/1977′); // modify item value
str += myHash.modify(‘Gasoline Alley’,’09/16/1977′); // modify item value (same as add?)
}

str += ‘nnEND OF AREA EFFECTED BY MODIFY RECORD CHECKBOX CHANGE’;
// ==================================================

str += ‘nnBack to normal expected display’;
str += ‘nAll Key:Value pairsn’+myHash.getKeyValues().join(‘n’);
document.getElementById(‘TArea’).value = str;
}

</script>
</head>
<body>
<button onclick=”testHashTable()”>Test HashTable</button>
<input type=”checkbox” id=”cbModify”> Modify record <br>
<textarea id=”TArea” rows=”30″ cols=”60″></textarea>
</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameOct 28.2009 —  str += myHash.modify('Gasoline Alley','09/16/1977');[/QUOTE]
HashTable.prototype.modify does not return a value, so it's return value is undefined, so you are adding undefined on to str above. Same goes for HashTable.prototype.add which does not return a value either.
Copy linkTweet thisAlerts:
@JMRKERauthorOct 28.2009 — Thanks again 'astupidname'

Just when I think I can't get any dumber. :o

I appreciate the extra look! ?
Copy linkTweet thisAlerts:
@astupidnameOct 28.2009 — You're welcome, JMRKER. Remember, it's always the simple things that will trip you up! ?
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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