Welcome!



This content originally appeared on DEV Community and was authored by Madalina Pastiu

Instructions:

Your start-up’s BA has told marketing that your website has a large audience in Scandinavia and surrounding countries. Marketing thinks it would be great to welcome visitors to the site in their own language. Luckily you already use an API that detects the user’s location, so this is an easy win.

The Task
Think of a way to store the languages as a database. The languages are listed below so you can copy and paste!
Write a ‘welcome’ function that takes a parameter ‘language’, with a type String, and returns a greeting – if you have it in your database. It should default to English if the language is not in the database, or in the event of an invalid input.
The Database
Please modify this as appropriate for your language.

[ (“english”, “Welcome”)
, (“czech”, “Vitejte”)
, (“danish”, “Velkomst”)
, (“dutch”, “Welkom”)
, (“estonian”, “Tere tulemast”)
, (“finnish”, “Tervetuloa”)
, (“flemish”, “Welgekomen”)
, (“french”, “Bienvenue”)
, (“german”, “Willkommen”)
, (“irish”, “Failte”)
, (“italian”, “Benvenuto”)
, (“latvian”, “Gaidits”)
, (“lithuanian”, “Laukiamas”)
, (“polish”, “Witamy”)
, (“spanish”, “Bienvenido”)
, (“swedish”, “Valkommen”)
, (“welsh”, “Croeso”)
]

Solution:

function greet(language) {
  const languages = [
    ["english", "Welcome"],
    ["czech", "Vitejte"],
    ["danish", "Velkomst"],
    ["dutch", "Welkom"],
    ["estonian", "Tere tulemast"],
    ["finnish", "Tervetuloa"],
    ["flemish", "Welgekomen"],
    ["french", "Bienvenue"],
    ["german", "Willkommen"],
    ["irish", "Failte"],
    ["italian", "Benvenuto"],
    ["latvian", "Gaidits"],
    ["lithuanian", "Laukiamas"],
    ["polish", "Witamy"],
    ["spanish", "Bienvenido"],
    ["swedish", "Valkommen"],
    ["welsh", "Croeso"]
  ];

  for (let i = 0; i < languages.length; i++) {
    if (languages[i][0] === language) {
      return languages[i][1];
    }
  }
  return languages[0][1];
}

Thoughts:

1.I decided to store the input into an array. I loop through the array and if the language was found, will return the greeting in the found language. Otherwise will return ‘Welcome’ as default.

This is a CodeWars Challenge of 8kyu Rank


This content originally appeared on DEV Community and was authored by Madalina Pastiu