WilliamEvans1
7 years agoQrew Cadet
Javascript code for-loop calculation help
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
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;
}
}
location.reload();
}
catch(e){
console.log("Unexpected Error: " + e);
}
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;
}
}
location.reload();
}
catch(e){
console.log("Unexpected Error: " + e);
}
======end code ==========