Jump to content

Putting the result of a function into the document as text


Sansespere

Recommended Posts

Well, let's find out why it's not working. Alert the values of d and sides and see if they're doing anything:

links.onclick = function rolld() { var sides = this.id.match(/[0-9]+$/); var d=(Math.floor(Math.random()*Number(sides)) + 1); document.getElementById("d"+ sides +"result").innerHTML = d; alert(d + " " + sides); If (d/sides <= .25 && d/sides >0) { document.getElementById("d"+ sides +"result").style = "color: red"; } return false; }}
Hmm, I'm going to have to go and make this script myself and test it if that doesn't help.
Link to comment
Share on other sites

Ok, I did that and the alert didn't run at all. When I take the if out it runs and tells me the 3 4 or something.

Link to comment
Share on other sites

Hmm, try changing the if condition a bit, like this:

X = d/sides;If (X <= .25 && X >0) {  try {	document.getElementById("d"+ sides +"result").style = "color: red";  } catch (er) {	alert(er.description);  }}

Link to comment
Share on other sites

EDIT: The "i" in If was capitalized, the function works when it is lowercased with this code, however the catch error is spitting out undefined when the if statement comes up.Nothing is running still, not even an error description. Here is the code just incase everything isn't in syntax, but i'm pretty sure it is

function init () { var links = document.getElementsByTagName("a"); for (var i = 0; links[i]; i++) {   links[i].onclick = function rolld() {   var sides = this.id.match(/[0-9]+$/);    var d=(Math.floor(Math.random()*Number(sides)) + 1);   document.getElementById("d"+ sides +"result").innerHTML = d;   var X = d/sides;   If (X <= .25 && X > 0) {	try {	 document.getElementById("d"+ sides +"result").style = "color: red";	}	catch (er) {	 alert(er.description);	}   }   return false;  } }}window.onload = init;

Link to comment
Share on other sites

Ok, the javascript tells me when the if statement is true: "d20result" when i hit the d20 link...et cetera....for the others..

Link to comment
Share on other sites

OK THAT WORKS! :) I didn't realize you could put so many something.something.something.something.something = whatever Thank-you, Ingolme! You are great!

Link to comment
Share on other sites

function init () { var links = document.getElementsByTagName("a"); for (var i = 0; links[i]; i++) {  links[i].onclick = function rolld() {   var sides = this.id.match(/[0-9]+$/);    var d=(Math.floor(Math.random()*Number(sides)) + 1);   document.getElementById("d"+ sides +"result").innerHTML = d;   var x = d/sides;   if (x <= .25 && x > 0) {	document.getElementById("d"+ sides +"result").style.color = "#00d800";   }   else if (x > .25 && x <= .50) {	document.getElementById("d"+ sides +"result").style.color = "#eeee00";   }   else if (x > .50 && x <= .75) {	document.getElementById("d"+ sides +"result").style.color = "#ffaa00";   }   else if (x > .75 && x < 1) {	document.getElementById("d"+ sides +"result").style.color = "#d80000";   }   else if (x == 1) {	document.getElementById("d"+ sides +"result").style.color = "#d80000";	document.getElementById("d"+ sides +"result").innerHTML = d +"!";   }   return false;  } }}window.onload = init;

I owe most of this to you guys :)

Link to comment
Share on other sites

It's working. Good deal. Now lemme simplify things a little:

function init () {	var links = document.getElementsByTagName("a");	for (var i = 0; links[i]; i++) {		links[i].onclick = function rolld() {			var sides = this.id.match(/[0-9]+$/);			var d = (Math.floor(Math.random() * Number(sides)) + 1);			var result = document.getElementById("d"+ sides +"result"); // THIS IS THE SHORTCUT			result.innerHTML = d;			var x = d / sides;			if (x <= .25 && x > 0) {				result.style.color = "#00d800";			} else if (x > .25 && x <= .50) {				result.style.color = "#eeee00";			} else if (x > .50 && x <= .75) {				result.style.color = "#ffaa00";			} else if (x > .75 && x < 1) {				result.style.color = "#d80000";			} else if (x == 1) {				result.style.color = "#d80000";				result.innerHTML += "!";			}			return false;		}	}}

Link to comment
Share on other sites

Yeah that works fine Deirdre's Dad! I wasn't aware I could turn all the something.somethings into variables also. I am learning alot. Now I have used what I learned and widdled my problem down to a few steps. I am almost done with this little project. My new script looks like this

function init () { var links = document.getElementsByTagName("a"); for (var i = 0; links[i]; i++) {  links[i].onclick = function rolld() {   var sides = this.id.match(/[0-9]+$/);    var d=(Math.floor(Math.random()*Number(sides)) + 1);   var result = document.getElementById("d"+ sides +"result");   result.innerHTML = d;   var x = d/sides;   if (x <= .25 && x > 0) {	result.style.color = "#00d800";   }   else if (x > .25 && x <= .50) {	result.style.color = "#eeee00";   }   else if (x > .50 && x <= .75) {	result.style.color = "#ffaa00";   }   else if (x > .75 && x < 1) {	result.style.color = "#d80000";   }   else if (x == 1) {	result.style.color = "#d80000";	result.innerHTML += "!";   }   return false;  } }}window.onload = init;function rolladxb1() { var a1=document.getElementById("a1").value; var x1=document.getElementById("x1").value; var b1=document.getElementById("b1").value; var c1=0; for (var i = 0; i <= (a1 - 1); i++) {  c1 = (Math.floor(Math.random()*x1) + 1) + c1; } var d1 = (c1+b1); /* here it screws up, it combines the variables to the number is not the sum but just the two plopped together. If it was 12 and 4 it would be 124. add: [b]alert(d1 +"="+ c1 +"+"+ b1)[/b] to see what i mean */ document.getElementById("axbd1").innerHTML = d1;}function rolladxb2() { var a2=document.getElementById("a2").value; var x2=document.getElementById("x2").value; var b2=document.getElementById("b2").value; var c2=0; for (var i = 0; i <= (a2 - 1); i++) {  c2 = (Math.floor(Math.random()*x2) + 1) + c2; } var d2 = (c2+b2); /* here it screws up, it combines the variables to the number is not the sum but just the two plopped together. If it was 12 and 4 it would be 124. add: [b]alert(d1 +"="+ c1 +"+"+ b1)[/b] to see what i mean */ document.getElementById("axbd2").innerHTML = d2;}

Everything works fine, except for the comments in the script. The b1 variable is not being determined as a number so when I try to add the variables it is just plopping them together as if they were words. Is there a way to define the b1 variable as a number or a workaround seeing as it may not be possible since the user can input anything for that variable.?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...