Controller unable to properly increment values

    $scope.isChecked = function(id){              
    var i=0,j=0,k=0;
    //$scope.abc[i].usertype[j].keywords[0].key_bool=true;
    if($scope.abc[i].type_selected == true){
        while($scope.abc[i].usertype.length){
            while($scope.abc[i].usertype[j].keywords.length){
                if($scope.abc[i].usertype[j].keywords[k]._id == id){
                    if($scope.abc[i].usertype[j].keywords[k].key_bool == true){
                        $scope.abc[i].usertype[j].keywords[k].key_bool = false;
                        return false;
                    }
                    else{
                        $scope.abc[i].usertype[j].keywords[k].key_bool = true;
                        return true;
                    }                        
                }
                k++;
            }
            j++;
        }
    }
};

When incrementing k++, it is working as expected, but incrementing j++ is causing issues - can someone please explain why this is happening?

The isChecked function is called whenever a checkbox is checked or unchecked like this:

ng-click="isChecked(l._id)"

Everything functions properly for 'j=0', however, problems arise for subsequent 'j' values.

Answer №1

When a return statement is encountered, the function immediately exits, causing the subsequent code like j++; to be skipped. This phenomenon occurs every time the if statement is triggered.

Answer №2

When i=0, it enters the inner loop and exits upon reaching the return statement.

This is why the outer loop does not run again.

To learn more about using the return statement, visit this resource

Answer №3

Success! I finally found the solution by adding k=0; between the two while loops. Many thanks to everyone who helped me along the way.

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

Reasons for the failure of file uploads from the React frontend to the backend system

Recently, I embarked on a new project that involves using React for the front-end and Node for the back-end. The main requirement of this project is to upload multiple images from the front-end, with the back-end handling the file uploads to Cloudinary. I ...

Discover the XPath of a post on a Facebook page with the help of HtmlUnit

I am trying to fetch the xpath of a Facebook post using HtmlUnit. To better understand my goal, you can check out these two related questions: Supernatural behaviour with a Facebook page HtmlUnit commenting out lines of Facebook page To replicate my pro ...

JavaScript tutorial: Locate a specific word in a file and display the subsequent word

Seeking assistance with a task: I need to open a locally stored server file, search for a specific word, and then print the next word after finding the specified one. I have managed to write code that successfully opens the file, but it currently prints e ...

Response received after an extended operation in HTTP

We are currently working on a project using Node.js along with the Expressjs framework. The application we are developing involves storing client/prospect information, with authenticated users having the ability to modify the database and trigger long-runn ...

Experiencing difficulties accessing the API route through Express

Every time I attempt to access /api/file, I am receiving a status code of 404. Here is the relevant code snippet: app.js : ... app.use("/api", require("./routes/users")); app.use("/api", require("./routes/file")); ...

Encountered an issue accessing property 'Component' which is undefined during the webpack build of a React component plugin

I have developed a wrapper for my leaflet plugin called leaflet-arrowheads using react-leaflet. This component is designed to be installed as an npm package, imported, and used easily. The structure of the component is quite simple: import React from &apo ...

Identifying the Click Event Within an ngx Bootstrap Modal

I recently set up an ngx bootstrap modal using the instructions provided in this helpful guide - . However, I'm facing a challenge in detecting click events within the modal body once it's open. Below is the code snippet from my app component. D ...

JavaScript nested function that returns the ID of the first div element only upon being clicked

I am facing an issue with a function that returns the id of the first div in a post when an ajax call is made. The problem is that it repeats the same id for all subsequent elements or div tags. However, when the function is used on click with specified ...

Is there a way to replicate the functionality of $(document).ready for dynamically loaded AJAX content?

$(document).ready(handler) is triggered once the DOM has finished loading. However, if new content containing a $(document).ready(handler) function is added to the page via AJAX, this function will be executed immediately according to the jQuery API. It&ap ...

What could be the reason for the mismatch in size between the downloaded file in Express.js and the file size on the server?

My code using express.js is quite simple: app.get("/download", download); and export let download = async (req: Request, res: Response) => { const file = "/tmp/my-file.zip"; res.download(file); } The client-side code is also straightforward: im ...

What is the best way to dynamically link an Angular Material table with information pulled from the backend server

I am attempting to connect a mat-table with data from the backend API following this Angular Material Table Dynamic Columns without model. Below is the relevant content from the .ts file: technologyList = []; listTechnology = function () { ...

Sending JSON data from AngularJS to Django REST API using POST method

Currently, I am working on an app using AngularJS and relying on an external API that is built with Django. To make API calls, I have been utilizing Restangular (although I've tried $http as well with the same results). By default, for post requests ...

Display additional information from a JSON file after choosing an ID with AngularJS Select

After saving a JSON file filled with information, I managed to successfully populate a select menu with the names of each element from the JSON data using this code snippet: <select ng-model="car.marca" ng-options="item.brakeId as item.name for item in ...

Creating interactive HTML buttons using JavaScript to trigger AJAX requests

My current task involves populating an HTML table to showcase users. By making API calls to retrieve user data, I utilize Javascript to add rows to the table. Each row ends with a delete button, intended to trigger a $put request to a separate API endpoint ...

Creating a dynamic category menu using angularJS

I'm struggling with the logic behind creating a category menu using AngularJS I need to display all categories with a parent category id of 0. Once that is done, I want to display all subcategories that belong to each parent category. The final categ ...

Use the keyboard to interact with the user interface

Imagine you have the following HTML markup - <ul id="list"> <li class="list-item" tabindex="0">test 1</li> <li class="list-item" tabindex="1">test 2</li> <li class="list-item" tabindex="2">test 3</li> ...

When activating another function, Javascript failed to trim and hide the div as intended

Before adding another function (*2), my function (*1) was working perfectly. However, after adding the second function, there seems to be a conflict and now it's not working as expected. :( <script type="text/javascript> $(function() { ...

Search the table for checked boxes and textboxes that are not empty

Could you suggest alternative ways to express the following scenario? I have a table with 3 rows. Each row contains a column with 3 checkboxes and another column with just a text box. I would like it so that when the values are retrieved from the database ...

What is the method for calling a JavaScript function from one file to another within a Node.js environment?

Just starting out with JavaScript and exploring the idea of breaking the code into multiple modules. While working with nodejs, I've encountered an issue where it's complaining about pathChecker not being defined. Any insights on how to resolve t ...

Encountering a null pointer exception when launching the Chrome browser using Selenium

Hope you all are doing well. I need assistance in resolving a null pointer issue while developing a new Selenium framework for my company. The problem arises after calling the method "StartBrowser()" from the base class in the browser class. Everything ru ...