Need help with functions!


#1

So here is the code
`
function eval2function() {
let option = document.getElementById("choices").value;

	if (option = "min"){
		GenMin();
	}
		else if (option = "max") {
			GenMax();
		}
		else if (option = "pow") {
				GenPow();
			}
	 }


  function GenMin(){
	let min1 = parseFloat(document.getElementById("int1").value);
	let min2 = parseFloat(document.getElementById("int2").value);

	let minnum = Math.min(min1,min2);

	document.getElementById("result").innerHTML = "Minimum is " + minnum;

  }


  function GenMax(){
	let max1 = parseFloat(document.getElementById("int1").value);
	let max2 = parseFloat(document.getElementById("int2").value);

	let maxnum = Math.max(max1,max2);

	document.getElementById("result").innerHTML = "Maximum is " + maxnum;

  }
  function GenPow(){
	let base = parseFloat(document.getElementById("int1").value);
	let expo = parseFloat(document.getElementById("int2").value);

	let power = Math.pow(base, expo);
	document.getElementById("result").innerHTML = "Power is equal to" + power;
  }

`

So this works by you choosing one option, adding values.. and then hitting run...
that triggers the function eval. that evaluates what option it is...
and then the function for that option kicks in.

the code for the functions is perfect my mate has the same code word to word.. it is working fine.

but mine keeps showing the math.min answer.. even tho I have the other option chosen


#2

Where did your html code go? I wanted to help you, but i need your html code


#3

<h2> Part 1</h2>
      
            <div>
			  <form name = "math">
			  <table name = "mathtable">
				<tr>
				<td>
				<select id = "choices">
					<option>*Choose*</option>
					<option id = "min">Find Min</option>
					<option id = "max">Find Max</option>
					<option id = "pow">Find pow</option>
				</select>
				</td>
				<td>
				<input type = "text" id = "int1" placeholder = "Enter the first Number"> █
				<input type = "text" id = "int2" placeholder = "Enter second Number">
				<input type = "button" name = "Run" value = "Run it!" onclick = "return eval2function()">
				</td>
				</tr>
			</table>
			</form> 
			<div id="result"></div>
			  </div>

#4

you made a trivial error here:

if (option = "min")

a single equal sign means assign, in a if condition it would make more sense to check if option equals "min"


#5

I tried (option == min)... but when I do that the function doesn't work. I get no answer when I click the button!


#6

here:

var option = document.getElementById("choices").value;

this will get the value of the value attribute of <input>, given you don't have one, it will get the text (Find Min)

give your options fields value attributes so you actually get the right value for comparison.


#7

Oh, If you would check my 1st post.. those lines are for some reason outside the code,

but I already had let option = document.getElementById("choices").value;

 function eval2function() {
		var option = document.getElementById("choices").value;
		
		if (option == "min"){
			GenMin();
		}
		else if (option == "max") {
				GenMax();
			}
		else if (option == "pow") {
					GenPow();
				}
		 }

I can't see a syntax error or an error of any kind... chrome shows nothing.


#8

Did you read my answer? option will contain the text of the <option>this text</option> element ("Get Min" for example), this will never equal "min"

you should give your <option> a value attribute so you get the right value from document.getElementById("choices").value for your comparison.