0

I am calculating row and column of a given point by javascript function.
To achieve this I have to declare global variables

var global_row;
var global_col;

Here is function body

function calculate_city_row_col(cur_city_id)
{
    var r = 565;
    var c = 1;
    var max_city_id = 159895;
    do{
        if((r%2) == 0) c++;
        max_city_id -= r;
        r--;
    }
    while(cur_city_id <= max_city_id)

    //Saving Result in seperate row and column
    global_row = r + 1;                                 //Global Variable
    global_col = c + (cur_city_id - (max_city_id + 1)); //Global Variable
}

Here is function call

var city_id = 1244;
calculate_city_row_col(city_id);
var city_row = global_row;
var city_col = global_col;

Is there anyway to directly pass local variables? (without using global variables)
like this

calculate_city_row_col(cur_city_id, city_row_ref, city_col_ref);

2 Answers 2

2

Just return an object:

function calculate_city_row_col(cur_city_id)
{
    var r = 565;
    var c = 1;
    var max_city_id = 159895;
    do{
        if((r%2) == 0) c++;
        max_city_id -= r;
        r--;
    }
    while(cur_city_id <= max_city_id)

    return {
        row: r + 1,
        col: c + (cur_city_id - (max_city_id + 1))
    };
}

var rowAndCol = calculate_city_row_col(1244);
var city_row = rowAndCol.row;
var city_col = rowAndCol.col;
Sign up to request clarification or add additional context in comments.

Comments

1

Can we pass variables by reference in JavaScript Function

No. JavaScript is purely pass-by-value (the value passed when you pass an object around is an object reference, but it's still a value).

Your options are:

  1. Have the function return an object (or array)

    function calculate_city_row_col(cur_city_id)
    {
        var r = 565;
        var c = 1;
        var max_city_id = 159895;
        do{
            if((r%2) == 0) c++;
            max_city_id -= r;
            r--;
        }
        while(cur_city_id <= max_city_id)
    
        //Saving Result in seperate row and column
        return {
            row: r + 1,
            col: c + (cur_city_id - (max_city_id + 1))
        };
    }
    

    Usage:

    var result = calculate_city_row_col(someCityId);
    console.log(result.row);
    console.log(result.col);
    
  2. Have the function accept a reference to an object (or array) that it updates

    function calculate_city_row_col(cur_city_id, result)
    {
        var r = 565;
        var c = 1;
        var max_city_id = 159895;
        do{
            if((r%2) == 0) c++;
            max_city_id -= r;
            r--;
        }
        while(cur_city_id <= max_city_id)
    
        //Saving Result in seperate row and column
        result.row = r + 1;
        result.col = c + (cur_city_id - (max_city_id + 1));
    }
    

    Usage:

    var result = {};
    calculate_city_row_col(someCityId);
    console.log(result.row);
    console.log(result.col);
    

Barring a strong reason to go with #2 in a specific situation, #1 is usually the best option. For instance, if you were calling this function thousands of times in a tight loop, there might be a justification for reusing a single object (by passing it in) instead of creating a new object every time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.