I am trying to build a quiz which shows one question per slide dynamically. I wanted to let remain the selected answers as is when I click on next/previous question. I tried the following code. This is is returning NaN when trying to get the values from the array also, the length of the array is increasing even I go forward and backward among the slides. Can any one please help -
function choose() {
console.log("selection -"+$('input[name="answer"]:checked').val())
selections[questionCounter] =+ $('input[name="answer"]:checked').val();
console.log("selections - "+selections[questionCounter])
}
full code here - https://jsfiddle.net/fqrhuo23/
instead of using
selections[questionCounter] =+ $('input[type="radio"]:checked').val();
You need to use
selections[questionCounter] = $('input[type="radio"]:checked').val();
Since you already initiated selections as []. selections[questionCounter] = $('input[type="radio"]:checked').val(); will create corresponding array index for you. When you are using + in front of the value, which is trying to cast $('input[type="radio"]:checked').val() to a number. You are trying to cast a string type to number. That's why you are getting NaN.
console.log(+"a");
Related
Hi Helpful Contributors,
I have a gform where user can select more than 1 answer. In response sheet, we will see the multiple answers will be printed in one cell of a column separated by comma. For that matter, I have some calculation to do on each answer. So, I was thinking to declare each input separated by comma as an array value so that I can then refer each value by it's index number in that array. Is this possible?
Below is my code that I tried but when I tried to refer back to that array on index[0], the output is still all the values in that same cell, so I think all are still kept as 1 value of array.
function mytest(){
var sheet=SpreadsheetApp.getActiveSheet();
var input=[];
var extraitem=sheet.getRange(lastorder,77).getValue(); //this cell keeps the multiple answers : "Change of address, Change of mobile no., Change of vehicle type, Change of license type"
input.push(extraitem.split(','));
Logger.log("myinput :"+input[0]); // check the value in position 0 is the first among the multiple answers
}
Please correct my code. Thank you in advance.
The issue with your code is that you push an array extraitem.split(',') into another array input=[]. As a result, input[0] is the full array extraitem.split(',').
To get the first element of the extraitem.split(',') array you can do Logger.log(input[0][0]) or (preferably) simply ignore the push part:
function mytest(){
var sheet=SpreadsheetApp.getActiveSheet();
var extraitem=sheet.getRange(lastorder,77).getValue();
var input= extraitem.split(',');
Logger.log("myinput :"+input[0]);
}
Demonstration:
const extraitem = "Change of address, Change of mobile no., Change of vehicle type, Change of license type";
const input = extraitem.split(',');
console.log("myinput :" + input[0]);
I try to compare the current line item index with the line number and see if they are the same with a new sales order. If the two numbers are the same then skip the validation otherwise check if they are the same item. However, it doesn't always return the line number which I wonder why.
Here are the two variables that I want to compare:
var linenum= nlapiGetCurrentLineItemValue('item','line');
var currentIndex = nlapiGetCurrentLineItemIndex('item');
linenum does return a number sometimes but most of the time it returns null; when that happened the comparison doesn't work. When I logged the variables, index always shows up correct. Is there any other parameter I can use to grab information from creating sales order page?
Any idea/suggestion would be appreciated!
var linenum= nlapiGetCurrentLineItemValue('item','line');
linenum will only have a value if the line was saved previously. If linenum is null it means the user is trying to add a new Line item and not edit.
I'm am an all around new coder and trying to learn React by building a small app that calculates how much cash one has (an app my kids can use). I am dynamically creating 5 components, each with an input field. Those input fields accepts an entry (a number) and multiplies that input by the fixed amount that I am mapping out from an object ($20, $10, $5, $2 and $1 fixed amounts) for each of the 5 components. That part works great. $10 x 2 is showing me $20.
However, to add up all 5 of the bill amount totals as they are keyed in, I was thinking I need to put them into an array and then get the sum of the array to show the final total. Things aren't working and I've been searching this past week for a way to code my onChange handler. I'm just flat out stuck. Any help would be so greatly appreciated. I am very sorry for any hard to read code.
Here's the jsfiddle (please excuse the lack of styling)
I was able to find the error easily by opening the javascript console (F12 or right click -> inspect).
The error was thrown because you did pass handleCashInput as a prop to CashItemsList
<CashItemsList key={bill.id} bill={bill} onChange={this.handleCashInput} />
Also using an array with reduce was not a good idea because if you delete an amount and re-enter it, the total will be wrong. I did the following to solve it:
I use an object instead of an array
{ 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
I pass the bill id to handleCashInput to be able to change the amount this.props.onChange(this.props.bill.id, newBillSubTotal)
I update the correct bill value in the object like this
const cashTemp = this.state.billFinalTotal;
cashTemp[id] = newBillFinalTotal;
I calculated the total by adding every value of the object together
const bt = this.props.billFinalTotal;
const total = bt[1] + bt[2] + bt[3] + bt[4] + bt[5];
Here's my JSFiddle
You had two issues in your fiddle.
First, you didn't pass an onChange prop to CashItemsList from Parent. So I changed:
<CashItemsList key={bill.id} bill={bill} />
to:
<CashItemsList key={bill.id} bill={bill} onChange={this.handleCashInput} />
The second issue is that the return of array.push() is not the full array, so in your "handleCashInput()" method on Parent, using array.concat() instead of array.push() solved that issue, by changing this:
const cashTemp = this.state.billFinalTotal.push(newBillFinalTotal)
to:
const cashTemp = this.state.billFinalTotal.concat(newBillFinalTotal)
Here's an updated fiddle that works: https://jsfiddle.net/ggtu71gh/1/
Note: it doesn't seem that you've implemented the subtotal functionality so this only attempts to fix the errors in what you've already implemented.
I face a problem when tried to assign a value with a specific index. suppose I have javascript variable like
var track = new Array();
Now I assign a value for a specific index like-
track[10]= "test text";
now array has one value and it's length would be 1. But the main problem is it show it's length is 11.
alert(track.length); // 11 but I expect 1
and if I print its value then it shows like--
alert(track); // ,,,,,,,,,test text
and if I console this array then it show like below--
console.log(track); // undefined,undefined,undefined,undefined,.....,test text
I am very much confused because I assign only one value but it show 11. How it assign it's value and what characteristics array variable shows. Can anyone explain me and how to get its length 1 using below code--
var track = new Array();
track[10]= "test text";
alert(track); // test text
alert(track.length); // 1
console.log(track); // test text
The Array object automatically fills in the missing indexes. The reason it gives length 11 is because the index starts at 0.
If you are wanting to use key-value just use an object.
var track = {};
It will not have a .length value however.
javascript automatically fills in the array to push the element you want. You can get the "true" count by doing this:
track.filter(Boolean).length
However note that this will only "work" if you do not have any other elements that resolve to "false" value (eg. empty strings or setting them to false) so if you want to this, make sure you never actually set any other array elements to a falsy value so that you can use this convention. For example if you want to set other array values to a falsy value, use something like -1 instead as the thing to check.
Since you're setting the value for 10th position it's showing array size of 11
You must start from 0th position..
var track = new Array();
track[0]= "test text";
alert(track); // test text
alert(track.length); // 1
console.log(track); // test text
Try this
For such kind of operations i generally prefer the library called Underscore.js.
It abstracts array manipulations. You might want to checkout the compact method
Compact works like this:
_.compact([undefined, undefined, undefined, "test test"]) as ["test test"]
Then you can check the length of the returned array.
Though a simple approach is
filter(Boolean).length
But if you want to use the array then you might like underscore.
I need help with a loop... it's probably simple but I'm having difficulty coding it up.
Basically, I need to check existing Ids for their number so I can create a unique id with a different number. They're named like this: id="poly'+i'" in sequence with my function where i is equal to the number of existing elements. Example: Array 1, Array 2, Array 3 corresponding with i=1 for the creation of Array 1, i=2 for Array 2, etc.
Right now i is based on the total number of existing elements, and my "CreateNew" function is driven off x=i+1 (so the example above, the new element will be named Array 4). The problem is that if you delete one of the middle numbers, the "Create" function will duplicate the high number. i.e. Array 1, 2, 3 delete 2, create new-> Array 1, 3, 3.
I need an if() statement to check if the array already exists then a for() loop to cycle through all i's until it validates. Not sure how to code this up.
The code I'm trying to correct is below (note I did not write this originally, I'm simply trying to correct it with my minimal JS skills):
function NewPanel() {
var i = numberOfPanels.toString();
var x = (parseInt(i)+1).toString();
$('#items').append('<div onclick="polygonNameSelected(event)" class="polygonName" id="poly'+i+'"> Array '+ x +' </div>');
$('div[id*=poly]').removeClass('selected');
$('#poly'+i).addClass('selected');
$('#poly'+i).click(function() {
selectedPolygon = i;
$('div[id*=poly]').removeClass('selected');
$(this).addClass('selected');
});
}
THANK YOU! :)
Please clarify "The problem is that if you delete one of the middle numbers, ". What do you mean by delete? Anyway, the simplest solution is to create two arrays. Both arrays will have the same created id's. Whenever an id is created in the first array, an id will be added to the second array. So when it is deleted from first array, check your second array's highest value and then create this id in first array. I hope this did not confuse you.
Well it is hard to tell why you cannot just splice the array down. It seems to me there is a lot of extra logic involved in the tracking of element numbers. In other words, aside from the index being the same, the ids become the same as well as other attributes due to the overlapping 1, 3, 3 (from the example). If this is not the case then my assumption is incorrect.
Based on that assumption, when I encounter a situation where I want to ensure that the index created will always be an appending one, I usually take the same approach as I would with a database primary key. I set up a field:
var primaryKeyAutoInc = 0;
And every time I "create" or add an element to the data store (in this case an array) I copy the current value of the key as it's index and then increment the primaryKeyAutoInc value. This allows for the guaranteed unique indexing which I am assuming you are going for. Moreover, not only will deletes not affect future data creation, the saved key index can be used as an accessor.