What could be causing this issue in the JavaScript code?

Can anyone explain why this code isn't functioning correctly? Although it prints "generating fish," nothing is being printed after that.

function fish(x, y, degree, genes, Snumber) {
            this.x = x;
            this.y = y;
            this.dgree = degree;
            this.energy = 50;
            this.genes = genes;
            this.Snumber = Snumber;
        }

        var fishs = new Array(10);
        var Snumber = 0;
        document.writeln("Generating fish");
        for (i = 0; i < 10; i++) {
            var x = Math.round(Math.random * 600);
            var y = Math.round(Math.random * 600);
            var degree = Math.round(Math.random * 360);
            var genes = new Array(12);
            for (j = 0; j < 12; j++) {
                genes[j] = Math.random * 2 - 1;
            }
            fishs[i] = new fish(x, y, degree, genes, Snumber);
            Snumber++;
            document.writeln("Generating fish number " + i);
        }

Answer №1

Your code contains a few errors and warnings that need to be addressed:

1.) Instead of using the var keyword, you are placing variables in the global scope automatically.

2.) In this line of code, you are using the + operator instead of the = operator:

genes + new Array(12);

3.) You are mistakenly utilizing Math.random which returns the random function, not a random number, in three different instances.

4.) The use of document.write(ln) is deprecated. Consider using console.log instead, which prints output to the console (press F12 to view it).

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

The Owl Carousel npm package is experiencing issues within a ReactJS environment

Currently, I am developing a reactjs application and utilizing the owl carousel npm module to display some data. In my codebase, there is a component dedicated solely to rendering the owl carousel. To achieve this functionality, I have installed the owl c ...

angularjs running multiple promises sequentially with $q

I was recently tackling a project in AngularJS 1.5.3, and I've run into some difficulties with chaining promises. Here's a snippet of the function causing me trouble: this.login = function(username, password) { var promise = $auth.login(use ...

Searching for a particular element using its ID with JavaScript

Within this paragraph, there is an anchor tag that I am trying to access using its id in javascript: <p>Please check out <a id="myblog" href="/myblog/"> My Blog </a></p> I attempted to retrieve the tag using the following code in ...

Transforming class attributes in Typescript

I am facing a situation where I need to alter the type of a variable that stores an object based on certain conditions. Here is the variable in question: class MyClass { public referrers: SelectItemGroup[]; } The issue arises when I only need to add a ...

ensure that angular's ng-if directive sets aside space to display content when triggered by an event

I'm facing an issue with my modal form setup where if a text-field is left blank, a label saying "it is required" is generated below the field, causing the "Proceed" button to move (along with all other elements below it). How can I make sure that DOM ...

Utilizing JavaScript to manage sections within a dropdown menu

When dealing with this particular HTML code, there is a feature where a list item includes options such as all, a, b, c, and d. If the user selects 'All', it should restrict them from choosing any other items. However, if they do not choose &apos ...

Inquiring about the intricacies of using Regular Expressions in

Can anyone provide a regex that only allows the characters 0-9, a-z, A-Z, hyphen, question mark, and "/" slash, with a character length between 5 to 15? I attempted the following approach, but it did not yield the desired result: var reg3 = /^([a-zA-Z0-9? ...

"Step-by-step guide on linking an Angular controller to an already existing object

I am working with a JavaScript object that looks like this: var Test = function () { var x; var y; this.start = function() { //some action if ( x > y) { x = x* 10; // update $scope.x in Angular } }; this.end = function() ...

Populate an ng-repeat table again while maintaining its original sorting order

Incorporating a table filled with data fetched from a webservice using the ng-repeat directive, I have enabled sorting functionality for all columns. Additionally, at regular intervals, the application polls the webservice to retrieve updated data. This o ...

Refreshing a PNG file without the need to refresh the entire page

Developed a captcha using imagestring imagestring($image, 5, 5, 30, $text, $text_color); imagepng($image,"captcha_image.png"); imagepng($image,"captcha_image.png"); The code snippet above shows part of the implementation. <img ...

Is there a way for me to store the retrieved information from an API into a global variable using Node.js?

function request2API(option){ const XMLHttpRequest = require('xhr2');//Cargar módulo para solicitudes xhr2 const request = new XMLHttpRequest(); request.open('GET', urlStart + ChList[option].videosList + keyPrefix + key); request. ...

Modify the value of a variable inside another {{variable}} (not sure what it's called)

I am looking to update the variable "prefs" to reflect either "easy, medium, or hard" options based on user interaction. For instance: THIS {{choice.prefs.title}} NEEDS TO BE UPDATED TO {{choice.easy.price}} or {{choice.medium.price}} or {{choice.hard. ...

Tips on invoking a factory method from a different service method in AngularJS

I have a factory method that goes like this.. (function (angular) { "use strict"; angular .module('app') .factory('UserService', ['$rootScope', '$q', function ($rootScope, $q) { ...

Verify Session Cookies through JSONP requests

I've developed a bookmark that pulls all images from a webpage upon clicking and sends the image's src back to another server using JSONP. Challenge: The remote server must validate session authentication cookies to confirm that the user sending ...

Organizing arrays of doubles in increasing sequence

I am currently attempting to manually sort a Double Array in ascending order. The issue I am facing is that the output only displays the smallest value at the top (which is correct), but the other values are shown as 0.0. The range of values is between - ...

The next column featuring a dropdown menu

My goal is to make this sketch operational. The red elements in the sketch need to be programmed. Currently, I am using angularJS and bootstrap with the following code: <div class="col-md-6"> <div class="form-group"> <label&g ...

What is the best way to invoke a method from CodeFile with Bootstrap in C# using Visual Studio 2013?

Working on an aspx page using VS 2013 with a C# CodeFile. If Bootstrap is applied to the page, how can I reference the CodeFile to call a function? For example, when a button is clicked to handle it through a Button1_Click function in the CodeFile: <as ...

"Problem with AngularJS: Unable to display data fetched from resource using ng-repeat

I am currently working on an AngularJS application that retrieves data from a RESTful API using $resource. However, I have encountered an issue where the view is not updating with the data once it is received and assigned to my $scope. As a beginner in An ...

Add a checkbox element to a web API using ReactJS

I'm currently learning react and encountering an issue with checkboxes that I can't seem to resolve. I am working on a modal for updating and inserting data in a .net core web api, which is functioning correctly. However, within the app, I'm ...

I need to display the product name associated with the product_id found in the line_items array within the order table. I aim to achieve this functionality by utilizing Laravel with Vue.js

In my database, there is a cell called line_items in the orders table that contains data like: [ {"customer_id":"30","product_id":"10","unit_id":"2","quantity":"1","price":"2700","total_price":"2700"}, {"customer_id":"30","product_id":"43"," ...