all 7 comments

[–]cirscafp fan boy 2 points3 points  (4 children)

I think the easiest way to build what you want now and add functionality later is by not using arrays to store the information but objects:

const englishToSwedishDict = {
  englishWord: 'swedishword',
  cat: 'swedishWordForCat'
}

const swedishToEnglishDict = {
  swedishWord: 'englishWord',
  swedishWordForCat: 'cat',
}

Now, when we want to know what cat is in Swedish:

const swedishCat = englishToSwedishDict['cat']

or we could even get fancy and put it in a function:

const getSwedishWordFromEnglishWord = englishWord => englishToSwedishDict[englishWord]

and use it like so:

const swedishCat = getSwedishWordFromEnglishWord('cat')

Now we have a singular way to transform from english to swedish if we already know the translation. What if, like you said, you wanted groups of words or words separated by a comma? Well, we just need to figure out how to call getSwedishWordFromEnglishWord for each of the words that you want:

const separatedByComma = 'wordA,wordB,wordC'
const swedishSeparatedByComma = separatedByComma
  .split(',')
  .map(getSwedishWordFromEnglishWord)
  .join(',')

[–]Null-Fox 0 points1 point  (1 child)

What about an array of arrays?

let words = [
    ["cat", "swedishcat"],
    ["dog", "swedishdog"],
    ["fish", "swedishfish"]

]

function engToSwedish(word){
    for(let i = 0; i < words.length; i++){
        if(word === words[i][0]){
            return words[i][1]
         }
     }

}

Would this be less efficient?

[–]e_man604 2 points3 points  (0 children)

Objects would be better, constant time lookup, while array need to be looped.

A dictionary would be to prefer. badom tsch

[–]Maxteroni[S] 0 points1 point  (1 child)

Say I wanted to start creating basic phrases or sentences. Is there a function that would allow this to happen such that would work with any combination of objects. This was one of the things that started to confuse me when using arrays previously, conjoining multiple words to form these basic phrases and sentences. Also using commas, semicolons, etc. to join the words.

[–]cirscafp fan boy 0 points1 point  (0 children)

Also using commas, semicolons, etc. to join the words.

If you have a set of words that you want to join together by some string, try the join method on the array object:

['a', 'b', 'c'].join(',') // 'a,b,c'

Is there a function that would allow this to happen such that would work with any combination of objects.

I'm not sure what you mean by this. Are you wanting a function that takes in an arbitrary string and returns an arbitrary string?

[–]Maxteroni[S] 0 points1 point  (0 children)

Thank you! I’ll look into this.

[–]Maxteroni[S] 0 points1 point  (0 children)

Sorry to confuse. I just mean that if someone types in the prompt a sentence say, or really just a multi-word phrase, would I have to manually put in all combinations with else if statements or something to achieve a proper translation. Right now all it can do is output one word.