11 Replies - 406 Views - Last Post: 03 November 2017 - 07:28 AM

#1 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Math.Random error

Posted 03 November 2017 - 05:55 AM

Hey guys, I really suck at coding. Trying to understand arrays a little bit more but I can never get them to work for me. I'm getting an undefined error here, I think maybe the math is wrong? I can't figure it out, just wondering if anyone had any ideas.


<html>
<head>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <p id="parQuest"></p>
    <input id="btnStart" type="button" onclick="btnStart_onclick()" value="Start" />
    <input id="txtAns" type="text" />
    <input id="btnCheck" type="button" onclick="btnCheck_onclick()" value="Check"/>
    <p id="txtCheck"></p>
    <script>    
    var Country = new Array(4);
    var City = new Array(4);
    var Num;

    function window_onload(){
    Country[1] = "UK";
    City[1] = "London";
    Country[2] = "France";
    City[2] = "Paris";
    Country[3] = "Spain";
    City[3] = "Madrid";
    Country[4] = "Greece";
    City[4] = "Athens";
    }

    function btnStart_onclick(){
    Num = 1 + parseInt(Math.random() * 3);
    parQuest.innerText = "What is the capital of " + Country[Num] + "?";
    }
        function btnCheck_onclick(){
    if (txtAns.value = City[Num]){;
        txtCheck.innerText = "Correct!"
    }else{
        txtCheck.innerText = "Sorry, try again"
    }
    }
    </script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Math.Random error

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Math.Random error

Posted 03 November 2017 - 06:03 AM

Arrays are 0-indexed, not 1-indexed, so

Num = parseInt(Math.random() * Country.length);
parQuest.innerText = "What is the capital of " + Country[Num] + "?";


And obviously assign values to indices 0 to Country.length - 1
Was This Post Helpful? 1
  • +
  • -

#3 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 06:06 AM

View Postg00se, on 03 November 2017 - 06:03 AM, said:

Arrays are 0-indexed, not 1-indexed, so

Num = parseInt(Math.random() * Country.length);
parQuest.innerText = "What is the capital of " + Country[Num] + "?";


And obviously assign values to indices 0 to Country.length - 1


I'm really sorry. I don't understand what you are saying. What is 0 indexed? What do you mean "assign value to indices 0 to Country.length - 1?
Was This Post Helpful? 0
  • +
  • -

#4 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 06:17 AM

View Postohgod, on 03 November 2017 - 06:06 AM, said:

View Postg00se, on 03 November 2017 - 06:03 AM, said:

Arrays are 0-indexed, not 1-indexed, so

Num = parseInt(Math.random() * Country.length);
parQuest.innerText = "What is the capital of " + Country[Num] + "?";


And obviously assign values to indices 0 to Country.length - 1


I'm really sorry. I don't understand what you are saying. What is 0 indexed? What do you mean "assign value to indices 0 to Country.length - 1?


So I seem to understand that 0 indexed means they are ordered from 0 onwards, 0, 1, 2, 3. While I have done it 1, 2, 3, 4. I think I understand that.

But you want me to assign values to indices 0 to country.length -1? I don't understand that at all. I barely know what indices are. Is country.length is length of that string? I don't know why I'd ever subtract 1 from that. I don't know why you're subtracting 1 at all. I feel like an idiot
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Math.Random error

Posted 03 November 2017 - 06:56 AM

Quote

So I seem to understand that 0 indexed means they are ordered from 0 onwards, 0, 1, 2, 3.

Correct. But you're not doing that here:

Quote

Country[1] = "UK";

etc.

Country[4] doesn't exist
Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: Math.Random error

Posted 03 November 2017 - 06:58 AM

If you have an array with indexes 0,1,2,3 then that array has a length of 4, right? So it goes from 0 to 4-1, or 0 to 3, correct?
Was This Post Helpful? 0
  • +
  • -

#7 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 07:00 AM

View PostCTphpnwb, on 03 November 2017 - 06:58 AM, said:

If you have an array with indexes 0,1,2,3 then that array has a length of 4, right? So it goes from 0 to 4-1, or 0 to 3, correct?


Correct. So I have changed that. I am still getting "what is the capital of undefined". Is the because the math is wrong?

<!DOCTYPE html>
<html>
<head>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <p id="parQuest"></p>
    <input id="btnStart" type="button" onclick="btnStart_onclick()" value="Start" />
    <input id="txtAns" type="text" />
    <input id="btnCheck" type="button" onclick="btnCheck_onclick()" value="Check"/>
    <p id="txtCheck"></p>
    <script>    
    var Country = new Array(4);
    var City = new Array(4);
    var Num;

    function window_onload(){
    Country[0] = "UK";
    City[0] = "London";
    Country[1] = "France";
    City[1] = "Paris";
    Country[2] = "Spain";
    City[2] = "Madrid";
    Country[3] = "Greece";
    City[3] = "Athens";
    }

    function btnStart_onclick(){
    Num = 1 + parseInt(Math.random() * 3);
    parQuest.innerText = "What is the capital of " + Country[Num] + "?";
    }
        function btnCheck_onclick(){
    if (txtAns.innerText == City[Num]){;
        txtCheck.innerText = "Correct!"
    }else{
        txtCheck.innerText = "Sorry, try again"
    }
    }
    </script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 07:00 AM

View PostCTphpnwb, on 03 November 2017 - 06:58 AM, said:

If you have an array with indexes 0,1,2,3 then that array has a length of 4, right? So it goes from 0 to 4-1, or 0 to 3, correct?


Correct. So I have changed that. I am still getting "what is the capital of undefined". Is the because the math is wrong?

<!DOCTYPE html>
<html>
<head>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <p id="parQuest"></p>
    <input id="btnStart" type="button" onclick="btnStart_onclick()" value="Start" />
    <input id="txtAns" type="text" />
    <input id="btnCheck" type="button" onclick="btnCheck_onclick()" value="Check"/>
    <p id="txtCheck"></p>
    <script>    
    var Country = new Array(4);
    var City = new Array(4);
    var Num;

    function window_onload(){
    Country[0] = "UK";
    City[0] = "London";
    Country[1] = "France";
    City[1] = "Paris";
    Country[2] = "Spain";
    City[2] = "Madrid";
    Country[3] = "Greece";
    City[3] = "Athens";
    }

    function btnStart_onclick(){
    Num = 1 + parseInt(Math.random() * 3);
    parQuest.innerText = "What is the capital of " + Country[Num] + "?";
    }
        function btnCheck_onclick(){
    if (txtAns.innerText == City[Num]){;
        txtCheck.innerText = "Correct!"
    }else{
        txtCheck.innerText = "Sorry, try again"
    }
    }
    </script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: Math.Random error

Posted 03 November 2017 - 07:13 AM

Where do you call window_onload()? Functions do nothing unless they're called.

By the way, parallel arrays make for messy code. I'd use a single array of objects:
function tryThisTest() {
	var cityInfo = [{country:"UK", city: "London"}, {country:"France", city:"Paris"}];
	//for(var i in cityInfo) { // <--- Alternative loop
	for(var i = 0; i < cityInfo.length; i++) {
		console.log(cityInfo[i].city + ", "+ cityInfo[i].country);
	}
}


This post has been edited by CTphpnwb: 03 November 2017 - 07:14 AM

Was This Post Helpful? 1
  • +
  • -

#10 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 07:17 AM

View PostCTphpnwb, on 03 November 2017 - 07:13 AM, said:

Where do you call window_onload()? Functions do nothing unless they're called.

By the way, parallel arrays make for messy code. I'd use a single array of objects:
function tryThisTest() {
	var cityInfo = [{country:"UK", city: "London"}, {country:"France", city:"Paris"}];
	//for(var i in cityInfo) { // <--- Alternative loop
	for(var i = 0; i < cityInfo.length; i++) {
		console.log(cityInfo[i].city + ", "+ cityInfo[i].country);
	}
}


I'm really bad at this. Thanks for your time and help.
Was This Post Helpful? 0
  • +
  • -

#11 ohgod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 17

Re: Math.Random error

Posted 03 November 2017 - 07:24 AM

View PostCTphpnwb, on 03 November 2017 - 07:13 AM, said:

Where do you call window_onload()? Functions do nothing unless they're called.

By the way, parallel arrays make for messy code. I'd use a single array of objects:
function tryThisTest() {
	var cityInfo = [{country:"UK", city: "London"}, {country:"France", city:"Paris"}];
	//for(var i in cityInfo) { // <--- Alternative loop
	for(var i = 0; i < cityInfo.length; i++) {
		console.log(cityInfo[i].city + ", "+ cityInfo[i].country);
	}
}



I did actually manage to get it slightly working with your help. I didn't know I could call functions inside the body tag. None of this makes real sense to me and I'm stressing myself out. Thanks again for your help.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: Math.Random error

Posted 03 November 2017 - 07:28 AM

Start smaller, and try different things until you understand them. Then add a little more.

Did you try my function? Did you try it with the alternative loop? Did you try adding information to the array? You might try adding another city and country, or add area/postal codes to the objects and output them too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1