Solved

pick random nodes from xml doc

Posted on 2010-11-20
2
457 Views
Last Modified: 2012-05-10
I have a page with 3 spaces for information pulling from an xml file. My xml file has 20 nodes of different  information.
What can I put on my page in javascript or asp to pull a random different 3 each time the page loads?
0
Comment
Question by:rivkamak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Expert Comment

by:him6ul
ID: 34182169
You can write an algorithm using Random number implementation to give you three unique random numbers between 1 and 20 everytime you call that function.

Then you can pass those numbers to your processor which is processing XML node and it can give you the XML nodes data corresponding to those three numbers.
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34182171

Without knowing your xml structure, the following picks three values from an array:

<script>
// http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26629425.html

var nodes = ["one","two","three","four","five"
  ,"six","seven","eight","nine","ten"
  ,"eleven","twelve","thirteen","fourteen","fifteen"
  ,"sixteen","seventeen","eighteen","nineteen","twenty"];
var pick = Array();

while ( pick.length < 3 )
{
  var index = randomTo( 20 );
  while ( isIn( pick, index ) )
  {
    index = randomTo( 20 );
  }
  pick[pick.length] = nodes[index];
}
alert(pick);

function randomTo( modulo )
{
  return Math.floor( Math.random() * new Date().getMilliseconds() ) % modulo;
}

function isIn( array, value )
{
  result = false;
  for ( var i = 0; i < array.length; i++ )
  {
    if ( array[i] == value )
    {
       result = true;
       break;
    }
  }
  return result;
}
</script>

Open in new window

0

Featured Post

ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question