Create a matrix by utilizing the x and y axes to form a multi-dimensional array

var axis_x = document.getElementById("x_axis").value; // 3
var axis_y = document.getElementById("y_axis").value; // 2

for (var i=1; i <= axis_x; i++){
    axs_x.push(i); // [1,2,3]
    alert(axs_x);
}
for(var j=1 ; j <= axis_y; j++){
    axs_y.push(j); // [1,2]
    alert(axs_y);
}
}

I am seeking assistance in resolving this problem. The desired outcome is:

[11, 12, 21, 22, 31, 32]

Answer №1

It seems like you're looking for something along these lines

var x_axis = document.getElementById("x_axis").value;//3
var y_axis = document.getElementById("y_axis").value;//2

for (var i=1; i <= x_axis; i++){
    for(var j=1 ; j <= y_axis; j++){
        alert(x_axis);
        alert(y_axis);
    }
}

Answer №2

If you're looking to generate pairs of x and y axes based on the values within 2 div elements, give this method a try:

function getAxisValues(id) {
  var element = document.getElementById(id);
  
  if (element) {    
    return parseInt(element.innerHTML, 10);
  }
  
  return null;
}

function generateAxes(x, y) {
  var pair = [];  
  
  for (var i = 1; i <= x; i++) {
    for (var j = 1; j <= y; j++) {
      pair.push(i * 10 + j)
    }    
  }
  
  return pair;
}

var axisX = getAxisValues('x_axis');
var axisY = getAxisValues('y_axis');

alert(generateAxes(axisX, axisY)); // [11, 12, 21, 22, 31, 32]
console.log(generateAxes(axisX, axisY)); // [11, 12, 21, 22, 31, 32]
<div id="x_axis">3</div>
<div id="y_axis">2</div>

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

Having trouble bypassing custom points on the reactive gauge speedometer

My current project involves utilizing the npm package react-d3-speedometer to create a custom points-based gauge. The issue I am facing is that while the package works properly with values from 0 to 1000 when passed to the customSegmentValues property, it ...

Ways to eliminate the unusual dashed space in ReactJS using Bootstrap

While developing an app with NextJS and Bootstrap, I noticed a strange dashed gap at the top of the screen in the elements tab. Despite checking for margin or padding-top properties on any element, there doesn't seem to be a clear cause for this issue ...

Can you please highlight parts of the text and provide dialogue with additional information?

I am currently enhancing my blog and looking for a way to implement JavaScript functionality that will help highlight specific parts of my text and provide additional information, like: I am currently working on my laptop When the user clicks on "lapto ...

Trouble with Bootstrap 5 Dropdown Functionality

Currently, I am using Bootstrap 5 in conjunction with Django to create a website and I'm encountering difficulties with getting a dropdown feature to work properly. Despite copying the code directly from w3schools, it fails to function when I load the ...

Tips on dividing a div into two separate pages when converting to PDF

I am working on an asp.net MVC project and I need to convert a HTML div into a PDF with two separate pages. Here is an example of my code: HTML Code <div class="row" id="mycanvas"> <p>This is the first part of my content.</p> <p ...

Learn the process of transmitting data from middleware to components and APIs in Next.js version 13

I've been experimenting with the Next Js 13 middleware feature and I'm a bit confused about how to pass data from the middleware to components/pages/api. For example, when trying to pass payload data or determine who the currently logged-in user ...

Improper ordering using insert method within a forEach loop

I have an array containing objects that I need to import into a sqlite3 database using a forEach loop. The process is working correctly, but I noticed that the objects are not being imported in the same order as they appear in the database. This is my app ...

Tips for automating the activation of intents at specific scheduled times in Dialogflow

I'm attempting to automatically trigger intents in Dialogflow to obtain the user's contact details at a scheduled time. Is it possible to achieve this using JavaScript? If so, could you please provide the code? ...

JavaScript Evaluation Error

try { eval(somejavascript); } catch(e) { console.log(e); } When I encounter runtime errors like: TypeError: Cannot call method 'leftPad' of undefined I wonder if there is any way to debug this error. Specifically, I'm looking for ...

PHP: Transform a multidimensional array into a unique structure

Hello everyone, I'm facing a challenge with the array I have: $aryMembers = array( array ( 'ID1', 'Martin Mars', 'England' ), array ( 'ID2', 'Marie Arteau', 'Fran ...

Discovering Duplicates in PHP - A Step-by-Step Guide

Currently, I am working with PHP and would like to showcase a list of records retrieved from the database. Below is an example of the table structure: sno val1 val2 val3 amt 1 2c 3e I 500 2 2b i7 I 1500 3 2w ...

Leverage environment variables in React JS to define distinct API URLs for production and development environments

For my React app, I started with create-react-app as the boilerplate. To make a request to my local server, I'm using fetch. fetch("http://localhost:3000/users") .then(function(res){ return res.json() }) .then(function(res){ return res.data }) ...

Creating a sticky header for a MatTable in Angular with horizontal scrolling

Having an issue with merging Sticky Column and horizontal scrolling. Check out this example (it's in Angular 8 but I'm using Angular 10). Link to Example The base example has sticky headers, so when you scroll the entire page, the headers stay ...

bespoke validation using AngularJS

Consider a scenario where there is a table comprising of 10 rows and each row contains 10 columns of checkboxes. Prior to the user submitting the form, it is necessary to implement a validation rule: at least two checkboxes must be checked in each row! & ...

Creating a multi-dimensional array in VB.NET or PHP: A step-by-step guide

I have a set of data organized for comments, complete with levels and a sort column. Now I am looking to transform this information into an array or find a way to present it in a more useful format: id comment Parentid Level sortcol 2 ...

JavaScript can be utilized to manage the exit events

Possible Duplicate: Alert when browser window closed accidentally Is there a way to trigger a new window to open when a user clicks the exit button (X) on the browser, similar to how the Wordpress Admin Panel functions? In the Wordpress panel, when t ...

Working with Node.js and MySQL can involve using callbacks within nested queries

I am trying to add data to my database only if it doesn't already exist. While attempting this, I encountered an error message: { [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false } My ...

What could be causing my conditional element to flicker during the loading of the route?

Situation: I am working on a webpage that dynamically displays different div elements based on the response received from an Axios request. If the response does not populate the data array called myExpense[], a div with the alert-warning class is shown. Ot ...

How to retrieve a refined selection of items

In my scenario, there are two important objects - dropdownOptions and totalItem https://i.sstatic.net/VreXd.png The requirement is as follows: If 12 < totalItems < 24, then display "Show 12, Show 24" If 24 < totalItems < 36, only show "Show ...

Javascript Using Promise to Await Ajax Content Loading

As a newcomer to JavaScript, I am exploring ways to traverse a DOM that utilizes Checkboxes for filtering results and displays them through Ajax. The checkboxes are organized in 4 levels: Grand Parent Parent Child Grand Child My goal is ...