0

I need a function that basically just multiplies members of an array by 100. The number of members of the array may change. Also, I can't use something like array.prototype.map - it has to be a loop.

Here's what I've got so far, but newArray comes back as undefined... What am I doing wrong?

var a = [1, 2, 3];

function toPts(array){
    for(var i=0; i<array.length; i++){
        array[i] *= 100;
    }
}

var newArray = toPts(a);

The end result needed is newArray = [100, 200, 300]

1
  • you need to return the array, now you just modify the existing one. Commented Oct 28, 2014 at 17:39

5 Answers 5

2

You forgot to return the array. If you just want to modify existing one, you only need to do:

toPts(a);
console.log(a) // [100,200,300]

If you want a new array with original untouched:

function toPts(array){
    var arr = array.slice(0); // copy array to new reference
    for(var i=0; i<arr.length; i++){
        arr[i] *= 100;
    }
    return arr;
}
var newArray = toPts(a);
console.log(newArray) // [100,200,300]
console.log(a) // [1,2,3]
Sign up to request clarification or add additional context in comments.

3 Comments

Should you cache the array.length value as well in case this is a huge array?
Not really necessary, since .length is a property and not something calculated when called, so its the same as storing in a variable. Only IE seems to benefit from catching array length
Old wive's tales... you're right. jsperf.com/for-vs-foreach/234 it does seem like doing i-- rather than i++ etc saves a little bit.
0
function toPts(array){
    for(var i=0; i<array.length; i++){
        array[i] *= 100;
    }
    return array;
}

You didn't return the array to the outside.

However, note you are modifying the original array, so it doesn't make much sense to have it in two variables.

Probably, you want to copy it:

function toPts(array){
    var newArray = array.slice();
    for(var i=0; i<newArray.length; i++){
        newArray[i] *= 100;
    }
    return newArray;
}
var a = [1, 2, 3],
    newArray = toPts(a); // `a` is not altered

Comments

0

a is multiplied in-place as its passed by reference, so after the function (which returns nothing)

toPts(a);
alert(a) // 100,200,300

3 Comments

It looks like he wants a new Array not to modify his current one.
@tbh__ The accepted answer is still referencing the same array, so it's not really a "new" array.
0

you are not returning anything from the function, you just need to return the modified array inside the function (after the loop has finished executing)

like

  var a = [1, 2, 3];

  function toPts(array){
   for(var i=0; i<array.length; i++){
    array[i] *= 100;
   }
return array;
 }
 var newArray = toPts(a);

Comments

0

by default function return undefined when it does not return anything.your a array already changed to resulted array.

var a = [1, 2, 3];

function toPts(array){
    for(var i=0; i<array.length; i++){
        array[i] *= 100;
    }
}

console.log(a);//this give you resulted output becuse array is passing as call by reference. so changes made in the same array

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.