i want to display questions and below i want to put a textbox for answer. for eg. What is the capital of India.. When the page loads at first time this question appears.. when loads second time another question should appear randomly how to write.. please help me
If you have an array of questions, var questions = ["asdf", "sdfg", ...]; then you can get random ones by:
questions[Math.floor(Math.random() * questions.length)];
Edit: For associative arrays...
Just create a normal array from it:
var questions = [];
for (var i in assocArr) {
if (assocArr.hasOwnProperty(i)) questions.push(assocArr[i]);
}
And then use the above method.
Related
I'm having issues in pushing and returning an array with the events titles. As far as i'm concerned, this should append the title of the events into the titulos array.
for (j=0;j<events.length;j++){
var titulos = []
var events =a.getEventsForDay(testingstartdate, {search: 'OOO'});
var eventstitle = events[j].getTitle();
Logger.log(eventstitle);
titulos.push(eventstitle);
};
The Logger.log in question here is returning correctly one row per title, so no sure why the final array is only pushing 1 single value to it.
Any ideas?
The Logger.log in question here is returning correctly one row per
title, so no sure why the final array is only pushing 1 single value
to it.
This is happening because you define your array in each step of the for loop. Moving the following statement:
var titulos = [];
before the start of the for loop will solve your problem.
I have a variable containing an array of answers looking like this:
var answers = [
{"answerId":5,"text":"<p>xx</p>","correct":null,"response":true},
{"answerId":6,"text":"<p>yy</p>","correct":null,"response":false},
{"answerId":7,"text":"<p>zz</p>","correct":null,"response":false},
{"answerId":8,"text":"<p>aa</p>","correct":null,"response":false},
{"answerId":9,"text":"<p>bb</p>","correct":null,"response":false},
{"answerId":21,"text":"<p>cc</p>","correct":null,"response":false}];
and another variable containing an array of responses:
var reply = [
{"answerId":5,"correct":true},
{"answerId":6,"correct":false},
{"answerId":7,"correct":false},
{"answerId":8,"correct":false},
{"answerId":9,"correct":false},
{"answerId":21,"correct":false}];
How can I update the answers variable with the correct responses for each answerId ? So that the null's are replaced?
If we know that the indexes and answers are the same (otherwise we can sort them). A simple for loop can handle this.
for(var i=0;i<answers.length;i++){
answers[i].correct = reply[i].correct;
}
Fiddle
I have this collection of images resources where that is stored in array, the user will select an image and then the selected image will be removed from the list(also from the array) and after that The array would be rearrange. How could I perform such task? (as much as possible I do not want to use an open source library)
Sounds like you need to look up splice() method. It allows you to add and remove one to many items within an array at any index.
here's reference for it.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice
your question lacks a code example but you can use Array.splice(index,number) whereas index is zero based and number is how many items to remove.
images.splice(selectedIndex,1);
Simply, you can create a temporary array where you store the initial array elements you need and reassign the value of your initial array to the temporary array.
function clean_array(my_array){
var no_need_value = 'value you want to remove'
var tmpArray = new Array()
for (var i = 0; i < my_array.length; i++)
if (my_array[i] != no_need_value)
tmpArray.push(my_array[i])
my_array = tmpeArray
}
I have four product gallery with the possibility to check the detail of each product from each gallery. Every time the user enter in the detail of any product an array is filled with all the products from that gallery. I should tell this is one single page.
I want to completely delete all the records in the array when the user exits the detail view, because when the user enter again in the detail view the array length increments his size.
I already tried arrayName.length =0; and arrayName.length = [] and it deleted the previous data, but his size continue incrementing like this:
1st time detail view is loaded --> arrayName{valA,valB,valC}
2nd time detail view is loaded --> arrayName{,,,val1,val2,val3}
what it's supposed to be in the 2nd time is: arrayName{val1,val2,val3}
Any idea in how I can solve this issue??
Thanks all
The solution
Thanks all guys. It was my problem.
while($rowDetails = mysql_fetch_array($rsDetails)){
?>
<script language="javascript">
arrayProd[pos] = <?php echo $rowDetails['RefArticleID']; ?>;
pos++;
</script>
...
The array is filled into a while cycle and it increments the pos. At the same time I re-initialize the array I force the pos to be 0 (zero)
arrayProd = [];
pos =0;
Thank you all
This one is really simple, just re-initialize the array:
arrayName = [];
Did you try re-initializing the array by setting it to an empty array?
arrayName = [];
I already tried arrayName.length =0;
That should work, except perhaps for browsers that don't comply with ECMA-262 ed 3 — which shoul be very, very few. In which browser does it fail for you?
and arrayName.length = []
Presumably you meant (otherwise you would see an error):
arrayName = [];
which should work without exception.
It might be a silly question but still i am facing problem with this.
var eformDetailIds = [];
eformDetailIds=$("[name=eform_id]").map(function(){ return $(this).val() }).get();
this is the code that i have written in js function and calling this function on button click.
But the problem is the list eformDetailIds containing the previous values also. could you please how to set this empty list for every function call? Thanks in advance.
Just set the length to zero:
eformDetailIds.length = 0;
Or allocate a new array:
eformDetailIds = [];
Now, that said, according to the code you posted the entire array will definitely be replaced each time that ".map()" call runs. In other words, the previous values will not remain in the array. Perhaps you should post more to explain what it is that makes you think the old values remain.
Don't forget you can always reset the array in this way:
myArray = new Array();
It is pretty easy.