Javascript code for-loop calculation help

• 2
• Question
• Updated 1 year ago
Hi,

I have a simple javascript code page which duplicates an item (original question here https://community.quickbase.com/quickbase/topics/javascript-to-copy-a-record-n-number-of-times-and-i... ) ....

I'm still learning Javascript and I need some help fixing up this loop:

In the code below,

a) it first retrieves the 'monthStart' variable (can be anything from 1 to 12)

b) it then prompts user for number of iterations (1 to 12),

c) It then copies the item 1 to 11 times, adding + 1 to the "nextmonth" field till "nextmonth" = 12

Now this works fine if the 'monthStart' = 1 (for January) - it then copies it 11 times and sets the value 2 (feb) to 12 (dec).  - it stops always when nextMonth gets to 12.

However we use a financial year from July (7) to June (6)... which means the month goes past 12 and starts at 1 again.

I would like to modify the code so that if 'monthStart' = 7 (or any other number) and the Prompt = 12, then it will copy 11 times, and set 'nextMonth' to the values 8, 9, 10, 11, 12, 1 , 2, 3, 4 , 5, 6 - e.g. up to the value of 'monthStart'

I'm hoping that makes sense? Please see the code below

========start code ==============
// This is used by the button that duplicates an item many times

var numMonths = prompt(" How many months to copy? enter 12 for whole year", 12); // Prompt for number of times to copy

var monthStart = parseInt(itemMonth, 10); //this is the starting month from Quickbase
var nextMonth = monthStart + 1;

var xhttp;

try{
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();

else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

if(numMonths > 0 && numMonths < 13){
for(var i = 0; i <= numMonths - 1 && nextMonth < 13; i++){

myAPI = addItem + commonfields + "&_fid_43=" + nextMonth;

xhttp.open("GET", myAPI, false);
xhttp.send();

nextMonth += 1;
}

}
}
catch(e){
console.log("Unexpected Error: " + e);
}
======end code ==========
• 226 Points

Posted 1 year ago

• 2
• 530 Points
You could just change this statement:
nextMonth += 1;

To
nextMonth = nextMonth == 12 ? 1 : nextMonth + 1;

and change the loop control from:
for(var i = 0; i <= numMonths - 1 && nextMonth < 13; i++){
To:
for(var i = 0; i <= numMonths - 1 ; i++){

If you're just learning I found the following resources useful:
https://developer.mozilla.org/en-US/docs/Web/javascript - for tutorials and specs

Neil
(Edited)
• 226 Points
Wow that's fantastic Neil, thanks so much

I just implemented it, and the button works perfectly now! Thanks!

Ⲇanom the ultimate (Dan Diebolt), Champion

• 30,184 Points
>works perfectly now!

Please take no offense but the code you posted uses outdated constructs from a decade ago:

(1) you are making multiple (a dozen) API calls when only one call to API_ImportFromCSV is needed

(2) you are  using synchronous API calls when you should be making asynchronous calls

(3) you are using XMLHttpRequest to make API calls when you should be using jQuery's AJAX methods or the fetch API

(4) Modern JavaScript rarely uses raw for loops - instead it uses functional methods such as each(), map(), filter(), reduce()

(5) There is no good reason to use prompt() when more modern dialog boxes can be accessed through jQueryUI which is already loaded on the page

Over the weekend I will create a couple of simple examples that will show you how to modernize and greatly simplify your code.