Questions tagged [for-loop]

A for loop serves as a control mechanism employed in numerous programming languages to traverse through a specified range. It embodies the method of executing statements repeatedly until the loop reaches its end. The range can vary based on the programming language, encompassing integers, iterators, and so forth.

Matter of Representing Nested For Loops in Javascript Arrays

When I have two arrays that intersect at certain elements, the resulting function should ideally output A, B, Y. However, in this case, it displays all possible combinations of lista.length * listb.length. <script> window.onload = function(){ ...

Navigating through a series of URLs using Selenium

I am encountering difficulties with looping through a list of URLs using selenium. The problem seems to lie within the #Second Part section of my code. Currently, the length of the linklinkfin list is 9, but this number can fluctuate as more URLs are gathe ...

What is the best way to assign a class to objects with a count greater than a

Can someone assist with editing a foreach loop to add a class to divs only if the number of divs is greater than 3, without affecting those with fewer divs? @if(!empty($property->testimonials)) @foreach($property->testimonials as $testimonial) ...

Looking for Precise Matching within JSON Using JavaScript

I've been experimenting with creating a form that submits data and then checks it against a JSON array to see if there's a matching object already present. Here is a snippet of my JSON data for reference: [ { "ASIN":"B0971Y6PQ3", ...

For loop not functioning correctly due to if statement

I'm just starting out and I've been working on an iterative calculation in Python that looks like this: for i in range(30): if i<10: p = 1 if 10<=i<20: p = 2 else: p = 3 But when I run the code, for the case wher ...

There seems to be a glitch in my JavaScript for loop as it is not iterating for the correct amount that it should

It seems like my for loop is not always iterating 7 times as intended. Sometimes it runs with 5 iterations, other times with 4 or 3. This is the JavaScript code I am using: var start = new Date().getTime(); var end = new Date().getTime(); function timeT ...

Increasing multiple variables within a PHP loop

Currently, I have a code that separates first and last names on a form with the possibility of having up to ten names. Instead of duplicating the code multiple times and manually updating the number each time, I am looking for a way to loop this function. ...

When looping through a numpy array, only the final item is returned

I'm experimenting with implementing a Sine wave function for navigation purposes. In my main code, I have two functions defined as follows: def stop(): steering = 1024 throttle = 1024 return steering, throttle def case2(): steering = ...

Generate summary columns using a provided list of headers

In my dataset, there is survey data along with columns containing demographic information such as age and department, as well as ratings. I want to enhance the dataset by adding new columns based on calculations from the existing rating columns. The goal ...

Python loop within a loop resetting nested iterators

Currently in the process of developing a sudoku solver, and one key aspect involves extracting the values from the 3x3 sub-box. The snippet of code I have written for this is as follows: def numbers_taken_in_box(row, col, board): col = col - (col % 3) ...

What is the process for adding a counter variable to a field within a Twig file?

I'm working on a Twig file where I need to create a table and populate it with data using a loop. The issue is that all my field names are the same, so I want to add a counter variable to differentiate them. How can I achieve this? Below is the code ...

Using selenium to iterate through previous results and accumulate them in a for loop

My code is functioning, but it seems to be accumulating the previous results. Can someone please assist me with this issue? Thank you. url=https://www.bbc.com/news/world news_search = driver.find_elements(By.XPATH, "//div[@class='gs-c-promo gs-t ...

Is there a way to showcase just the month in one spot when presenting data using Angular 13?

Just starting out with Angular and facing a challenge in the Milestone section. There is a loop for displaying years and months, but I need to ensure that the month name is displayed only once for each specific year. Can someone provide me with a possible ...

How come my AJAX request is only saving the last set of values in a continuous loop?

function saveangebotdetailfunc(url, GewerkFertig1, AngebotnrFertig1, PreisFertig1, WerkvertragID1, DatumFertig1) { $.ajax({ type: 'POST', url: url, data: { csrfmiddlewaretoken: &ap ...

The current code is displaying an "invalid syntax" error when executing the initial for loop

num = int(input()) list1 = [] list2 = [] list1 = list(map(int, input().split())) for i in range(num): if list1[i] == 0: list2.append(i) list3 = [] ...

Using React and Typescript: Populating an array within a For Loop using setTimeout is not happening sequentially or at all

I'm currently working on a function that animates images of random cars moving across the screen. My goal is to stagger the population of the "carsLeft" array using setTimeout, where I will eventually randomize the delay time for each car. Everything ...

Is it possible to locate/create a JSON keyname in a jQuery $.post function?

Currently, I am diving back into the world of Ajax after a long break. At this point, I have created a page that is able to fetch a dynamic number of elements (initially ten, but this can change), and populates each of the ten divs with relevant text. In ...

Replace the foreach loop with a for loop

As a beginner in PHP, I have a question. How can I modify this foreach loop to only iterate twice? <?php foreach($results as $row): ?> ...

Can someone assist me with troubleshooting my issue of using a for loop to iterate through an array during a function call

Having recently delved into the world of Javascript, I've encountered a challenging problem that has consumed my entire day. Despite attempting to resolve it on my own, I find myself feeling quite stuck. The structure of my code is relatively simple ...

Mouseover event in VueJS loopIs there a way to incorporate

I am working on a form that generates a component for each index. Within this component, there is a child div with edit, add, and minus buttons. I want these buttons to be displayed upon mouseover of the component. How can I achieve this dynamically with ...

Find the sum of every combination of numbers in the array

I've reviewed my code countless times and I'm stumped by the issue. My code is designed to calculate all possible sums of numbers within an array. It works perfectly when there are only 3 numbers in the array, but once I add a fourth number, it i ...

"An approach to testing and logging multiple times in Python with the help of -nosetest and unittest

Here is a condensed version of the issue: a = [[1,2,3][0,0,0]] b = [[1,0,1][0,0,0]] c = [[0,0,0][1,0,1]] If level 1 is [] and level 2 is [[]], my goal is to compare every list to see if their level 2's match (disregarding order). In this example, b ...

Ways to minimize the loading time of contents within the Dropdown

I am facing an issue with the loading time of my dropdown list. When trying to load 100,000 items on button click, it takes too long. Is there any way to reduce the loading time? Looping through Products for (int i = 0; i < 100000; i++) { ...

The exceptional speed of jQuery's each method sets it apart

I'm currently facing an issue where I am unable to successfully add the attribute data-size to my parent div. My code snippet is as follows: var width, height; $(".galerie img").each(function() { width = this.naturalWidth; height = this.naturalHeight ...

PHP Hourly Loop Across Midnight

I've dedicated considerable time to finding a solution to creating a loop that will display the time between specific $start_time and $end_time values, increasing by one hour in the format HH:MM. For example, if the $start_time is "22:00" and $end_tim ...

What is the best way to perform vectorized calculations on a pandas dataframe, replacing values with the data from the previous row if a certain condition is not satisfied

My current approach involves using a for loop with a conditional statement that triggers a calculation and updates the column in a dataframe when true. However, if the condition is not met, the data from the previous row is carried over to the new row. He ...

Is there a way I can utilize a for-loop and if statement in JavaScript to present the information accurately within the table?

My current task involves fetching data via AJAX and then using a for-loop and if-statement to determine which goods belong in each shopping cart. Once identified, I need to display these goods in separate tables corresponding to each customer. Although the ...

What is the proper way to include "arr[i]" within a for loop?

How can I include "arr[i].length" in my FOR LOOP? Using arr[0].length works correctly, but when using just "i" it throws an error. My goal is to iterate through a 2D array. function calculateSum(arr) { var total = 0; for (let i = 0; i < arr[i] ...

Java Eclipse - Utilizing a for-each loop for each line

I have successfully read a file, but now I need to process it line by line using a "for each line loop". This is what I have so far: File file = new File ("C:\\WebDrivers\\lex.txt"); Scanner scan = new Scanner(file); while(scan.hasNex ...

Transfer the index of a for loop to another function

Can you explain how to pass the 'i' value of a for loop to a different function? I want to create a click function that changes the left position of a <ul> element. Each click should use values stored in an array based on their index positions. va ...

Looping through elements with Python's Selenium WebDriverėŠ„ utilizing a For Loop

Basically, I am trying to extract all information from the 'Others' page on a website starting from the first item to the last. Here is my attempt: I encountered some difficulties due to the unique structure of the website. My goal is to retrieve all issu ...

What is the best way to loop through a list of questions and save the answers in an array of user input?

I'm encountering a challenge with iterating through an array of questions to correctly display them on the document. The initial code snippet provided below demonstrates what I aim to accomplish without using prompts. Currently, I have set up an array ca ...

What makes this effective in JavaScript?

While working on a project, I encountered the task of comparing coordinates in two arrays at the same index to see if they are identical. There are various methods to achieve this, but one particular approach piqued my interest. Why does it yield the expec ...

In PHP, the for loop executes a unique action on every alternate second and third element within a

I am having difficulty creating a grid of items in a three by X pattern using a for loop. Each column of items should have unique properties, but I am struggling to select the correct items accurately. So far, I have attempted to use Mod %. Here is an ill ...

What is the best way to combine numerous lists into a single dictionary in Python?

I am facing a challenge with an OrderedDict that contains multiple values for the same city which I need to merge into one single dictionary. The structure of the OrderedDict is as follows: print(OrderedDict) OrderedDict([('UF' , 'New York'), ('Total', ...

Executing a for loop asynchronously on an AsyncGenerator

When working with an asynchronous generator, the expectation is to be able to iterate through it asynchronously. However, after running the code below, a synchronous for loop is produced instead: import asyncio async def time_consuming(t): print(f"G ...