Oranges
Oranges

Reputation: 5

How to return the value of the second array through the input of the first array

I would like to make a multilingual tool in JavaScript. I need to match words with their pair in other language. I would like to use 3 different languages. User inputs names of vegetables, chooses one of several languages and gets the right output.

 var VegetablesInput = ["tomato", "potato", "cucumber", "carrot"]
 var Italian = ["pomodoro", "patata", "cetriolo", "carota"]
 var Croatian = ["rajčica", "krumpir", "krastavac", "mrkva"]
 var Spanish = ["tomate", "patata", "pepino", "zanahoria"]
 var languageInput = prompt ("Choose Italian, Croatian or Spanish.");
 var languageInput = languageInput.toLowerCase();
   function MatchTheWord (language, word) if (languageInput=="italian" && 
 WordInput 
 == "croatian" && WordInput == "spanish") {
 }

I would like to match an element of the VegetablesInput array with the right word according to the language chosen. But I am not sure how to do it simply by looping through the arrays inside the function. Any help would be appreciated.

Upvotes: 0

Views: 109

Answers (1)

Pavel Petrovich
Pavel Petrovich

Reputation: 764

Hope this help:

var Vegetables = {
  it: {
    tomato: "pomodoro",
    potato: "patata",
    cucumber: "cetriolo",
    carrot: "carota",
  },
  cr: {
    tomato: "rajčica",
    potato: "krumpir",
    cucumber: "krastavac",
    carrot: "mrkva",
  },
  sp: {
    tomato: "tomate",
    potato: "patata",
    cucumber: "pepino",
    carrot: "zanahoria",
  },
};
var languageInput = prompt("Choose Italian, Croatian or Spanish.");
var languageInput = languageInput.toLowerCase();
var wordInput = prompt("Choose Word: tomato, potato, cucumber, carrot.");

var translatedWord = Vegetables[languageInput][wordInput]

Upvotes: 1

Related Questions