Surprising lack of elements in the array

Apologies for the lengthy code, but unfortunately, I was unable to create a minimal example that reproduces the issue. The function below returns two arrays, with each cell containing a number:

function VNtorus(R, r, nx, ny) {
    var Vertices = new Array(nx);
    var Normals = new Array(nx);
    for (var i = 0; i < nx; i++) {
        Vertices[i] = new Array(ny);
        Normals[i] = new Array(ny);
        var u = i / nx * 2 * Math.PI;
        var cos_u = Math.cos(u);
        var sin_u = Math.sin(u);
        var cx = R * cos_u;
        var cy = R * sin_u;
        for (var j = 0; j < ny; j++) {
            var v = j / ny * 2 * Math.PI;
            var rcos_v = r * Math.cos(v);
            var rsin_v = r * Math.sin(v);
            Vertices[i][j] = new THREE.Vector3(
                cx + rcos_v * cos_u,
                cy + rcos_v * sin_u,
                rsin_v
            );
            Normals[i][j] = new THREE.Vector3(
                rcos_v * cos_u,
                rcos_v * sin_u,
                rsin_v
            );
        }
    }
    return {
        vertices : Vertices,
        normals : Normals
    }
}

If interested, the context of this code can be found here: Cannot render a mesh with THREE js

However, when I use the function as shown below, the first array turns out to be empty in the resulting arrays:

var a = 3; var c = 0.7; var mu = 1.7;
var b = Math.sqrt(a * a - c * c);
var bb = b * Math.sqrt(mu * mu - c * c);
var omega = (a * mu + bb) / c;
var Omega = new THREE.Vector3(omega, 0, 0)
var inversion = function (M) {
    var OmegaM = Omega.sub(M);
    var k = OmegaM.dot(OmegaM);
    return Omega.addScaledVector(OmegaM, k);
}
var d = (a - c) * (mu - c) + bb;
var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
var omegaT = omega - (b * b * (omega - c)) /
    ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
var tmesh = VNtorus(10, 3, 64, 32);
var tvertices = tmesh.vertices;
var tnormals = tmesh.normals;
console.log(tvertices)
// ...

0: Array(32)
length: 32
__proto__: Array(0)
1: Array(32)
0: p {x: 0.008700136926854488, y: 0.0008568886953253847, z: 0}
1: p {x: 0.008532966235351477, y: 0.0008404238193190658, z: 0.0017055250840917766}
....

This situation has left me feeling quite perplexed. Being relatively new to Javascript, I fear that I may have made a beginner's mistake somewhere.

Answer №1

I have utilized jquery.mon.js and three.js to create a code snippet for you. Upon clicking the button, the code executes and generates an array with 64 elements.

It is recommended to double-check the three.js CDN to ensure its proper functioning.

(64) [Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32)]

function myFunction(elmnt, clr) {
            var a = 3; var c = 0.7; var mu = 1.7;
            var b = Math.sqrt(a * a - c * c);
            var bb = b * Math.sqrt(mu * mu - c * c);
            var omega = (a * mu + bb) / c;
            var Omega = new THREE.Vector3(omega, 0, 0)
            var inversion = function (M) {
                var OmegaM = Omega.sub(M);
                var k = OmegaM.dot(OmegaM);
                return Omega.addScaledVector(OmegaM, k);
            }
            var d = (a - c) * (mu - c) + bb;
            var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
            var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
            var omegaT = omega - (b * b * (omega - c)) /
                ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
            var tmesh = VNtorus(10, 3, 64, 32);
            var tvertices = tmesh.vertices;
            var tnormals = tmesh.normals;
            console.log(tvertices)
        }

        function VNtorus(R, r, nx, ny) {
            var Vertices = new Array(nx);
            var Normals = new Array(nx);
            for (var i = 0; i < nx; i++) {
                Vertices[i] = new Array(ny);
                Normals[i] = new Array(ny);
                var u = i / nx * 2 * Math.PI;
                var cos_u = Math.cos(u);
                var sin_u = Math.sin(u);
                var cx = R * cos_u;
                var cy = R * sin_u;
                for (var j = 0; j < ny; j++) {
                    var v = j / ny * 2 * Math.PI;
                    var rcos_v = r * Math.cos(v);
                    var rsin_v = r * Math.sin(v);
                    Vertices[i][j] = new THREE.Vector3(
                        cx + rcos_v * cos_u,
                        cy + rcos_v * sin_u,
                        rsin_v
                    );
                    Normals[i][j] = new THREE.Vector3(
                        rcos_v * cos_u,
                        rcos_v * sin_u,
                        rsin_v
                    );
                }
            }
            return {
                vertices: Vertices,
                normals: Normals
            }
        }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="https://threejs.org/build/three.js"></script>
 <button onclick="myFunction()">Click me</button </body> </body> </html>

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

What are the steps to utilizing the $document service in AngularJS?

I am a complete beginner when it comes to AngularJS and JavaScript in general, and I find myself a bit confused about the usage of $document. From what I understand, $document provides access to some JQuery functions. So, if I want to remove an element tha ...

The EJS file is failing to display the stylesheet even though it is being pulled from the

Encountering a strange issue where the page routed to display additional information about a specific record from my database list on the homepage is not loading the stylesheets located in my partial/head, despite successfully passing the object informatio ...

Enhance the standard input control in Vue.js by extending it to include features such as

Incorporating vue.js: Can you enhance a standard HTML input without the need for a wrapper element? I am interested in customizing a textarea like so: Vue.component('custom-textarea', { data () => { return { } }, template: &apo ...

Using require to access an Immediately Invoked Function Expression variable from another file in Node.js

File 1 - Monitor.js var MONITOR = (function () { // Code for Monitoring return { doThing: function() { doThing(); } }; })(); File 2 - Test.js var monitor = require('../public/js/monitor.js'); I am trying to access the doThing() funct ...

How can I retrieve the Sequelize results in the form of a 2D array rather than an array of objects?

I have a situation where I am using the Sequelize query() method like this: const sequelize = new Sequelize(...); ... // IMPORTANT: Cannot modify this query const queryFromUser = "SELECT table1.colname, table2.colname FROM table1 JOIN table2 ON/*...*/ ...

Finding the right property by comparing it with an array of objects in a MongoDB aggregation query

In my mongoDB collection, I have a field called 'abc' that contains an array of objects structured like this: 'abc': [{"_id": new ObjectId("someId"), "name": "entity name"}] I am looking to perfo ...

Uncovering the faces that grace the screen: A guide on finding them

When a user is navigating through a scene in my application, I want to be able to identify the visible faces on the screen (excluding those that are not in the camera's field of view or hidden by other objects). One approach I considered was using th ...

Implementing validation and displaying fields with v-model in vue.js

Looking at this code snippet: <button type="button @click="editing=true">Edit</button> <form v-show="editing" @submit="onSubmit"> <textarea v-model="text"></textarea> </form> <div> Current value: {{text}} </ ...

If the variable is empty, use jQuery ajax to send a request with different data

Below is the code I have written: $(document).ready(function() { cookieanimalid =($.cookie("cookieanimal")); $.ajax({ type: 'POST', url: "/myurl/", data: {cookieanimalid}, success: function ( ...

Error Message: Undefined Service in Angular version 1.5.4

I'm currently developing a sample application using AngularJS 1.5.4, built on angular seed, EcmaScript 6, and with a node.js web server. For routing, I am following the guidelines provided here: https://docs.angularjs.org/guide/component-router. Howe ...

The function could not be invoked due to the absence of a defined $rootScope

I am facing an issue while trying to call the function inside the controller 'Notification' when getNotification is executed in the SelectNotificationCtlr. The error message indicates that $rootScope is undefined just below the line console.log(" ...

Tips for parsing information from a text file and displaying it on a website in table format

Would like to create a 2x4 table where the first column has a dropdown for selection. Upon choosing an option, the associated data will populate the other 3 columns. I'm new to this, so please bear with me. Using x, y, z as placeholders, the data wil ...

The element type provided is not valid: it should be a string (for built-in components) or a class/function. Utilizing SSR with Node.js, React, and React-

Playground: https://codesandbox.io/s/focused-dream-ko68k?file=/server/server.js Issue Error: Encountered an error stating that the element type is invalid. It was expecting a string or a class/function, but received undefined instead. This could be due ...

Reload the Angular application and navigate to a different state

Introduction In my extensive angular application, there are numerous forms for various items. These forms are associated with action objects that have unique schemaforms. The dropdowns in these forms vary based on the specific action and its parent comp ...

Enable lodash access throughout a React Native application

Is it possible to have lodash automatically accessible in all files within a React Native project without needing to import it every time? ...

Error in Next.js 11: Unable to loop over undefined property

Upon upgrading Next.js from version 10 to 11, I encountered an error while running npm run build: Module parse failed: Cannot read property 'forEach' of undefined File was processed with these loaders: * ./node_modules/next/dist/build/babel/loade ...

Iterate through the collection to retrieve the value associated with a specific key

In the JSON response I have received, I am trying to extract the value of "fees" corresponding to "detailComponent" with the identifier "FULL_FEE". However, instead of retrieving the desired value, the loop seems to be fetching the last value associated wi ...

What is the best method for deleting scripts to optimize for mobile responsiveness?

My current plugin.js file houses all my plugins for responsive design, but it is unnecessarily large and cumbersome for mobile devices. I am considering creating two separate plugin.js files to toggle between for mobile and desktop views. What are the r ...

Click event recursion occurs due to the use of $.post

I have a collection of product rows available for customers to select from. Each row is designated with the class "product-line". Upon clicking on a row, I aim to visually indicate its selection status by toggling the "product-checked" class, and subsequen ...

How to send emails with attachments using WordPress?

Looking for assistance in sending emails with attachments using Wordpress. I am struggling and believe there might be something missing in my code. Any help would be greatly appreciated! :) Below is the code spread across two files: Name (required)<b ...