0

this is my code

var text = input.split(" ");
var wordCount = new Array();
var i;
for(i=0; i<text.length; i++){
    if(wordCount.length==0){
        wordCount.push(text[i], 1);
    }
    var index= wordCount.indexOf(text[i]);

so now I have the index of the key and want to find a value corresponding to this key.

I can write a custom sequential method to get the value but is there a shortcut so that I can just say,

var value = wordCount.getValue(text[i]); and that will return 0 or ant other integer

Also push is not making the format as expected, how shall I push key(my word) and its defalut value in the 2d array?

2

1 Answer 1

1

Store the corresponding index also with value in the array like this

for(i=0; i < text.length; i++){
if(wordCount.length==0){
    wordCount[i] = text[i];
}

so while retriving value you can do

for(var key in wordCount)
{
  alert("key " + key + " has value " + wordCount[key]);
}
Sign up to request clarification or add additional context in comments.

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.