1

I'd like to set #currentWord span to the hovered over Chinese word's English translation.

To do that, I'm storing words based on their category in an object:

var vocab = {
"pronouns" :
{
    'wǒ'    : 'I',
    'nǐ'    : 'You',
},
"nouns" : 
{
    'gǒu'   : 'dog'
},
"verbs" : 
{
    'bāng'  : 'help',
    'xún'   : 'search',
}

};

I then call parseHoveredText to send the word to the currentWord span:

$("#currentWord").text(parseHoveredText(word));

parseHoveredText() Function:

function parseHoveredText (word) {
  for (obj in vocab) {
      log(vocab[obj][word]);
      return vocab[obj][word];
  }
  return "";
};

When I hover over "Ni", it correctly outputs the English "You"

enter image description here

Console output is also correct: You


However: Hovering over bang (in the "verbs" object) outputs nothing:

enter image description here

Console output shows: Undefined

The weird thing is: If I comment out //return vocab[obj][word];, the console outputs the correct associated English, "Help", but still doesn't display it to the span...

Why is that?

Thanks!

1
  • 1
    @Sergio it's a different question... Commented Aug 30, 2013 at 19:08

3 Answers 3

4

You are iterating over the whole vocab object and returning in the first iteration without even checking whether the word is in the inner object.

Hovering over "Ni" works "by chance" since it seems that the property in the first iteration is nouns. But since "bang" is not in vocab.nouns, you get undefined.

You have to check whether the word is contained in each category and return the translation when you found it.

for (obj in vocab) {
    if (word in vocab[obj]) {
        return vocab[obj][word];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Quick question, since my incoming dialog text might be capital () or lower case (), and that my vocab object only has lower case , I wanted to make sure all cases were checked... So I'm doing if (word in vocab[obj] || word.toLowerCase() in vocab[obj]) { but it's not working... Why?
I cannot say, it seems to work for me: jsfiddle.net/CbKzh. Maybe you want to try word.toLocaleLowerCase() instead.
Hmm this also did not work... log(word + ", " + word.toLowerCase() + ", " + vocab[obj][word]); outputs Nǐ, nǐ, undefined
1

You need to check whether the element exists in the current part of speech before returning:

function parseHoveredText (word) {
  for (obj in vocab) {
    if (vocab[obj][word]) {
      log(vocab[obj][word]);
      return vocab[obj][word];
    }
  }
  return "";
};

Comments

0

You need to add "if" statement. You code loop through vocab obj but on first step call return and out. If you comment return function will loop through all vocab but will return ""

function parseHoveredText (word) {
  for (obj in vocab) {
     var result = log(vocab[obj][word]);
     if(result)
     {
        log(result);
        return result;
     }
  }
 return "";
};

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.