"; missing before instruction" - arrays

While using this code I get "; missing before instruction" error.
I believe this could have a simple explanation, but actually I can't understand what's wrong.
Also tried with switch/case statements but get same error... there could be something wrong with array...
function motori(){
var esito = [];
for (var j = 6; j < 16; j++) {
var rngRiga= foglio.getRange("AJ"+j+":BT"+j).getValues(); //store in array
var riga = rngRiga[0].join('-'); // build a string with array
Logger.log("riga = " + riga); // all right till now
if (riga.search("KOS") > -1){
var esito[j] = "KOS";} //<---- here highlights the error
else if (riga.search("OKS") > -1){
var esito[j] = "OKS";}
else if (riga.search("AN") > -1){
var esito[j] = "AN";}
else if (riga.search("OK") > -1){
var esito[j] = "OK";}
else if (riga.search("KO") > -1){
var esito[j] = "KO";}
Logger.log("result = " + esito);
Logger.log("j = " + j);
}
}
What I need is a new array with the result of the if statements.

Your issue is due to you already declared the variable esito and then inside the if you try to declare it again, so that will cause a problem because you're trying to create the same variable again. change Your code as follow:
function motori(){
var esito = [];
for (var j = 6; j < 16; j++) {
var rngRiga= foglio.getRange("AJ"+j+":BT"+j).getValues(); //store in array
var riga = rngRiga[0].join('-'); // build a string with array
Logger.log("riga = " + riga); // all right till now
if(riga.search("KOS") > -1){
esito[j] = "KOS";} //<---- here highlights the error
else if (riga.search("OKS") > -1){
esito[j] = "OKS";}
else if (riga.search("AN") > -1){
esito[j] = "AN";}
else if (riga.search("OK") > -1){
esito[j] = "OK";}
else if (riga.search("KO") > -1){
esito[j] = "KO";}
Logger.log("result = " + esito);
Logger.log("j = " + j);
}
}

Related

Why array comparison isn't working? (GAS)

The code below used to work. Now, when comparing two arrays, even though there is a difference, as highlighted below, the code says that there is a duplicate.
I've tried it using .join() as well, but I keep getting the same result.
Script:
function SaveEditedEntry() {
const lock = LockService.getScriptLock();
lock.tryLock(3000);
if (lock.hasLock()) {
var sourceSheet = 'Entries';
var destinationSheet = 'Database';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var LastRowSource = sheet.getLastRow();
//var LastColumnSource = sheet.getLastColumn();
var entryValues = sheet.getRange(7, 1, LastRowSource, 16).getValues();
var dbSheet = ss.getSheetByName(destinationSheet);
var entryDataPushed = [];
var dbData = dbSheet.getRange(2, 1, dbSheet.getLastRow(), 16).getValues();
var dbDataPushed = [];
//var dbDataPushed = new Array();
var company = sheet.getRange("B3").getValue();
var period = sheet.getRange("B4").getValue();
var entryID = sheet.getRange("J5").getValue();
var timeStamp = new Date();
var user = Session.getActiveUser().getEmail();
if (company == '') {
Browser.msgBox('Make sure that you have chosen a valid company.');
return;
}
if (period == '') {
Browser.msgBox('Please, make sure that you have chosen a valid period.');
return;
}
var emptyRow = 0;
for (var i = 0; i < entryValues.length; i++) {
if (entryValues[i][0] != '') {
emptyRow = i + 1;
}
if (emptyRow < 1) {
Browser.msgBox('Please, make sure that you have entered a transaction.');
return;
}
if (entryValues[i][0] != '') {
entryDataPushed.push(entryValues[i]);
}
}
for (var n = 0; n < dbData.length; n++) {
if (dbData[n][1] != '' && dbData[n][0] == company && dbData[n][14] == period) {
dbDataPushed.push(dbData[n]);
}
}
var duplicate = false;
loop1:
for (var x = 0; x < entryValues.length; x++) {
loop2:
for (var j = 0; j < dbDataPushed.length; j++) {
if (JSON.stringify(entryValues[x]) == JSON.stringify(dbDataPushed[j])) {
duplicate = true;
break loop1;
}
}
}
Logger.log("EntryDataPushed: " + entryDataPushed);
Logger.log("dbDataPushed: " + dbDataPushed);
if (duplicate == true) {
Browser.msgBox('No change has been made.');
return;
This is the logs print:
Could anyone point me to the right direction on getting this solved?
Thank you!
Antonio
You were comparing entryValues vs dbDataPushed but is logging entryDataPushed and dbDataPushed. Is this expected?
Anyways, since you are logging the variables, I assume the variables logged are the ones you need to compare.
Convert them into JSON string by wrapping the variables with JSON.stringify(). I use this all the time when I encounter an issue similar to yours right now.
var duplicate = false;
if (JSON.stringify(entryDataPushed) == JSON.stringify(dbDataPushed)) {
duplicate = true;
}
Output:
See resource below for other ways to properly compare arrays if the above code doesn't work.
Resources:
JSON.stringify()
StackOverflow Reference

Why for loop failure in angularjs?

I am having a angularjs script. In 4th if condition (if(myflag == 2)), when i am enter any minus value less than array length then that time array element removed with same product id. For Example, In my array there are 5 element of same product id, Now i want to remove 4 element then i am entering -4 then my array does not flow 4 time and splice specific index
//SCript
$scope.checkEneteredQuantity = function(qty,bagProduct){
var qty = parseInt(qty);
var nwqt = parseInt(qty);
var myflag = 0;
if(qty > 0){
myflag = 1;
}else if(qty < 0){
myflag = 2;
}
if(myflag == 1){
for(var i = 0;i < qty ; i++){
bagProduct.count = 1;
$scope.addProductInBag(bagProduct);
}
}
if(myflag == 2){
var nt = Math.abs(nwqt);
var ct = 0
for(var k = 0;k < $scope.newBagListOfProduct.length;k++){
if($scope.newBagListOfProduct[k].prod_id == bagProduct.prod_id){
if(nt != ct){
bagProduct.count = 1;
$scope.newBagListOfProduct.splice(k,1);
}
ct ++;
}
}
}
}

My if-statement doesn't work

It's either going to be "Riktig, bokstaven forekommer" for right letter chosen or otherwise. What have i done wrong, when i start it now none of the Messages show.
btnStart.addEventListener(MouseEvent.CLICK, start);
var feil:Array = new Array ;
var riktig:Array = new Array ;
var bokstav:Array = new Array ;
bokstav[0] = "b";
bokstav[1] = "r";
bokstav[2] = "e";
bokstav[3] = "v"
var txtBokstav:TextField = new TextField( );
txtBokstav.maxChars = 1;
txtBokstav.restrict = "a-z æ ø å";
txtBokstav.type = flash.text.TextFieldType.INPUT;
addChild(txtBokstav);
function start(evt)
{
var bokstavInn:String = String(txtBokstav.text);
if (txtBokstav.text.length == 1)
{
if (bokstav.indexOf(bokstavInn) >= 0)
{
txtUtskrift.text = "Riktig, bokstaven forekommer";
riktig.push(bokstavInn);
for (var i = 0; i < riktig.length; i++)
{
txtUtskrift.appendText(riktig[i] + ", ");
}
}
else
{
txtUtskrift.text = "Feil, bokstaven forekommer ikke";
feil.push(bokstavInn);
for (var l = 0; l < feil.length; l++)
{
txtUtskrift.appendText(feil[l] + ", ");
}
}
}
}
1) Are you sure that txtUtskrift is on the Stage and visible?
2) Make sure that start() is being called (use a breakpoint or trace)
3) As askmozo suggests, place trace statements in the possible flow of execution. So, at the beginning of the start function, and within each possible if condition. You might add an else for the text.length test:
function start( evt )
{
trace( "start()..." );
if (txtBokstav.text.length == 1)
{
// ... what you already have
}
else
{
trace( "txtBokstav length not valid:", txtBokStav.text.length );
}
trace( "...start()" );
}

accessing array elements in another frame as3

I have created an Array in frame 1 and dynamically create a list of movieclips with it , and I want to create the same Array in frame 3 and those movieclips again if something is true or false.is it possible ? because when I'm trying to do this , I will get this error :
1151: A conflict exists with definition i in namespace internal.
here is my code at frame 1 :
stop();
import flash.display.MovieClip;
var p_x:uint = 90;
var p_y:uint = 108;
var my_list:String = "Games,Videos, Medias,Images,Photos,Personal Photos,Social Media,Private,Social,None,Names,Families";
var myListString:Array = my_list.split(",");
var myArray:Array=new Array ();
var listObject = 1;
for (var i:uint=0; i<12; i++)
{
var myItemList:mrb=new mrb();
myItemList.x = p_x;
myItemList.y = p_y + 80;
myItemList.y = (p_y + (listObject * 65));
myArray.push(myItemList);
myItemList.txt.text = i.toString();
myItemList.txt.text = myListString[i].toString();
myItemList.name = "item"+i;
addChild(myItemList) as MovieClip ;
listObject++;
}
and here is code at frame 3 :
var tmpCurFrame:int = currentFrame;
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) {
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
if (so.data.fav1 != undefined)
{
if ( so.data.fav1 == "on")
{
for (var i:int = 0; i < myListString.length;)
{ if (myListString[i].indexOf() > -1)
{
var myRectangle:mrb=new mrb();
trace("found it at index: " + i);
myRectangle.x = p_x;
myRectangle.y = (p_y + (findobject * 50));
trace('p_y+80 =' + (p_y+(findobject*80)) + 'p_x = ' + p_x );
myArray.push(myRectangle);
myRectangle.txt.text = myListString[i].toString();
trace(my_array2[i].toString() );
addChild(myRectangle);
}
}
}
}
else
{
fav_1.fav_on.visible=true;
}
}
This error message simply means that you use twice the same variable i. You just have to give them differents names.

Search an array for a string and return the index in AS3

I want to search an array to see if it contains a specific string, and then get the index of the result.
For example, if I had:
array[0] = "dogs";
array[1] = "cats";
array[2] = "oranges";
I want to be able to search for "oran" and get 2 back. Any idea on how to do this?
You can do something like this:
function findInArray(str:String):int {
for(var i:int = 0; i < array.length; i++){
if(array[i] == str){
trace("found it at index: " + i);
return i;
}
}
return -1; //If not found
}
Then whenever you want to find something call it like:
findInArray("oranges"); // Returns 2
To search for a part of the word can pontentially return undesiderd results for bigger lists, but you can do it with the following:
function findInArrayPartially(str:String):int {
for(var i:int = 0; i < array.length; i++){
if(array[i].indexOf(str) > -1){
trace("found it at index: " + i);
return i;
}
}
return -1; //If not found
}
Another way to do it:
var arr:Array = ["dogs", "cats", "oranges", "apples"];
function indexOfSubstring(array:Array, stringToFind:String):int
{
var answer:int = array.indexOf(stringToFind);
if (answer == -1)
{
var separator:String = "|";
var arrayToString:String = array.join(separator);
var indexInString:int = arrayToString.indexOf(stringToFind);
if (indexInString != -1)
{
var shorterStr:String = arrayToString.substring(0, indexInString);
answer = shorterStr.split(separator).length - 1;
}
}
return answer;
}
trace(indexOfSubstring(arr, "oran")); // returns 2
trace(indexOfSubstring(arr, "les")); // returns 3
trace(indexOfSubstring(arr, "ts")); // returns 1
trace(indexOfSubstring(arr, "broom")); // returns -1

Resources