What are your opinions on my JavaScript function? Can my code below be refactored?var updatePrice = function () {    var goPrice;    var returnPrice;    if (typeof scope.selectedGo !== 'undefined' && typeof scope.selectedReturn !== 'undefined') {        for (var go in scope.gooffers) {            if (scope.gooffers[go].tarifPremiere !== null && typeof scope.gooffers[go].tarifPremiere !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifPremiere.id) {                goPrice = scope.gooffers[go].tarifPremiere.tarif;            } else if (scope.gooffers[go].tarifSecondeFlex !== null && typeof scope.gooffers[go].tarifSecondeFlex !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifSecondeFlex.id) {                goPrice = scope.gooffers[go].tarifSecondeFlex.tarif;            } else if (scope.gooffers[go].tarifSecondeNoFlex !== null && typeof scope.gooffers[go].tarifSecondeNoFlex !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifSecondeNoFlex.id) {                goPrice = scope.gooffers[go].tarifSecondeNoFlex.tarif;            }            if (scope.returnoffers[go].tarifPremiere !== null && typeof scope.returnoffers[go].tarifPremiere !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifPremiere.id) {                returnPrice = scope.returnoffers[go].tarifPremiere.tarif;            } else if (scope.returnoffers[go].tarifSecondeFlex !== null && typeof scope.returnoffers[go].tarifSecondeFlex !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifSecondeFlex.id) {                returnPrice = scope.returnoffers[go].tarifSecondeFlex.tarif;            } else if (scope.returnoffers[go].tarifSecondeNoFlex !== null && typeof scope.returnoffers[go].tarifSecondeNoFlex !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifSecondeNoFlex.id) {                returnPrice = scope.returnoffers[go].tarifSecondeNoFlex.tarif;            }        }       if (goPrice !== null && typeof goPrice !== 'undefined' && returnPrice !== null && typeof returnPrice !== 'undefined') {                    scope.finalPrice = goPrice + returnPrice;        }                }};
Selecting the correct price
javascript
Variable naming is very important for readability.Now, as some background, when I first saw your question, it had the title:What are your opinions on my javascript function?Titles like that are an poor choice for code review, and as a moderator, we actively change titles like that to reflect what the code does, and not what the question asker sees as being the problems in the code.Unfortunately, when I read your code, to try to see what it does, all I could think of was goofers... it took me a few moments to understand that the code was meant to be go offers.camelCase to the rescue...gooffers should be goOffersscope.returnoffers should be scope.returnOffersApart from those two, all your other variables are correcly cased. The following are good:tarifSecondeFlextarifPremierescope.selectedGogoPriceetc.Note, I believe you have mixed some natural language variable names in there. Is that French? In general, when programming, you should pick a language, and stick to it. You have a mix. Since you have goPrice and selectedGo which appear to be English, I would expect tarifPremiere to be primaryPrice, etc.Language consistency goes a long way toward readability.DRY codeRepetitive code is often an indication that function-extraction would be useful.Consider a function like:function selectedTarif(component, selection) {    if (component == null || typeof component == 'undefined' || component.id !== selection) {        return null;    }    return component.tarif;}Then, your code can become a cascading 'falsy' test:var updatedPrice = selectedTarif(scope.goOffers[go].tarifPremiere, scope.selectedGo)            || selectedTarif(scope.goOffers[go].tarifSecondeFlex, scope.selectedGo)            || selectedTarif(scope.goOffers[go].tarifSecondeNoFlex, scope.selectedGo);if (updatedPrice) {    goPrice = updatedPrice;}var updatedReturn = selectedTarif(scope.returnOffers[go].tarifPremiere, scope.selectedReturn)            || selectedTarif(scope.returnOffers[go].tarifSecondeFlex, scope.selectedReturn)            || selectedTarif(scope.returnOffers[go].tarifSecondeNoFlex, scope.selectedReturn);if (updatedReturn) {    returnPrice = updatedReturn;}