how to display all elements of an array in javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wish to print elements of the array below
var array = [1,2,3,4,5];
However, only 3 digits must be printed at a time, e.g,
step 1: should print 1,2,3
step 2: should print 2,3,4
step 3: should print 3,4,5
step 4: should print 1,2,3
step 5: should print 2,3,4
and the loop continues .....
I wish to display the thumbnails of this slider in a manner that they will fit a small screen.
http://www.tympanus.net/Tutorials/FullscreenSlideshowAudio
That means only a few thumbnails must appear at a time, while the rest are invisible for a short while.
Thanks..

As you wanted to display 3 elements from the array each time and when the last element is displayed you want to repeat the process again you are suppose to use setInterval() to call the function after a specified time continuously and reset the index to 0 immediately after the last element is displayed. Else increment the initial or counter value. Try this way,
var array = [1,2,3,4,5];
var initVal = 0;
setInterval(function(){
console.log(array[initVal] + "," + array[initVal+1] + "," + array[initVal+2]);
initVal = initVal == 2 ? 0 : initVal+1;
}, 500);
jsFiddle

Related

How to calculate all possible results in a 4x3 array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
assume that the array looks like this.
var res = [2,4,1,6,
1,1,2,8,
5,6,7,1];
all possible four quadrants combinations of this array are 81.
in this example we have only one combination on index : 4,5,2,11 which is four 1.
my question is how to calculate them.
thanks.
I'm not completely clear on what you're trying to achieve. Do you want to find every subset of length exactly 4 which contains all the same value? If so, you can do this in N^2 time with the following naive algorithm:
let quadrants = [];
res.forEach(checkElement => {
let possibility = [];
res.forEach((element, index) => {
if (element === checkElement) {
possibility.push(index);
}
});
if (possibility.length === 4) {
quadrants.push(possibility);
}
});
If you want to account for the possibility of the original array having more than 4 of the same number and include all subset quadrants, you'll need to change the length check to >=4 and add one more step at the end of this: calculate the power set of all listed quadrants with length greater than 4, filter out the ones that aren't length 4, and then concatenate those to the quadrants array. (You'll want to remove each quadrant with length >4 from the quadrants array before calculating its power set, so it won't be in the final result.)
If you do that, you may be able to optimize the last step by only calculating the subsets of length 4 of the longer quadrants; try using this as a guide for that if you need it: https://www.geeksforgeeks.org/print-subsets-given-size-set/

Using a while loop to print random numbers until a certain number is reached [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.

Count element selected in nested json [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have to count length of a jsonArray but i'm able to do it. This is the example from start:
https://jsfiddle.net/vuqcopm7/13/
Summary, if you click over an item of the list, for example "asd1" it will create an input text for each time you click that item or the others. What i need is count how many times that item is clicked, or better, how many input that item creates. Is it possible? For example instead:
asd1 (numberForEachItemSelected)
if i create 2 input, because i tap over it 2 times, it will be:
asd1 (2)
everything in angularjs
Updated Fiddle
I changed the push pushItems function to increment a 'clicks' key I added to 'attributes', then I bound that to the view:
$scope.pushItems = function pushItems(attribute, items) {
items.clicks++
//do stuff..
}
If you can't modify your model to add the 'clicks' key server side, adding this if statement will either add to the clicks key, or set it if it doesn't exist:
$scope.pushItems = function pushItems(attribute, items) {
if (items.clicks) {
items.clicks++
} else {
items.clicks = 1
}
// do stuff
}

Creating a table with a fixed number of rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Using Javascript, Html and BFO (Big Faceless Org) PDF Generator, I need to create a table with fixed number of rows for every page in a sales order. Basically, the table should consist of fixed 10 rows, however rows populated with information may only be 1 or 2 rows. The other rows will be empty.
Anyone can help?
Currently all I have is:
for(I=0;i<=salesitem.length;i++){
document.write('<tr><td>salesitem[I]</td></tr>');
}
That generates one td row for every sales item. However, I want a fixed 10 rows in the table.
There are several issues with the tiny piece of code you've supplied...
javascript is case sensitive, therefore I and i are different variables
looping from 0 to i<=salesitems.length will end in an error, because if length is 10 and you use salesitem[10] it will fail (arrays are 0-based, and therefore an array with length of 10 has items 0 to 9)
I believe you think that salesitem[I] will process like PHP, it won't. PHP allows you to use echo "print this $varName", javascript simply doesn't allow that.
Try something along these lines...
for (i = 0; i < salesitem.length; i++) {
document.write('<tr><td>' + salesitem[i] + '</td></tr>');
}
for (i = salesitem.length; i < 10; i++) {
document.write('<tr><td></td></tr>');
}
The first loop will display everything in your array (including any entries over 10)
The second loop will display lines to complete the table (if there are less than 10 entries)
Try this.
for (var i=0;i<10;i++) {
document.write("<tr>");
//This will depend entirely upon what conditions have to be met
//in order for you to display the content of each row
if(variable == 'condition') {
document.write("<td>content</td>");
}
document.write("</tr>");
}

Apply focus to one object, after 10 seconds apply focus to another object, is that possible? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to perform a set timeout function with this behavior:
Apply focus to one object, after 10 seconds apply focus to another object, is that possible?
So I want to called it like
focustimeout([objectsarray])
where objectsarray is an array with all the id names for the N fields that I want to have a 10 second focus before each other.
I'm sorry for the question but in really new on javascript.
What can I add to this in order to get that solution
var temp = "id1";
var temp1 = "id2";
setTimeout(function(){
$("#"+temp).focus()
setTimeout($("#"+temp1).focus(), 10);
}, 10);
It's possible. Here is an idea how you can do it. Create function with 2 parameters, number of focused element and array of IDs which you want to focus.
function focus(i,array){
$("#"+array[i]).focus();
if(i<array.length){
setInterval(function(){focus(i++,array),yourTime);
}
Then call it from beginning focus(0,array)

Categories