If and else both executed in nested loop - javascript

In google apps spreadsheet I am parsing json and filling up cells. After seting value to cell I check if the row has this value and if it does I go to the next cell and set new value, then I check the second value and so on. But something goes wrong and the if and else are both executing..
var customfields = SpreadsheetApp.openById('id').getSheetByName('Form').getRange('J25').getValue();
if (customfields == true) {
var jj = 2;
var ii = 1;
for each(item in data) {
var size = Object.keys(data[k].customfields).length;
if (size == 0) {
k = k + 1;
continue;
}
Logger.log(k)
for (j = 0; j < size; j++) {
var vl = data[k].customfields[j].value;
var nm = data[k].customfields[j].name;
var rng = SpreadsheetApp.openById('id').getSheetByName('Data').getMaxColumns();
for (var cn = 0; cn <= rng; cn++) {
var dat = sheet.getRange(1, cn).getValue();
Logger.log("IF STATEMENT")
if (dat == nm) {
sheet.getRange(ii, cn).setValue(nm);
} else {
sheet.getRange(ii, br).setValue(nm);
}
}
Logger.log(nm)
br = br + 1;
}
k = k + 1;
}
}

Related

how to send data to backend in key value structure using json

var idd ;
var subjects = [];
var ctr = 0;
var teacher = $('#selectTeacher option:selected').text();
for(var j = 0 ; j < lstClasses.length ; j++)
{
var sub = {};
var key = lstClasses[j];
for(var i = 0 ; i < lstSubjects.length ; i++)
{
var id = lstClasses[j] + lstSubjects[i];
var cls = $('#'+id).attr('class');
if(cls.indexOf('tickimage') >= 0)
{
if(ctr == 0)
{
var val = [];
}
ctr = ctr + 1;
idd = id.replace(lstClasses[j], "");
val.push(idd);
/* console.log(val); */
sub[key] = val;
}
/* console.log(sub); */
subjects.push(sub);
}
}
the data that i'm sending is not going to backend properly in the json format.i just want to send particular class data and its subjects but when sending the data its not going properly

how do i get non-repeated character and its count in JavaScript?

Here is my code. What should I modify of this code to get the output as
"T-1
r-1
a-1
e-1 "
(other characters are repeating. So no need to print the others)
function different() {
var retureArr = [];
var count = 0;
var complete_name = "Trammell";
var stringLength = complete_name.length;
for (var t = 0; t < stringLength; t++) {
for (var s = 0; s < stringLength; s++) {
var com1 = complete_name.charAt(t);
var com2 = complete_name.charAt(s);
if (com1 != com2) {
retureArr[count] = com1;
count++;
}
}
count = 0;
}
}
I think this is what you want. You need to count the number of occurrences of each character in a dictionary. Then you can print them based on the count being equal to 1.
var retureArr = [];
var complete_name = "Trammell";
for (var i = 0; i < complete_name.length; i++)
{
var key = complete_name[i];
if (!(key in retureArr))
{
retureArr[key] = 1;
}
else
{
retureArr[key] = retureArr[key] + 1;
}
}
var output = "";
for (var key in retureArr)
{
if (retureArr[key] == 1)
{
output += key + "-" + retureArr[key] + " ";
}
}
alert(output);
This alerts the following string:
T-1 r-1 a-1 e-1
This works. but perhaps isn't the most efficient!
var string = "input string";
var stringList = [];
var outputString = "";
for (var i=0; i < string.length; i++){
var charObject = {"Char": string.charAt(i), "Passed": false};
stringList.push(charObject);
}
for (var i=0; i < stringList.length; i++){
if(!stringList[i].Passed && stringList[i].Char != " "){
var currentCount = countOccurrences(string, stringList[i].Char);
if(currentCount == 1){
outputString += stringList[i].Char+"-"+currentCount + " ";
}
stringList[i].Passed = true;
}
}
console.log(outputString);
function countOccurrences(string, char){
var count = 0;
for (var i=0; i < string.length; i++){
if(string.charAt(i) == char){
count++;
}
}
return count;
}

Protractor:How to store values in array and then to do sorting

I need to sort list strings under the table ,so for that i have written following lines of code but on console i am not getting any values:
var j = 9;
var rows = element.all(by.repeater('row in renderedRows'));
var column = element.all(by.repeater('col in renderedColumns'));
expect(rows.count()).toEqual(5); //here its printing number of rows
expect(column.count()).toEqual(5); //here its printing number of columns
var arr = [rows.count()];
for (var i = 0; i < rows.count(); i++) {
console.log("aai" + i);
if (i = 0) {
//var columnvalue=column.get(9).getText();
var columnvalue = column.get(9).getText().then(function(ss) {
return ss.trim();
arr[i] = ss.trim(); //here it will save the value first value of column
console.log("value1" + arr[i]);
expect(arr[i]).toEqual('DN');
console.log("aa" + ss.trim());
});
} else {
var j = j + 8;
var columnvalue = column.get(j).getText().then(function(ss) {
return ss.trim();
arr[i] = ss.trim(); //here it will save the other values of column
console.log("value" + arr[i]);
expect(arr[i]).toEqual('DN');
console.log("ab" + ss.trim());
});
}
}
Sorting_Under_Table: function(col){
test = [];
var m;
var dm = 0;
element(by.xpath('//div[#class="ngHeaderScroller"]/div['+col+']')).click();
element.all(by.repeater('row in renderedRows')).then(function(row) {
m = row.length;
for (i = 1; i <= row.length; i++)
{
user_admin_table_name = browser.driver.findElement(by.xpath('//div[#class="ngCanvas"]/div['+i+']/div['+col+']'));
user_admin_table_name.getText().then(function(text) {
var test_var1 = text.toLowerCase().trim();
test.push(test_var1);
var k = test.length
if (k == m){
for (j = 0; j < test.length; j++){
test.sort();
d=j+1;
user_admin_table_name1 = browser.driver.findElement(by.xpath('//div[#class="ngCanvas"]/div['+d+']/div['+col+']'));
user_admin_table_name1.getText().then(function(text1) {
var test_var2 = text1.toLowerCase().trim();
if (test_var2 == test[dm]){
expect(test_var2).toEqual(test[dm]);
dm = dm +1;
}else {
expect(test_var2).toEqual(test[dm]);
log.error("Sorting is not successful");
dm = dm +1;
}
});
}
}
});
}
});
},
You can use this code for sorting and verifying is it sorted or not
I'm not sure how your above example is doing any sorting, but here's a general solution for trimming and then sorting:
var elementsWithTextToSort = element.all(by.xyz...);
elementsWithTextToSort.map(function(elem) {
return elem.getText().then(function(text) {
return text.trim();
});
}).then(function(trimmedTexts) {
return trimmedTexts.sort();
}).then(function(sortedTrimmedTexts) {
//do something with the sorted trimmed texts
});

Sum ranged values

I am trying to sum the ranged values but for some reason it's not summing. Any idea why?
I am not a very good in JavaScript but I have some knowledge...
function bag(input) {
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var AllValues = sheet.getRange(input).getValues();
var FValue = "";
var TotalGold = 0;
var TotalSilver = 0;
var TotalBronze = 0;
for(i = 0; i < AllValues.length; i++) {
var total = AllValues[i][0].split(" ");
for(i = 0; i < total.length; i++){
if (total[i].indexOf("g") >= 0) {
var value = total[i].replace("g","");
var gold = value * 1;
TotalGold = TotalGold + gold;
}
else if (total[i].indexOf("s") >= 0) {
var value = total[i].replace("s","");
var silver = value * 1;
TotalSilver = TotalSilver + silver;
}
else if (total[i].indexOf("b") >= 0) {
var value = total[i].replace("b","");
var bronze = value * 1;
TotalBronze = TotalBronze + bronze;
}
else {
return null;
}
}
}
return TotalGold + "g " + TotalSilver + "s " + TotalBronze + "b";
}
It appears you are overwriting your value of i in the nested iteration. Pick another identifier. E.g: for (var j = 0; j < total.length; j++) { ... }

avoid sorting in the JSP pages

var sortitems = 1;
function move(fbox, tbox, all)
{
for ( var i = 0; i < fbox.options.length; i++)
{
if (!all && fbox.options[i].selected && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
else
{
if (all && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
}
BumpUp(fbox);
if (sortitems)
SortD(tbox);
checkSelectAll();
}
This move function is getting called after clicking on the button, then it will call the sort method where sorting is happening by alphabetically. So we dont need to sort we need to populate the data as it is from the left side box to right side box and vice versa, but sorting is happening. Please help out be here.
function SortD(box)
{
var temp_opts = new Array();
var temp = new Object();
for ( var i = 0; i < box.options.length; i++)
{
temp_opts[i] = box.options[i];
}
for ( var x = 0; x < temp_opts.length - 1; x++)
{
for ( var y = (x + 1); y < temp_opts.length; y++)
{
if (temp_opts[x].value > temp_opts[y].value)
{
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
}
}
}
for ( var i = 0; i < box.options.length; i++)
{
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
}
}
Depends on the bumpup box function. The elements are moving from one box to another. It will replace the element with empty space and move to top and do for all the elements. Please help out me here
Thanks in advance
function BumpUp(box)
{
for ( var i = 0; i < box.options.length; i++)
{
if (box.options[i].value == "")
{
for ( var j = i; j < box.options.length - 1; j++)
{
box.options[j].value = box.options[j + 1].value;
box.options[j].text = box.options[j + 1].text;
}
var ln = i;
break;
}
}
if (ln < box.options.length)
{
box.options.length -= 1;
BumpUp(box);
}
}
Maybe it's just me, but it's hard to see what the issue is here.
If it is simply that SortD(tbox) is being called within the move() function, that's because
sortitems is set to 1 right at the top of the code. The value of sortitems is never changed anywhere else, so this conditional is always true and SortD is always called.
if (sortitems)
SortD(tbox);

Categories

Resources