0

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;

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

How about while(--n >= 0)? That for loop looks quite ugly, IMO ;) Anyway, have an upvote
@Johan It took me everything I had not to write while (n-->0)
@vsl0610 it throws SyntaxError: Illegal return statement
Thanks very much! I'm just starting to learn about JS, arrays,etc. Sorry for my clampsyness. 1. It says error, not sure if it's synthax. 2. How can you call the ``multiplyBy´´ only once. 3. This is because mine is not a general function that can be applied to other numbers, correct? I'll try to indent the code next time too!
Thanks Paul, looks like that's the issue. Where could I place it to make it work? It's confusing to me since the console.log shows the correct array
|
1

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);

2 Comments

whit his example he doesnt need the return anyway, this is the only mistake
I have tried putting the return on another place, same result (not working)
0

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

Comments

0

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

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.