Generating a JavaScript array populated with floating point values ranging from one to ten, inclusive of decimals

I need an array that includes the range [1,1.1,1.2 ...... 9.9,10]

I have this code that gives me values from 1.1 to 9.9

  1. Is there a more concise way to achieve this range in JavaScript? The current code seems lengthy.
  2. How can I also include the numbers 1 and 10 in the array?
let ratingRange = Array(10).fill().map((v,i)=> {
    return Array(10).fill().map((v,decimalI)=> {
        let value = parseFloat(`${i}.${decimalI}`)
       return value > 1 ? value : false 
    })
}).flat().filter( v => v > 0.9  );
// [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9]

Answer №1

You have the ability to accomplish this task.

Give it a shot!

const numbers = [];

for(let i=10; i<=100; i++) {
  numbers.push(i);
}

const dividedNumbers = numbers.map(num => num/10);
console.log(dividedNumbers);

Answer №2

And here is the solution.

let startVal = 1.0, numbers = [];
while (startVal < 9.9) {
  startVal = Math.round((startVal + 0.1) * 10) / 10
  numbers.push(startVal);
}
console.log(numbers);

Answer №3

Here is the solution for you:

let numbers = [];

for (let j = 1.1; j < 10; j += 0.1)
  numbers.push(j);

console.log(numbers)

Answer №4

If you ever need to create a multidimensional array in JavaScript, consider using the Array.from method.

Unlike using new Array, which returns a jagged array that is not easily looped through, Array.from can fill in the gaps and also accept a mapper function as an argument to create complex arrays.

const result = Array.from(
  { length: 9 },
  (_, i) => Array.from(
    {length: 10},
    (__, index) => (i + 1) + index * 0.1
  )
).flat()

console.log(result)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on creating an HTML5 rectangle for reuse using the Prototypal Pattern

I'm struggling to grasp Prototypal Inheritance through the use of the Prototypal pattern by creating a rectangle object and an instance of that rectangle. It seems like it should be straightforward, but I'm having trouble understanding why the Re ...

How to show table cell value in Angular 4 using condition-based logic

I am a beginner in Angular development. Here is the HTML code I am working with: <tr *ngFor="let item of calendarTableSelected; let idx = index"> <span *ngIf="idx === 0"> <td style="width:15%;" *ngFor="let name of item.results" ...

Facing a dilemma: Javascript not updating HTML image source

I am facing an issue while attempting to modify the source of my HTML image element. Despite using document.getElementId('image') in my code, I am unable to make it work as intended. Surprisingly, there are no errors displayed. Interestingly, whe ...

methods for transferring information from a website to a smartphone using SMS

I am currently in the early stages of learning JavaScript and working on a project that involves creating a form (a web page) to send data to my mobile device via SMS when the submit button is clicked. However, I am unsure how to transfer data from JavaS ...

My goal is to use both jQuery and PHP to automatically populate the dropdown list

Here is my PHP code for executing a query: $host = "localhost"; $user = "root"; $pass = "abc123"; $databaseName = "class"; $con = mysql_connect($host,$user,$pass); $dbs = mysql_select_db($databaseName, $con); $result = mysql_qu ...

The groupings of D3 chart values do not reflect the loaded JSON data

I have encountered an issue with the grouping (Countries) in my JSON query while using a D3 chart to present the data. The current implementation is causing the data to display more than one line for some countries (e.g. Canada). Below is a snippet of the ...

Conceal the .dropdown-backdrop from bootstrap using solely CSS styling techniques

Is there a way to hide the .dropdown-backdrop element from Bootstrap for a specific dropdown on a webpage using only CSS? I found a solution that involves Javascript, you can view it on JSFiddle here. However, I am hoping to achieve this without relying o ...

Tools for parsing command strings in NodeJS

Currently, I'm utilizing SailsJS for my application. Users will input commands through the front-end using NodeWebkit, which are then sent to the server via sockets. Once received, these commands are parsed in the back-end and a specific service/cont ...

Clicking on a button within the parent element will enable you to remove the parent element multiple times with the use of VanillaJS

Within a ul element, each li contains multiple elements in the following structure: <ul> <li> <div> <p>some text </p> <button>delete</button> <div> </li> <li> ...

Retrieving JSON Array Information Using PHP Objects

stdClass Object ( [id] => 8586332 [email] => <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f373a3333301f32263a323e3633713c3032">[email protected]</a> [optInType] => Unknown [emailType] => Html [ ...

Adjust the Angular menu-bar directly from the content-script of a Chrome Extension

The project I've been working on involves creating an extension specifically for Google Chrome to enhance my school's online learning platform. This website, which is not managed by the school itself, utilizes Angular for its front-end design. W ...

This issue with ng-include not functioning properly in Chrome but working fine in Firefox

My code is running only in Firefox and not working in Chrome. HTML Code <div ng-controller="myController1"> Select View: <select ng-model="employeeview"> <option value="1.html">1</option> < ...

Step-by-step guide on implementing a real-time data array counter in React js

I am attempting to create a function that continuously generates new data arrays with an increasing counter. Each second, a new array should be added with the next number in sequence. data={[ { x: 1, y: 1 }, { x: 2, y: 2 }, ...

Is there a way to print messages to the console of openDevTools in Electron JS?

After finishing a hello world application using electron js, I have successfully printed to my terminal with console.log and opened the openDevTools in the window of my application. However, I am now interested in finding a way for my console.log stateme ...

Is there a way to convince Python to interpret data as a multi-dimensional list instead of a string when converting from HTML?

Currently, I am working on formatting table data in HTML and then sending it to Python using the script below: var html_table_data = ""; var bRowStarted = true; var count1 = 1 $('#woTable tbody>tr').each(function () { if (count1 != 1) { ...

Difficulty with NodeJS, Hapi, and Cascading Style Sheets

I've decided to challenge myself by creating a small webhost using Node.js with hapi. Even though I'm not an expert in Node.js, I am eager to learn as much as possible. Currently, my main issue revolves around fetching a CSS file from an include ...

What is the best way to retrieve the initial cell from a row that the user is currently hovering over?

Is there a way to extract the first cell from a row when a user hovers over it and then clicks a specific key combination? (considering using jQuery) I have a table set up similarly where placing the mouse over a tr and pressing ctrl+c will copy the ID t ...

Fixing script type error when using web components with Angular on Nginx

Currently facing an issue while trying to serve my Angular 8 app with a basic Nginx server. Upon attempting to run the app, I encountered an error in Chrome that states: Failed to load module script: The server responded with a non-JavaScript MIME type ...

Icons are failing to display in the dropdown menu of MUI after an option is selected in ReactJS

There seems to be an issue with the icon in the select tag. Initially, it is not showing which is correct. However, when you click the dropdown, the icon appears as expected. But after selecting an option, if you click the dropdown again, the icon disapp ...

Can we organize a list based on the size of each element, specifically integers?

Assuming there is a list with elements [5,7,83,1], and you want to transform it to be stored as [2,3,4,1], preferably in a single loop for improved performance. ...