Create array with numbers +10/-10, where middle one is 0, depending on what length I want

Filippo854

I need to dynamically create array, knowing only how long I want it to be. So for example I need array to be 3 long so I need array = [-10, 0, 10], if I need array 9 long it will be [-40, -30, -20, -10, 0, 10, 20, 30, 40] etc. How can I do it automatically?

Viktor Gusev

Another solution with Array.from:

const createArray = (length) => Array.from({length}, (el, i) => Math.round(i - length / 2) * 10);

console.log(createArray(1))
console.log(createArray(3))
console.log(createArray(9))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

codename one i want to create a list of 10 items with icons and a checkbox if an item is selected

user enters 10 numbers into an array how would I write code to find that number and output what position it is in the array?

I want 10 numbers between RFMIn and RFMax with using linspace in python

I want to know the regular expression of "0_10_00.csv"

I want to make a function to sum numbers and if the numbers have 10 exclude it and if the numbers have 5 subtract it

java - how to create and manipulate a bit array with length of 10 million bits

I want to create an array of non-overlapping random numbers

Using random numbers from a range of -10 to 10, I want to write a program that will change the places of two columns

Why is the first one cannot be executed?? and if i want to use the first one what should i add?? is it sum = (long) sum + n % 10; ? HELP MEEEEE

An array initializer of length '10' is expected

I want to create a loop that creates 10 different strings and use that strings to show 10 different cards

I want an image to change depending on where I choose to go

I want to make sure that users can put numbers only from 1 - 10 in TextFormField Flutter

I want to track the path of particle for ONE HOUR by solving the equation of motion. For time step, t= np. arange(0, 3600,10). Is this correct?

20 random numbers array from 0 to 10. How to count specific numbers in it?

I want to show the array value one by one (0 to i ) in a text view

What can I use if I want't to display a top 10 list of records

I want to create a batch file to do things depending on hibernate status

I want to redirect to another page depending what on url paremeter

I have 10 pages, I am on 4th page, then I want 2,3,4,5,6 as page numbers

Windows 10 I have two recovery partitions, and one is >10GB. What is this and is it ok to delete?

Some values are printing extra times the i (i = 0; i < 10; i++) that i don't want . While using for loop in C

i have a 2D list of any size of all "0" and want to randomly replace 10 of these with a "1"

I want to move win10 storage spaces (configured as raid0 mirror) to new pc

Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array in java

I want to show a Message to Internet Explorer (10 -11) Only and show it one time a day

Algorithm: Given an array A of numbers, create an array B where B[i] = sum(A[j]: A[j] <= A[i])

I want to return a multidimensional array depending on values within nested arrays

Convert small numbers to a scale from 0 to 10