0

I am not sure if this is a scope problem because all seems reasonable to me but somehow my variable keeps on returning empty.

I am trying to refactor these two similar codes and put into function. the codes works perfectly to my needs but when I try to refactor, the variable keeps on returning empty.

Can someone please give me a hand?

This is the two similar codes I have which works fine

var productOutOfStock = "";
$(".availability.outofstock").each(function(){
    var outOfStockMsg = $(this).text();
    var name = $(this).closest(".basket-items").find(".product-name").text();
    if(outOfStockMsg != ""){
        productOutOfStock += name + "<br>";
    }
});

var productOffline = "";
$(".availability.instock").each(function(){
    var outOfStockMsg = $(this).text();
    var name = $(this).closest(".basket-items").find(".product-name").text();
    if(outOfStockMsg != ""){
        productOffline += name + "<br>";
    }
});

This is the refactor I am trying and giving me empty string

var productOutOfStock = "";
var productOffline = "";

function getProductNames(offOrNostock, nameList){
    offOrNostock.each(function(){
        var message = $(this).text();
        var productName = $(this).closest(".basket-items").find(".product-name").text();
        if(message != ""){
            nameList += productName + "<br>";
        }
    });
    console.log(nameList); //this does show the names
}

getProductNames($(".availability.outofstock"), productOutOfStock);
getProductNames($(".availability.instock"), productOffline);
console.log(productOutOfStock); //this is giving me empty string

Thanks in advance for any help.

Dora
  • 6,776
  • 14
  • 51
  • 99
  • Possible duplicate of [Are there pointers in javascript?](http://stackoverflow.com/questions/17382427/are-there-pointers-in-javascript) – Alexander O'Mara Sep 09 '16 at 23:22

1 Answers1

2

JavaScript is a pass-by-value language, so your strings are passed as copies. When the function modifies the nameList variable, it's modifying a copy of the string you pass as the second parameter.

You could modify your function so that it returns the updated string:

function getProductNames(offOrNostock, nameList){
    offOrNostock.each(function(){
        var message = $(this).text();
        var productName = $(this).closest(".basket-items").find(".product-name").text();
        if(message != ""){
            nameList += productName + "<br>";
        }
    });
    return nameList;
}

productOutOfStock = getProductNames($(".availability.outofstock"), productOutOfStock);
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    It's pass-by-value for most things but arrays and objects which are pass-by-reference. Something that has caught me off guard more times than I care to admit. – VLAZ Sep 09 '16 at 23:23
  • 1
    @Vid the term "pass-by-value" is confusing because it's older than the modern concept of objects. Actual pass-by-reference languages (like old Fortran) seem really weird to modern programmers; it's not at all the same thing as the way Java and JavaScript pass object references. Objects in JavaScript are passed by value too, because a "value" that's an object happens to be a pointer to that object, and the language passes the pointer. That sort of "reference" is not what the word means in the term "pass-by-reference", however. – Pointy Sep 09 '16 at 23:26
  • @Dora make sure you do **two** things: add that `return` statement to the function, **and** assign the return value back into the variable. – Pointy Sep 09 '16 at 23:56
  • got it! thx thx, I tried before no luck because I returned the statement but then I didn't assign it to the value again. Getting more understanding how this works. I remember reading about this but never done it before. :D helps a lot.. – Dora Sep 10 '16 at 00:03
  • I know I can just test this out but just want to make sure I am right. Actually the second parameter isn't even needed isn't it? I can just create a new `var nameList` inside the function after all it's returning the `nameList` then assigned to other variable such as `productOutOfStock` – Dora Sep 10 '16 at 00:20
  • @Dora well that's a design decision. It's not bad to make the function involve passing the string in and returning it; it makes it more flexible. – Pointy Sep 10 '16 at 01:45