0

Is there a JavaScript Version of the Python expression in the form of

nums = [0,0] + [1] * (n)

?

n is the number of times an item is repeated. Examples: If n is 3, nums produces [0,0,1,1,1], If n is 4, nums produces [0,0,1,1,1,1] and if n is 5, nums produces [0,0,1,1,1,1,1] and so on.

  • 5
    What does that expression produce in Python? Without knowing this, it's kind of hard to give an equivalent operation in JS. – VLAZ Dec 03 '20 at 14:30
  • So, `[1] * (3)` produces `[1,1,1]` and then `[0,0] + [1,1,1]` makes `[0,0,1,1,1]`, correct? – VLAZ Dec 04 '20 at 16:36
  • Yes it does make [0,0,1,1,1]. – derektypist Dec 04 '20 at 16:37
  • OK, just checking if I understood it correctly. – VLAZ Dec 04 '20 at 16:38
  • Thank you - expression in the form of `nums = [0,0] + [1] * (n) ` produces `[0,0]` plus the number of times `[1] `is repeated. For example, to get `[0,0,1,1,1,1,1]` use the expression `nums = [0,0] + [1] * (5)`. To get `[0,0,1,1,1,1]`, use the expression `nums = [0,0] + [1] * (4)`. Change the variables according to your wish later, for example, `items` instead of `nums`. – derektypist Dec 04 '20 at 17:02
  • 1
    Can you edit the question to add that information there, please? I've voted to reopen but ideally the question should contain all information, instead of having to look at the comments. – VLAZ Dec 04 '20 at 17:03
  • 1
    Your question basically consists of two questions. [How to concatenate arrays](https://stackoverflow.com/questions/3975170/javascript-how-to-join-combine-two-arrays-to-concatenate-into-one-array) and [how to create an array of repeated elements](https://stackoverflow.com/questions/12503146/create-an-array-with-same-element-repeated-multiple-times). Put together you get `[0,0].concat(Array(3).fill(1))`. – Ivar Dec 04 '20 at 17:07

2 Answers2

1

In JavaScript there aren't operators defined that work on arrays, however, you can imitate their results:

Operations

List concatenation (+)

This is very simple as there is the Array#concat() method

a = [1, 2]
b = [3]

c = a + b

in JavaScript is equal to

const a = [1, 2];
const b = [3];

const c = a.concat(b);

console.log(c);

You could define a function that concatenates any amount of elements as:

const add = (...items) => [].concat(...items);

console.log("[1, 2] + [3]:", add([1, 2], [3]));
console.log("[1] + [2] + 3:", add([1], [2], 3));
console.log("[1, 2] + 3:", add([1, 2], 3));
console.log("1 + 2 + 3:", add(1, 2, 3));

List Repetition (*)

There is no native way to repeat an array in JavaScript. You can apply a loop and repeatedly call add, however an easier way might be to use Array.from. The first argument it takes is an array-like which can be used to define the size of the create {length: neededSize} and the second argument it takes is a function that will determine the contents of this array.

This is a simple implementation:

const mul = (arr, times) => 
  Array.from({length: times*arr.length}, (_,i) => arr[i%arr.length]);

console.log("[] * 3:", mul([], 3));
console.log("[1] * 3:", mul([1], 3));
console.log("[1, 2] * 3:", mul([1, 2], 3));
console.log("[1, 2, 3] * 3:", mul([1, 2, 3], 3));

Module that handles operations fluently

For the sake of it, here is a small module that makes the operations into a fluent interface and eagerly evaluates each operations:

const add = (...items) => 
  [].concat(...items);
const mul = (arr, times) => 
  Array.from({length: times*arr.length}, (_,i) => arr[i%arr.length]);

const arrayMath = arr => ({
  add: (...items) => arrayMath(add(arr, ...items)),
  mul: num => arrayMath(mul(arr, num)),
  result: () => arr
})

const res1 = arrayMath([])
    .add([1, 2])
    .add([3])
    .mul(2)
  .result();

const res2 = arrayMath([])
    .add(1, 2)
    .add([3])
    .mul(2)
  .result();

const res3 = arrayMath([])
    .add(1, [2])
    .add([3])
    .mul(2)
  .result();


const res4 = arrayMath([1])
    .mul(2)
    .add(2)
    .mul(3)
  .result();


console.log(res1);
console.log(res2);
console.log(res3);
console.log(res4);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
0

Since when you simply multiply a number to an array, it will parse all the contents in array to integer to perform the operation, but while parsing it also considers the comma that separates the elements in the array, so ultimately the parse gives you a NaN(Not a Number).

So you have to do it manually by a for loop(or any loop):

var arr = [0,0];

 function myFunction() {
  let x;

  for(x=0;x<3;x++){
  arr.push(1)
 }

Change the variables according to your wish later.