0

I got an external JS file which includes JSON array and functions helps me to pick a random set of variables. When I tried to generate the result in the main HTML file, nothing showed in the p tag. Am I mismatching any objects or variables?

Short Example of HTML file in body section

<p id="result"></p>
<script src="myJSfile.js" type="text/javascript">
<script language="javascript" type="text/javascript">
document.getElementById("result").innerHTML = lottery(selected.name + selected.age);
</script>

Short Example of External JS file

function lottery() {
var from, to, selected=new Array();
var person=
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    if (arguments[0] == "all") {
      from=0;
      to=person.length;
    }
    else {
      from=Math.floor(Math.random()*3);
      to=Math.floor(Math.random()*3+1);
    }
    for (i=0; i<to; i++) {
        selected[i]=person[from];
        from=(from+1)%3;
    }
    return selected;
   }
0

2 Answers 2

2

your from, to and selected variables are not visible outside the function just bring this line var from, to, selected=new Array(); out of the function and it should work.

All

var from, to, selected = new Array();
lottery('all');
for(var k = 0; k < selected.length; k++){
  document.getElementById("result").innerHTML += selected[k].name + " " + selected[k].age+'<br />';
}

function lottery() {
var person =
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    
    if (arguments[0] == "all") {
      from = 0;
      to = person.length;
    }
    else {
      from = Math.floor(Math.random()*3);
      to = Math.floor(Math.random()*3+1);
    }
    
    for (i = 0; i < to; i++) {
        selected[i] = person[from];
        from = (from+1)%3;
    }
    
    return selected;
    
   }
<p id="result"></p>

Random

var from, to, selected = new Array();
lottery('all');
for(var k = 0; k < selected.length; k++){
  document.getElementById("result").innerHTML += selected[k].name + " " + selected[k].age+'<br />';
}

function lottery() {
var person =
    [
     {"name":"Georgia","age":"45"},
     {"name":"John","age":"38"},
     {"name":"Michael","age":"29"},
    ];
    
    if (arguments[0] == "alla") {
      from = 0;
      to = person.length;
    }
    else {
      from = Math.floor(Math.random()*3);
      to = Math.floor(Math.random()*3+1);
    }
    
    for (i = 0; i < to; i++) {
        selected[i] = person[from];
        from = (from+1)%3;
    }
    
    return selected;
    
   }
<p id="result"></p>

Sign up to request clarification or add additional context in comments.

6 Comments

Thx @Muhammad. I tried. It seems worked but I still could not get the value from JSON given. I excepted as [John 38] but it returned as [object Object]. Maybe there is still something wrong in the innerHTML part. Almost done anyway.
Try to get the value like selected[0].name
sadly it does not change anything... should I take away the blankets of lottery ?
@JoeyU Please see my answer I have added an example, if it solves the problem don't forget to mark it as accepted. :-)
AWESOME. it works! thx so much. So, adding an extra variable k to define selected is the key.
|
0

I have made some changes in your code, please check, this should work

<p id="result"></p>
<script src="stackoverflow.js" type="text/javascript">
<script>
 (function() {
   document.getElementById("result").innerHTML = lottery('all');
 })();
</script>

and in your external js

function lottery() {
var from;
var to; 
var selected=new Array();
var person=
[
 {"name":"Georgia","age":"45"},
 {"name":"John","age":"38"},
 {"name":"Michael","age":"29"},
];
if (arguments[0] == "all") {
  from=0;
  to=person.length;
}
else {
  from=Math.floor(Math.random()*3);
  to=Math.floor(Math.random()*3+1);
}
for (i=0; i<to; i++) {
    selected[i]=person[from];
    from=(from+1)%3;
}
return selected;
}

3 Comments

Thx @sanjay. It does shows somthing, not grabbing the exact value from JSON though. Shown as [object Object],[object Object] not [John 38] that thing. I think I got the direction.
@JoeyU its showing [object Object] because lottery function returns selected as object, so try to get value from it.. like selected[0].name. and if this answer solved your problem then please accept it
thx for the tips. I think that is the reason. selected[0].name is not working though. I checked with inserting the same set of JSON in HTML file directly and then something like person[0].name works properly...*sighs*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.