functions.js

export const getLocale = (userConfig, knownLocales) => {
	//return userConfig.locale;

	let result;

	for (let i = 0; i < knownLocales.length; i++) {
		if (knownLocales[i] === userConfig.locale) {
			result = userConfig.locale;
		} else {
			result = 'en!';
		}
		return result;
	}
	//return userConfig.locale;
};

Results:

The second output should be "ru" as it should match the knownLocales[1] as it iterates through the loop.

Disregard the comments in the code.

I have been at this for couple of days. Could someone point me in the right direction please?

Thank you

    Gazzooks the main problem is that you return the result too early. The for loop only does 1 iteration.

    Moving the return statement out of the loop is the core change that you need to make, then think about the fact that you need to go over each element of knownLocales before you can return en as default locale.

      Coderslang_Master
      Thank you for replying.

      I moved the "return result" out of the loop. Now each call results in "en!"

      I know the arrays are objects, is there something I need to do to convert the result?

        Gazzooks don't rush it. Start by solving the problem without code.

        Here's an example:

        • analyze every item in knownLocales
        • if some item matches userConfig.locale, then return that locale immediately
        • if you've looped through all of the array elements and haven't been able to return the result yet, then return the default locale

        Then (as soon as everything's clear with the logic) try to implement it in JavaScript.

        Write a Reply...