So I created a script that takes some data from 3 different google spreadsheets and paste them onto multiple spreadsheets. The location to paste it on is separated into 3 different sheets (overview, NL, and BE). For the overview tab, all of the spreadsheet contains the same data (prodArray), therefore I created it before the loops. For the NL and BE it uses the same logic but different source (finalArray), so I created it inside the MSF loop. Also for these 3 data, the first 4 columns are the same so I created the initArray.
However the weird thing is the prodArray changes after the first iteration of the sCount loop (the one that changes to the different spreadsheets), even though it should not be changed after entering the MSF loop. After trying to debug it I found that the value changes inside the loop that creates the finalArray. When finalArray[i][4] and [5] is inputted, for some reason the prodArray changes. Here it is the part of the code, and this is part of the log of the program.
Logger.log("A\n" + prodArray[i]);
finalArray[i][4] = "=COUNTIF(F" + (i + 2) + ":AA" + (i + 2) + ";\"SELECTED\")/COUNTA(F" + (i + 2) + ":AA" + (i + 2) + ")"
if(selected[i] == supName[sCount]) finalArray[i][5] = "SELECTED";
else finalArray[i][5] = "NOT SELECTED";
Logger.log("B\n" + prodArray[i]);
This is the fullcode, the only thing that I cut are the sheetIDs.
function setMarketplace(){
var itemCount = centralSh.getLastRow() - 1;
var prodData = centralSh.getRange(2,1,itemCount, 5).getValues();
var initArray = new Array(itemCount);
var prodArray = new Array(itemCount);
for (var i = 0; i < itemCount; i++){
prodArray[i] = new Array(6)
initArray[i] = new Array(4);
initArray[i][0] = prodData[i][0];
for (var c = 0; c < 3; c++){
initArray[i][c + 1] = prodData[i][c + 2];
}
prodArray[i] = initArray[i];
prodArray[i][4] = "=VLOOKUP(A" + (i + 2) + ";NL!A:E;5;0)";
prodArray[i][5] = "=VLOOKUP(A" + (i + 2) + ";BE!A:E;5;0)";
}
for(var msfNumCount = 0; msfNumCount < 2; msfNumCount++){
Logger.log("Starting with MSF " + msfName[msfNumCount])
var msfSh = SpreadsheetApp.openById(msfID[msfNumCount]).getSheetByName("Supplier Selection " + msfName[msfNumCount]);
Logger.log("Fetching Data Source")
var msfCount = msfSh.getLastRow();
var msfData = msfSh.getRange(2,1,msfCount, 7).getValues();
Logger.log("Rearranging Data")
var selected = new Array(itemCount);
for (var i = 0; i < itemCount; i++){
for (var j = 0; j < msfCount; j++){
if(prodData[i][0] == msfData[j][0]){
selected[i] = msfData[j][6];
j = msfCount;
}
}
}
var finalArray = new Array(itemCount)
for (var sCount = 0; sCount < supName.length; sCount++){
if (msfNumCount == 0){
var shProd = SpreadsheetApp.openById(supSheet[sCount]).getSheetByName("Overview Percentages");
shProd.getRange(2,1,itemCount,6).setValues(prodArray);
}
Logger.log("Calculating Data for Supp " + sCount + " MSF " + msfName[msfNumCount])
for(var i = 0; i < itemCount; i++){
finalArray[i] = new Array(6)
finalArray[i] = initArray[i];
Logger.log("A\n" + prodArray[i]);
finalArray[i][4] = "=COUNTIF(F" + (i + 2) + ":AA" + (i + 2) + ";\"SELECTED\")/COUNTA(F" + (i + 2) + ":AA" + (i + 2) + ")"
if(selected[i] == supName[sCount]) finalArray[i][5] = "SELECTED";
else finalArray[i][5] = "NOT SELECTED";
Logger.log("B\n" + prodArray[i]);
}
var sh = SpreadsheetApp.openById(supSheet[sCount]).getSheetByName(msfName[msfNumCount]);
Logger.log("Pasting Data for Supp " + sCount + " MSF " + msfName[msfNumCount]);
sh.getRange(2,1,itemCount,6).setValues(finalArray);
}
}
}
So yea I have no clue why this happens.
Book has page with the ff code to convert Centigrade to Fahrenheit, we've been asked to rewrite/simplify it.
function convertToCentigrade(degFahren)
{
var degCent;
degCent = 5/9 * (degFahren - 32);
return degCent;
}
var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter;
for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{
degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);
}
for (loopCounter = 2; loopCounter >= 0; loopCounter-- )
{
document.write(“Value “ + loopCounter + “ was “ +
degFahren[loopCounter] + “ degrees Fahrenheit”);
document.write(“ which is “ + degCent[loopCounter] +
“ degrees centigrade<br />”);
}
My version:
var degFar = [212, 32, -459.15];
var degCent = [];
function convert(input) {
result = (5/9 * (input - 32));
return result;
}
for (i = 0; j = degFar.length; i <= j; i++) {
degCent.push(convert(degFar[i]));
document.write(degCent[i]);
}
I pulling an error (obviously), but I don't understand what I'm doing wrong.
degFar has 3 elements but its index starts at 0.
Your array is like this:
[0] 212
[1] 32
[2] -459.15
You're trying to push an index [3] that doesn't exists. So modify your loop with this, notice I replaced i<=j for i<j:
for (i = 0; j = degFar.length; i < j; i++) {
degCent.push(convert(degFar[i]));
document.write(degCent[i]);
}
Your for loop is causing the error. You try to use two variables, 'j' and 'i'. I'm not sure what you are trying to accomplish by using 'j' (on top of that you never declared 'j' before trying to initialize it to degFar.length).
Keeping with your logic, I would get rid of 'j'-- you don't need it. After removing 'j' and using 'i' it should look something like this:
for (i = 0; i < degFar.length; i++) {
degCent.push(convert(degFar[i]));
document.write(degCent[i] + "<br>");
}
Hope this helps.
I'm trying to separate / explode an String to do something with it later on.
The input string is this:
1_2_3_2_2
The function I'm calling with the above value as parameter:
void parseXString(String value){
int amountX = (value.length() / 2) + 1;
int seperatorIndex = value.indexOf('_');
int secondSeperator = 0;
for(int i = 0; i < amountX; i++){
String xPoint = "";
if(i == 0){
xPoint = value.substring(0, seperatorIndex);
}else{
xPoint = value.substring(seperatorIndex + 1, secondSeperator);
}
sendMessage((String)i + " X = " + xPoint + " || SEP: " + (String)seperatorIndex + " / " + (String)secondSeperator );
seperatorIndex = value.indexOf("_", seperatorIndex + 1);
secondSeperator = value.indexOf("_", seperatorIndex + 1);
}
sendMessage("Last X = " + value.substring(seperatorIndex + 1));
}
The sendMessage function will shout the value back to the operating Java application. The output I get is this:
0 X = 1 || SEP: 1 / 0
1 X = 3 || SEP: 3 / 5
2 X = 2 || SEP: 5 / 7
3 X = 2 || SEP: 7 / -1
4 X = 1 || SEP: -1 / 1
Last X = 2_3_2_2
As you can notice, on the second iteration there should be an return of the value 2 instead of an 3.
I think there's something wrong with the seperatorIndexes, but I'm out of the blank right now (working on this way to long).
So my question is very simple. Why doesn't I get the right value back / how can I fix that?
Your error is that you increase seperatorIndex in the first iteration. Therefore, seperatorIndex is 3 in your second iteration.
You should put the part where you increment seperatorIndex into the else part of your if(i == 0) condition. When doing this, you also have to increment secondSeperator in the if part of your condition.
I have seen this question for other languages but not for AS3... and I'm having a hard time understanding it...
I need to generate 3 numbers, randomly, from 0 to 2, but they cannot repeat (as in 000, 001, 222, 212 etc) and they cannot be in the correct order (0,1,2)...
Im using
for (var u: int = 0; u < 3; u++)
{
mcCor = new CorDaCarta();
mcCor.x = larguraTrio + (mcCor.width + 5) * (u % 3);
mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(u / 3));
mcCor.gotoAndStop((Math.random() * (2 - u + 1) + u) | 0); // random w/ repeats
//mcCor.gotoAndStop(Math.floor(Math.random() * (2 - u + 1) + u)); // random w/ repeats
//mcCor.gotoAndStop((Math.random() * 3) | 0); // crap....
//mcCor.gotoAndStop(Math.round(Math.random()*u)); // 1,1,1
//mcCor.gotoAndStop(u + 1); // 1,2,3
mcCor.buttonMode = true;
mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
mcExplic.addChild(mcCor);
trio.push(mcCor);
}
those are the codes i've been trying.... best one so far is the active one (without the //), but it still gives me duplicates (as 1,1,1) and still has a small chance to come 0,1,2....
BTW, what I want is to mcCor to gotoAndStop on frames 1, 2 or 3....without repeating, so THE USER can put it on the right order (1,2,3 or (u= 0,1,2), thats why I add + 1 sometimes there)
any thoughts?? =)
I've found that one way to ensure random, unique numbers is to store the possible numbers in an array, and then sort them using a "random" sort:
// store the numbers 0, 1, 2 in an array
var sortedNumbers:Array = [];
for(var i:int = 0; i < 3; i++)
{
sortedNumbers.push(i);
}
var unsortedNumbers:Array = sortedNumbers.slice(); // make a copy of the sorted numbers
trace(sortedNumbers); // 0,1,2
trace(unsortedNumbers); // 0,1,2
// randomly sort array until it no longer matches the sorted array
while(sortedNumbers.join() == unsortedNumbers.join())
{
unsortedNumbers.sort(function (a:int, b:int):int { return Math.random() > .5 ? -1 : 1; });
}
trace(unsortedNumbers); // [1,0,2], [2,1,0], [0,1,2], etc
for (var u: int = 0; u < 3; u++)
{
mcCor = new CorDaCarta();
mcCor.x = larguraTrio + (mcCor.width + 5) * (u % 3);
mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(u / 3));
// grab the corresponding value from the unsorted array
mcCor.gotoAndStop(unsortedNumbers[u] + 1);
mcCor.buttonMode = true;
mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
mcExplic.addChild(mcCor);
trio.push(mcCor);
}
Marcela is right. Approach with an Array is widely used for such task. Of course, you will need to check 0, 1, 2 sequence and this will be ugly, but in common code to get the random sequence of integers can look like this:
function getRandomSequence(min:int, max:int):Array
{
if (min > max) throw new Error("Max value should be greater than Min value!");
if (min == max) return [min];
var values:Array = [];
for (var i:int = min; i <= max; i++) values.push(i);
var result:Array = [];
while (values.length > 0) result = result.concat(values.splice(Math.floor(Math.random() * values.length), 1));
return result;
}
for (var i:uint = 0; i < 10; i++)
{
trace(getRandomSequence(1, 10));
}
You will get something like that:
2,9,3,8,10,6,5,1,4,7
6,1,2,4,8,9,5,10,7,3
3,9,10,6,8,2,5,4,1,7
7,6,1,4,3,8,9,2,10,5
4,6,7,1,3,2,9,10,8,5
3,10,5,9,1,7,2,4,8,6
1,7,9,6,10,3,4,5,2,8
4,10,8,9,3,2,6,1,7,5
1,7,8,9,10,6,4,3,2,5
7,5,4,2,8,6,10,3,9,1
I created this for you. It is working but it can be optimized...
Hope is good for you.
var arr : Array = [];
var r : int;
for (var i: int = 0; i < 3; i++){
r=rand(0,2);
if(i == 1){
if(arr[0] == r){
i--;
continue;
}
if(arr[0] == 0){
if(r==1){
i--;
continue;
}
}
}else if(i==2){
if(arr[0] == r || arr[1] == r){
i--;
continue;
}
}
arr[i] = r;
}
trace(arr);
for(var i=0;i<3;i++){
mcCor = new CorDaCarta();
mcCor.x = larguraTrio + (mcCor.width + 5) * (i % 3);
mcCor.y = alturaTrio + (mcCor.height + 5) * (Math.floor(i / 3));
mcCor.gotoAndStop(arr[i]);
mcCor.buttonMode = true;
mcCor.addEventListener(MouseEvent.CLICK, cliquetrio);
mcExplic.addChild(mcCor);
trio.push(mcCor);
}
function rand(min:int, max:int):int {
return Math.round(Math.random() * (max - min) + min);
}
try this...
Been working on a program that is suppose to Loop and display 13 times. Here is my code
{
var count;
var user_Input;
var output_msg;
var cel;
count = 0;
do
{
user_Input = get_integer("Temperature conversion","");
count = count + 1;
cel = user_Input * 9/5 +32;
user_Input = user_Input +10;
output_msg =(string(count) + "Celsius" + string(user_Input) + " = Farenheit " + string(cel));
show_message(output_msg);
}
until (count == 13)
}
What It Does is it Displays the loop each time I hit enter instead of showing all 13 at once also if i enter 10 for example each time it loops its suppose to add 10 from the last loop.
eg. 1. Celsius 10 = Farenheit (answer here)
..... 2. Celsius 20 = Farenheit (answer Here)
......13. Celsuis 130 = Farenheit "" if some one could walk me through and help me that would be great
What you'll have to do is :
move the dialog box show_message outside the loop, well, after the Do loop to be precise. Then, it will be displayed only at the end of the loop, while the get_integer dialog box will of course wait for the user to enter a value.
move the get_integer aswell outside the loop, right before it. User will just have to input value once. If you put it inside the loop, you'll be asked to enter a value 13th times...
append the resulting calculations to the message to be displayed contained in output_msg to itself, with the line feed "#" at the end.
{
var count = 0;
var user_Input;
var output_msg = "";
var cel;
count = 0;
user_Input = get_integer("Temperature conversion","");
do
{
count = count + 1;
cel = user_Input * 9 / 5 + 32;
user_Input = user_Input + 10;
output_msg = (output_msg + string(count) + ". Celsius" + string(user_Input) + " = Farenheit " + string(cel) + "#");
}
until (count == 13)
show_message(output_msg);
}
For clarity, I've initialized some variables initial values.
Your issue is not a code issue, it's a logic issue (except for the line feed) Everything inside a loop, (Do, While) will be executed on every iteration. If you don't want something to be executed, you must either move it outside the loop (before/after), or use a condition check.