Exploring Different Sets of Numbers Across Two Columns

In this task, the objective is to compare two sets of ten randomly generated numbers using Javascript to determine whether they are greater than, less than, or equal to each other.

So far, I have the following code:

document.write("Comparing Numbers from Corresponding Lists: <br>");

var list1 = new Array();
for (var i = 0; i < 10; i++) {
    list1[i] = Math.round(Math.random() * 100);
}

var list2 = new Array();
for (var i = 0; i < 10; i++) {
    list2[i] = Math.round(Math.random() * 100);
}

if (list1 > list2) {
    document.write(list1 + " is greater than " + list2);
} else if (list1 < list2) {
    document.write(list1 + " is less than " + list2);
} else if (list1 == list2) {
    document.write(list1 + " is equal to " + list2);
}

Currently, it displays the following output:

Comparing Numbers from Corresponding Lists:
15, 4, 30, 39, 46, 8, 91, 85, 64, 17 is less than 97, 78, 32, 50, 60, 36, 42, 6, 12, 80

However, I would like the output to be in two columns resembling a table with two columns and ten rows. A classmate suggested incorporating the if/else if statement within a for () loop, but I am unsure about what parameters should go inside the loop. For the first two statements, I considered something along these lines:

for (var list1 = 0, list2 = 0; list1.length, list2.length; ...) {

I apologize if this sounds too simple or obvious, but I am struggling to figure it out. Thank you in advance for any assistance.

Answer №1

It seems like you're interested in comparing the individual numbers within an array. One way to do this is by using a simple loop to check each element:

for (var i = 0; i < list1.length; i++) {
    if (list1[i] > list2[i]) { //it's greater! }
    else if (list1[i] < list2[i]) { //it's less! }
    else { //they must be equal }
}

Just a heads up, using document.write will continuously rewrite your page with each execution!

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

Pass the POST parameter in the ajax request

In my current AJAX code, I am attempting to send the dataString as a parameter to a PHP file. I have attempted placing dataString inside xhr.send(dataString);, however, this did not work as expected. Is there another solution available? dataString = txCr ...

Tips for retrieving and transforming the date time picker into the server format (toISOString)

I am attempting to utilize a datetime picker and convert it for server-side transmission, but I'm encountering issues. Here is my attempted code: <html> <head> <meta charset="utf-8"> <meta name="viewport" cont ...

Using the .map method to index an array is causing issues in Node.js

Hey everyone, I'm struggling to properly index a JSON array using the map function. It seems like my code isn't quite right. This is what I have: var entireHTMLssssq = lloopmois.map((result, index) => `<div id=${index} style="position: a ...

What is the process for converting this code to HTML format?

I am new to programming and I am using an API with node.js to display the result in a browser. The API is working fine with console.log, but I want to render it on the browser instead. I am using Jade template for this purpose. How can I write the code t ...

Is there a way to determine when a Telegram web app specifically designed for bots has been shut down?

I have created a Telegram bot that opens a web app, whether through an inline button, keyboard, or link. Once the user completes the tasks in the web app, they can close it using the "close" button instead of any button within the web app that I can track. ...

retrieving session variables from the server side in javascript

I have set a session variable in the backend (code-behind ascx.cs page) and now I need to access that same value in a checkbox checked event using JavaScript. Below is my JavaScript function code: $(document).ready(function () { $('#<%= gvPR ...

How can I trigger an onclick event for a link automatically upon loading in Laravel?

I am presenting the link below: <a href="javascript:void(0)" onclick="xtenderSearchItem.modal_visibility = ! xtenderSearchItem.modal_visibility;">Request</a> My goal is for it to change the language of the page (which occu ...

The array function does not operate as a standard function

While working on my Express, Node.js Server, I decided to enhance an Array with a new function. However, the readiness of this function is not the main concern at the moment. Below is the function I added: File: users.cache.js // This is my Array let Us ...

Integrate a resizable sidebar on the webpage's right side that dynamically adjusts the layout

As I develop a Chrome Extension, my goal is to incorporate a sidebar into all webpages without overlapping the existing content. The sidebar should be placed beside the content, effectively reducing the width of the body of the webpage to the initial width ...

Strategies for Mitigating Duplicate Requests in AngularJs with just one click

I am facing an issue with my AngularJS application where clicking on a link triggers the API call twice. I'm not sure why this happens and how to fix it. My service: SomethingService function getData() { return apiSettings.getApiInformation().t ...

Having trouble with sending a post request through ajax

Whenever I try to initiate an Ajax request upon clicking a button, the request never seems to get executed. Below is the snippet of my HTML code : <!DOCTYPE html> <html lang="en"> <head> <title>User Form</title> ...

Unexpected errors causing havoc in my internet browser

I am facing difficulties uploading large files (~ 2 GB) on my server. To prevent crashes caused by huge files, I have removed the bodyParser from Express. However, the crash error occurs randomly, making it challenging to pinpoint the exact cause. The cod ...

js include exclude switch a category

Although this question has been asked before, I have a query on completely removing an "id" from an element. Let's consider the following scenario: <div id="page"> some content here </div> I want to eliminate the "id" attribute from the ...

Having trouble disabling an HTML select menu using JQuery?

My HTML page includes multiple select menus that all have the shared class name .iconDropDownMenu. During generation with PHP, some of these select menus may be hidden by PHP adding an additional class. I attempted to disable only the hidden .iconDropDown ...

Why are Three.js ply files lacking in color?

I have been experimenting with the Three.js example located in the directory: three.js/examples/webgl_loader_ply.html By replacing their .ply file with my own (created in Blender). In Blender, I used Vertex Paint to paint vertices. Before exporting to . ...

Preventing text highlighting with javascript: A guide for developers

One potential solution involves using CSS, although it is considered a non-standard feature: ::selection { background: transparent; } ::-moz-selection { background: transparent; } Is there a method to achieve the same effect using JavaScript? Ad ...

What is the preferred workflow for client-side modules: (Browserify + npm + gulp) or (RequireJS + Bower + gulp)?

As I delve into various client-side Javascript modules workflows for my current Node.JS Express project, I find myself torn between Browserify + npm + gulp and RequireJS + Bower + gulp. While I lean towards CommonJS due to its syntax, the idea of sharing ...

Personalizing the label of a select input using materialui

Working on customizing a select element using the "styled" method: const StyledSelect = styled(Select)` height: 2rem; color: #fff; border-color: #fff; & .${selectClasses.icon} { color: #fff; } & .${outlinedInputClasses.notchedOutl ...

How do I make the YouTube Grid Gallery player show up in a pop-up window?

I have been experimenting with the following code: <head> <script type="text/javascript" src="http://swfobject.googlecode.com/svn/trunk/swfobject/swfobject.js"></script> <script type="text/javascript"> function loadVideo(playerUrl, ...

The Vue JS Router is prominently displayed in the center of the webpage

I have been delving into Vue JS and working on constructing an app. I've implemented vue router, however, it seems to be causing the content within the routed component to display with excessive margins/padding. I've attempted various solutions s ...