0

如果碰巧出现我想转换为瑞典语的单词列表

 var translate = {
     "merry":"god", 
     "christmas":"jul", 
     "and":"och", 
     "happy":"gott", 
     "new":"nytt", 
     "year":"år"     
};

// prompt for the text to be translated
var qu = prompt("what would you like to translate?");

// switching it into an array
var result = qu.split(" ");

// empty string to store my answer
var realResult = "";

// for loop to browse through the items in my array
for(i = 0 ; i < result.length ; i++){

/* place where im stuck, whatever I come up doesnt result in proper answer.
I tried to use for(asd in translate) loop to compare every element
but there is something im missing
*/
// if the item in the array is equal to the item in object replace
// that item 
    if( result[i] === translate[????] ){
        realResult += translate.i + " ";

// else put the item from array into the string        
    } else {
        realResult += result[i] + " ";
    }
}
// print out the result
console.log(realResult);

我在这里可能完全错了,答案可能在其他地方。先感谢您

4

1 回答 1

0

只需替换这部分:

 if( result[i] === translate[????] ){
    realResult += translate.i + " ";

// else put the item from array into the string        
} else {
    realResult += result[i] + " ";
}

有了这条线:

realResult += ( translate[result[i]] || result[i] ) + " ";
于 2013-11-08T10:56:00.420 回答