vsl0610
vsl0610

Reputation: 59

How to populate an array with integers from a loop in javascript

I just signed up in codewars and I'm trying the first kata. This is the question:

Write a function to multiply a number (x) by a given number (y) a certain number of times (n). The results are to be returned in an array. eg. multiplyBy(2, 4, 6); The output is: [8, 32, 128, 512, 2048, 8192]

I believe my code is write, when I do console.log I get the array that the exercise is asking for, but it won't let me proceed. I'd appreciate some help, thanks!

var array  = [];
function multiplyBy(x, y, n) {
while (x<2049){
var z = x*y*n;
var x = z;
array.push(z)
}
}
multiplyBy(2,2,2);
console.log(array);
return array;

Upvotes: 0

Views: 122

Answers (4)

Oriol
Oriol

Reputation: 288080

Your return is outside the function. And all calls to multiplyBy populate the same array. And your logic is flawed.

Probably, it should be

function multiplyBy(x, y, n) {
  var array  = [];
  while (n--) array.push(x *= y)
  return array;
}
console.log(multiplyBy(2,4,6));

Or, in ECMAScript 6,

var multiplyBy = (x, y, n) => Array(n).fill().map(a => x*=y);

Upvotes: 1

marsh
marsh

Reputation: 1441

Look at this (single-line solution):

function multiplyBy(x,y,n) {
    return Array.apply(null, new Array(n)).map(function(v, i) { return x * Math.pow(y, i + 1); });
}

DEMO

Upvotes: 0

Paul S.
Paul S.

Reputation: 66324

You have a few things going on outside your function which make it only work one time. Make sure that you keep everything inside the function until the last return

function multiplyBy(x, y, n) {
    var array = []; // var this inside your function
    for (; n > 0; --n) { // the loop is only supposed to happen n times
        x = x * y; // you can reuse this variable (x)
        array.push(x);
    }
    return array; // return statement should be inside your function
}

// example of logging the returned value
console.log(multiplyBy(2, 4, 6)); // [8, 32, 128, 512, 2048, 8192]

Your while loop also was hardcoded to x<2049, but this isn't always the case as it depends on the n parameter given to the function


it won't let me proceed

There are 3 issues in the code you posted that probably prevent you from proceeding,

  1. The return array is probably throwing a syntax error because it's outside a function
  2. In your code, calling multiplyBy several times appends the new values onto the end of the previous array
  3. They are probably testing your function against other sets of values to check it works as expected, which is not true in your function, i.e. you gave the example inputs of 2, 4, 6 but used 2, 2, 2 in your own code

As a final note, try to get into the habbit of indenting your code, it'll save you headaches reading it later as it lets you quickly see where blocks begin and end

Upvotes: 2

Vitaliy Terziev
Vitaliy Terziev

Reputation: 6671

ramove the return at the end, this must be used in function to return something, if not used the function will return undefined.

Upvotes: 0

Related Questions