1

I'm a complete newbie to programming and mathematical concepts (took Math 101 in college) so I'm struggling with this problem:

Write a function that takes in a number, and returns an array with that number in it that many times.

Here's the code I've got so far:

    function numReturn(x) {
         var newArray = [];
         if (typeof x === "number") {
             return newArray.push[x]* x;
         } else {
             return null;
         }
     }

Here's my thought process:

  1. Create a function that can take in a number, x.
  2. Within that function, create a blank array so you can push values to it later.
  3. Check if the typeof value entered in for x is a number. If it is, return it pushed to the end of the blank array. Otherwise, return null.

When I put this in the Javascript console and plug a value in, it comes back undefined. Anyone have some pointers for me?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
zasman
  • 446
  • 1
  • 8
  • 28

5 Answers5

3
function a(i) {
    var a = new Array(i);
    return a.fill(i);
}

or return new Array(i).fill(i);, for short. Test:

a(4)
// --> [4, 4, 4, 4]

Array.prototype.fill() is an ES6 method and is not yet universally implemented. Chrome and Firefox have it, IE does not - but there is a polyfill available.

Compare: http://kangax.github.io/compat-table/es6/#test-Array.prototype_methods_Array.prototype.fill

Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

In order to do something an arbitrary number of times, one uses a loop. There are several loops, but here the for loop is most appropriate.

A for loop has the following structure:

for(var i = 0; i < x; i++) {
    // ^initializer
    //         ^condition
    //                ^increment
    //body
}

The initializer is the first thing that is done when entering the loop. IN this case, it means a variable named i is set to 0. Then the condition x is checked. If the condition i < x holds, the loop is executed: the body is executed. After the body is executed, the increment is performed (here i++), and then the condition is rechecked, if the condition still holds, the loop is executed again, and so on.

You can apply this concept as follows:

function numReturn(x) {
    var newArray = [];
    if (typeof x === "number") {
        for(var i = 0; i < x; i++) {
            newArray.push(x);
        }
        return newArray;
    } else {
        return null;
    }
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thank you sir for the explanation. I now know that the loop works by checking by pushing x "in a the array the number between x and 0, which is x. – zasman Jan 07 '16 at 23:18
1

This: newArray.push[x]* x does not push x times. The * operator just multiplies numbers, always and only. You want to push x times, so use a for like this:

for (var i = 0; i < x; i++ )
      newArray.push(x);

and then return newArray.

Cecilio Pardo
  • 1,717
  • 10
  • 13
1

Taking from answer of Most efficient way to create a zero filled JavaScript array?

function numReturn(x){
    return Array.apply(null, Array(x)).map(Number.prototype.valueOf,x);
}
console.log(numReturn(10)); // [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Community
  • 1
  • 1
n-dru
  • 9,285
  • 2
  • 29
  • 42
0

var n = 7
var result = Array.apply(null, {length: n}).map(function(){return n})

// Demo output
document.write(JSON.stringify(result))

As function:

function numReturn(x) {
     if (typeof x === "number") {
         return Array.apply(null, {length: n}).map(function(){return n})
     } else {
         return null;
     }
 }
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • 1
    Spectacular one-liner. I don't think the OP will learn much from it though. – John Weisz Jan 07 '16 at 12:47
  • 1
    @JohnWhite: my idea exactly. One here introduces JSON, higher order functions, all kinds of library functions, document,... This is simply too much to understand at once. It's like giving a typewriter to a monkey and expecting it to start typing Shakespear. (this comment is not meant as an offense to anybody). – Willem Van Onsem Jan 07 '16 at 12:52
  • @WillemVanOnsem I want just to open his eyes so he can see that there is more ways to do it. The loop answer is already posted anyways. – CoderPi Jan 07 '16 at 12:53
  • Also the `.fill` is better for this, I learned something new! – CoderPi Jan 07 '16 at 12:54