hashtable - How do I look up the key in a hash by its value? (JavaScript) -
i have small program below takes array of strings input, say, ["abc","defg","z","hijk"]
. strings taken 1 one , stored in hash, respective lengths recorded next them. eg---> { abc: 3, defg: 4, z: 1, hijk: 4 }
a new array formed, , values of keys created goes it. max number removed twice. third max value 1 have grabbed. have it, how call associated key returned, please?
so example: if strarr
["abc","defg","z","hijk"]
, because "abc" 3
chars, return 3, "abc". how refer "abc" in above hash it's value?
for illustrative example, if strarr
happened ["hello", "world", "before", "all"]
ultimate output should 5 associated world
because "before"
6 letters long, , "hello"
, "world"
both 5
, output should world
because appeared last 5 letter word in array.
var hashobject = {}; var strarr = ["abc","defg","z","hijk"]; function thirdgreatest(strarr) { var count = 0; for(var = 0; i<strarr.length; i++) { var cur = strarr[i]; var curlen = cur.length; hashobject[cur] = curlen; } console.log(hashobject); var newarr = []; for(var x = 0, keys = object.keys(hashobject), xx = keys.length; x < xx; x++) { var val = hashobject[keys[x]]; newarr.push(val); //here form array of key values above } console.log(newarr); for(var j = 0; j < 2; j++) { console.log(newarr); newarr.splice(newarr.indexof(math.max.apply(math,newarr)),1); //here remove max value 2 times } return math.max.apply(math,newarr); //this 3rd largest string's value. //but how use return key associated instead, please? } console.log(thirdgreatest(strarr));
Comments
Post a Comment