What is the method for creating a number pattern within a loop?

Below is a code snippet I have:

for ( let i = 1; i <= 12; i++ ) {
    console.log(i % 4);
}

This code produces the following output:

1
2
3
0
1
2
3
0
1
2
3
0

I want to modify the pattern (i % 4) so that it outputs the following sequence instead:

1
1
1
1
2
2
2
2
3
3
3
3

I am looking for a way to achieve this without introducing additional variables, rather by adjusting the pattern within the console.log statement to produce the desired sequence.
The sequence should increment by 1 every 4 iterations and should be applicable for more than just 12 iterations.

Answer №1

To get the next integer value after dividing by 4, you can use the Math.ceil function.

for ( let i = 1; i <= 12; i++ ) {
    console.log(Math.ceil(i / 4));
}

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

Is Consistency Possible with CSS Transition Direction?

Take a look at this example: https://codepen.io/jon424/pen/XWzGNLe In the provided code snippet, there is a button that can toggle the visibility of an image. When clicked, the image disappears from bottom to top and reappears when clicked again. The use ...

Setting individual state variables for each child component within a Vue loop

When I look at the code snippet below, I find myself facing an issue with state usage. <div class="featured-card-container"> <swiper class="swiper" :options="swiperOption"> <template v-fo ...

What steps are involved in integrating Element Plus with Nuxt 3?

Seeking assistance with installing Element Plus in Nuxt 3. I followed the instructions from the official Element Plus documentation, but despite adding unplugin-vue-components, unplugin-auto-import, and adjusting webpack settings in the nuxt config file, ...

Ordering a string of whole numbers using JavaScript

I'm currently working on a form that takes a string of numbers, splits them at each semi colon and space, sorts the numbers, and then displays the sorted list. However, when I click the button, the value in the text box doesn't get posted. Can ...

Tips for manipulating and storing nested information within the "state" by utilizing "text input"

Using Material-ui Version: 0.1 The current value stored in state: formatter = [ {currencyField: "", amountField: ""} {accountField: "", amountField: ""} {numberField: ""} ] I am looking to update each element of the object in state ...

Just finished the "Modern React with Redux" Udemy course and encountering some peculiar problems. Any assistance in solving them would be greatly appreciated!

While following the lectures, I made the decision to completely remove the "src" folder. To my surprise, even after refreshing the browser on the "localhost:8080" page, the content "Hello There!!!" from the index.js file remained visible. This was quite st ...

Exploring collision detection in Three.js

I'm fairly new to 3D and Threejs. I've created a scene with a ground, where many cubes are positioned on top of it. http://jsfiddle.net/whurp02s/1/ My goal is to select the cubes that intersect with the yellow rectangle. To achieve this, I re ...

The Angular JavaScript page successfully compiles, yet displays only a blank screen

I am facing an issue with my Angular app where it compiles successfully, but the HTML page appears blank and my application is not displaying properly. I have encountered similar problems in the past which were often related to Imports, but this time I&apo ...

Converting JavaScript code to jQuery and integrating it into a WordPress website has become a common practice

I recently developed a working javascript function as shown below: function calc(A,B,SUM) { var one = Number(A); var two = Number(document.getElementById(B).value); if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } if (isNaN(tw ...

Unleashing the power of conditional exports in package.json

Within my package.json, I define an exports section: "exports": { "import": "./dist/a.es.js", "require": "./dist/b.umd.js" }, However, during development, I wish to use different pa ...

Leveraging bootstrap's grid system to prioritize content layout based on the

Currently working on a compact React Js project with Bootstrap for styling. One obstacle I've encountered is the need to display the last element as the first one on the page. To achieve this, I plan to utilize the order-1 and order-12 classes from Bo ...

Maxima's nested for loops generate arrays efficiently

With the goal of creating 11 arrays in Maxima, I attempted the following approach: for n:1 step 1 while n<=11 do( for j:1 while j<=21 do( if i<j then aa[n][i,j]:i+j+n)); Although this compiles without errors, I am unable to use it as intended. F ...

A new sub-schema has been created in the JSON schema specifically tailored to a certain property

I needed to create a JSON schema with 3 fields: num, key1, and key2. The fields num and key1 are required, while the field key2 is only mandatory if key1 has a value of 'abc'. Below is the schema structure I came up with: schema = { type: &ap ...

Arranging an array of objects in Angular based on the value inside each object

How can I retrieve objects by filtering the key value within the object? In the example below, I need to extract data only with session_id:23. data = [{ name: "First Name", session_id : "23", login: "date" }, { name: "Second Name", ...

Deactivating Cluetip tool tips and adjusting the height limit in JQuery

How can I momentarily turn off the hints? I have seen references to being able to do so on the website and in this forum, but I am having trouble locating the command to disable them. I just need to temporarily deactivate them and then enable them again. ...

Retrieve the selected value from a specific div element

Having some issues with retrieving the selected value from a Bootstrap chosen select field within a div. Any ideas on what might be causing this problem? $('#content_' + id).find('#GroupID > option:selected').text() // returns blank ...

Create a still image using Three.js

After following the tutorial for setting up a basic scene at this link, it is suggested to call the renderer in the standard way: function render() { requestAnimationFrame( render ); renderer.render( scene, camera ); } render(); However, since I ...

Ensuring that a $(function() is consistently executed and does not diminish over time

Simply put, I am troubleshooting my website and in order for it to function properly, I need to include the following code: <script type="text/javascript"> $ (function() { RESPONSIVEUI.responsiveTabs(); }); </script> What I& ...

How can you sustain a backend connection using NodeJS as users navigate through different pages?

Currently, I am establishing connections through thrift (node-thrift) with a backend server for api calls. The communication is bidirectional (push/pull) to NodeJS. As users navigate various URLs and Node serves jade templates and javascript files using C ...

Controller receiving empty object array from FormData

I am encountering an issue with my ajax call to the controller, where I am passing FormData() containing an array of objects and other properties. The list array that I pass seems to have 0 elements in the controller. Can anyone assist me with this problem ...