# javascript problem..

## Recommended Posts

this is the problem: input 10 numbers and display the highest number in odd sequence.. does anyone have an idea?.. i already found a way to find the highest among 10 input numbers using math.max() function but I'm having some problem in sequencing them as odd..

##### Share on other sites

This sounds like a school project, so I'm not going to help you too much.One thing you need is to identify which numbers are odd. There are several ways to do this. One way is an old programmer's trick and if you try to explain to your teacher how it works, you might not understand it, so it might be better not to use it. (Fellow board members, I am discussing the bitwise trick.)The other way is to use the modulus operator. Read about it here. It works like this:

`var num = 27;if (num % 2 == 0) {   // it's even}else {   // it's odd}`

What we're doing is dividing by 2 and seeing if there is a remainder. If the number is even, there will be 0 remainder, so that is what we test for. The other possible values are 1 and -1, which we could also test for, but it's faster to test for just one value instead of two. (And much much faster than converting to absolute value before we run the test.)

##### Share on other sites

this is the problem: input 10 numbers and display the highest number in odd sequence.. does anyone have an idea?.. i already found a way to find the highest among 10 input numbers using math.max() function but I'm having some problem in sequencing them as odd..
so are you only supposed to return the odd ones in descending order?
##### Share on other sites

This sounds like a school project, so I'm not going to help you too much.One thing you need is to identify which numbers are odd. There are several ways to do this. One way is an old programmer's trick and if you try to explain to your teacher how it works, you might not understand it, so it might be better not to use it. (Fellow board members, I am discussing the bitwise trick.)The other way is to use the modulus operator. Read about it here. It works like this:
`var num = 27;if (num % 2 == 0) {   // it's even}else {   // it's odd}`

What we're doing is dividing by 2 and seeing if there is a remainder. If the number is even, there will be 0 remainder, so that is what we test for. The other possible values are 1 and -1, which we could also test for, but it's faster to test for just one value instead of two. (And much much faster than converting to absolute value before we run the test.)

thanks for the reply btw.. yeah you are right, this is a school project.. and this topic wasn't discussed to me at all.. so feel free to help me with all you can.. i guess this would be a very hard project.. odd sequencing seems to be my only problem..how can i use "if..else" function to my highest number if i used math.max() to determine the highest among 10 numbers?..is there any other functions i can use to find the highest number among 10 inputs?.. or i can use math.max() in "if..else" function?..this is my unfinished code:var a=prompt("Enter a number1:","");var b=prompt("Enter a number2:","");var c=prompt("Enter a number3:","");var d=prompt("Enter a number4:","");var e=prompt("Enter a number5:","");var f=prompt("Enter a number6:","");var g=prompt("Enter a number7:","");var h=prompt("Enter a number8:","");var i=prompt("Enter a number9:","");var j=prompt("Enter a number10:","");document.write("The highest number is:");document.write(Math.max(a,b,c,d,e,f,g,h,i,j));as you can see.. i already found out how to find the highest.. thanks for the reply, and still hoping for more..
##### Share on other sites

I don't understand the question. You have the Max number. What's the next thing you think you need to do?

##### Share on other sites

I don't understand the question. You have the Max number. What's the next thing you think you need to do?
the next thing to do Sir, is to display the highest number as odd sequence.. for example if I input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 the highest is obviously 10.. (in which I used max() to find the highest)so the output should look like this:13579i guess.. if this is an odd sequence.. from your understanding, what is an odd sequence Sir?..and is this possible in javascript?..
##### Share on other sites

That's what I would call an odd sequence. Are you supposed to be returning a sequence of odd numbers, though, or just the highest odd number in the set?

##### Share on other sites

That's what I would call an odd sequence. Are you supposed to be returning a sequence of odd numbers, though, or just the highest odd number in the set?
the whole SET of odd sequence should be returned. is this possible Sir?.. Edited by SICK
##### Share on other sites

the whole SET of odd sequence should be returned. is this possible Sir?..
Of course it is.First off, I'd start by redoing the way you collect the input. I'd use a while loop to loop 10 times and each time add the value to an array using the Array.push() method. Look at the reference pages I linked and see if you can figure out how to set that up. Once you get that part working, all you need to do is loop through the array and perform the test DD showed you, printing out the ones that are odd.
##### Share on other sites

Of course it is.First off, I'd start by redoing the way you collect the input. I'd use a while loop to loop 10 times and each time add the value to an array using the Array.push() method. Look at the reference pages I linked and see if you can figure out how to set that up. Once you get that part working, all you need to do is loop through the array and perform the test DD showed you, printing out the ones that are odd.
this is difficult Sir, do you mind making me a 5 input while loop or just 3 and I'll handle the rest, I'm having difficulties at making codes with no reference.. :)the references i found on the internet doesn't help much at all.. and i don't know how to use array yet.. am very sorry.. T_Tbtw thanks for the rep
##### Share on other sites

loops are very basic control structures, and array's are some of the simplest objects to deal with in programming. I'm not saying this to downplay your skillset, but rather to suggest you at least attempt these basic techniques so that way you can learn how and why they work.besides, there are examples of while loops right in the links ShadowMage posted. Start small, see if you can get it to output the numbers 1 - 10. Then, replace the output code with a line of code that takes the users input and inserts it into the array. (you've already managed to get the input, so now you just have to expand upon it slightly). Come back and let us know how you are making out by that point.

##### Share on other sites

loops are very basic control structures, and array's are some of the simplest objects to deal with in programming. I'm not saying this to downplay your skillset, but rather to suggest you at least attempt these basic techniques so that way you can learn how and why they work.besides, there are examples of while loops right in the links ShadowMage posted. Start small, see if you can get it to output the numbers 1 - 10. Then, replace the output code with a line of code that takes the users input and inserts it into the array. (you've already managed to get the input, so now you just have to expand upon it slightly). Come back and let us know how you are making out by that point.
ok.. would I still use the max() function?..
##### Share on other sites

I'm very sorry SIR.. I really cannot deal with this.. but thanks a lot..

##### Share on other sites

An alternative (a rather slow one) but is quite basic: if (j % 2 == 0 && j < i && j < h etc) { // it's even}else { document.write(j + "<br />")}so if it is odd and compare it to every other variable to order it. I haven't fully tested it but pretty sure it will work.

Edited by BobbyFrankyJim
##### Share on other sites

It looks like OP has dropped out of the thread, and I had some time, so I wrote up a way to do this thing. Hopefully, someone out there can learn a thing or two. The CSS is for convenience only. The JavaScript is more important, and then the way the script interacts with the HTML. Certainly there are other ways the goal could be accomplished.

`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>   <head>	  <meta http-equiv="content-type" content="text/html;charset=UTF-8">	  <title></title>	  <style type="text/css">		 * {			padding: 0;			margin: 0;		 }		 form {			width: 160px;			margin: 20px;		 }		 #myinputs {			border: 0;		 }		 #myinputs * {			display: block;			width: 120px;			float: left;			margin-bottom: 10px;		 }		 #myinputs input[type="text"] {			width: 38px;			border: solid 1px #000;		 }		 		 #myinputs label, #mybutton {			clear: both;		 }		 #mybutton {			width: 100%;		 }		 #myoutput {			margin: 20px;		 }	  </style>	  <script type="text/javascript">		 function sequence_numbers () {			var input_array = document.getElementById("myinputs").getElementsByTagName("input");			var len = input_array.length;			var output_array = [];			var val;			var i;			var output = "";			for (i = 0; i < len; ++i) {			   val = input_array[i].value;			   if (val != "" && isFinite(Number(val) ) ) { // it's a string and evals to a number				  if (val % 2 != 0) { // it's odd					 output_array.push(input_array[i].value);				  }			   }			}			function by_numbers (a, b) {			   return b - a;			}			output_array = output_array.sort(by_numbers);			len = output_array.length;			for (i = 0; i < len; ++i) {			   output += output_array[i] + "<br>";			}			document.getElementById("myoutput").innerHTML = output;			return false;		 }		 function init () {			document.getElementById("myform").onsubmit = sequence_numbers;		 }		 window.onload = init;	  </script>   </head>   <body>	  <form id="myform" action=""><a href></a>		 <fieldset id="myinputs">			<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">		 </fieldset>		 <input type="submit" value="Sequence numbers" id="mybutton">	  </form>	  <p id="myoutput"></p>   </body></html>`

##### Share on other sites

And make sure you read it and understand it

##### Share on other sites

It looks like OP has dropped out of the thread, and I had some time, so I wrote up a way to do this thing. Hopefully, someone out there can learn a thing or two. The CSS is for convenience only. The JavaScript is more important, and then the way the script interacts with the HTML. Certainly there are other ways the goal could be accomplished.
`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>   <head>	  <meta http-equiv="content-type" content="text/html;charset=UTF-8">	  <title></title>	  <style type="text/css">		 * {			padding: 0;			margin: 0;		 }		 form {			width: 160px;			margin: 20px;		 }		 #myinputs {			border: 0;		 }		 #myinputs * {			display: block;			width: 120px;			float: left;			margin-bottom: 10px;		 }		 #myinputs input[type="text"] {			width: 38px;			border: solid 1px #000;		 }		 		 #myinputs label, #mybutton {			clear: both;		 }		 #mybutton {			width: 100%;		 }		 #myoutput {			margin: 20px;		 }	  </style>	  <script type="text/javascript">		 function sequence_numbers () {			var input_array = document.getElementById("myinputs").getElementsByTagName("input");			var len = input_array.length;			var output_array = [];			var val;			var i;			var output = "";			for (i = 0; i < len; ++i) {			   val = input_array[i].value;			   if (val != "" && isFinite(Number(val) ) ) { // it's a string and evals to a number				  if (val % 2 != 0) { // it's odd					 output_array.push(input_array[i].value);				  }			   }			}			function by_numbers (a, b) {			   return b - a;			}			output_array = output_array.sort(by_numbers);			len = output_array.length;			for (i = 0; i < len; ++i) {			   output += output_array[i] + "<br>";			}			document.getElementById("myoutput").innerHTML = output;			return false;		 }		 function init () {			document.getElementById("myform").onsubmit = sequence_numbers;		 }		 window.onload = init;	  </script>   </head>   <body>	  <form id="myform" action=""><a href></a>		 <fieldset id="myinputs">			<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">						<label>Enter a number: </label>			<input type="text">		 </fieldset>		 <input type="submit" value="Sequence numbers" id="mybutton">	  </form>	  <p id="myoutput"></p>   </body></html>`

NOW THIS IS WHAT I CALL LEARNING!..THANK YOU VERY MUCH SIR.. I can easily learn if there is a reference code..instead of countless hours searching over the internet with no clue where I'm heading..And again Sir thank you for the codes, but I will not use it completely.. I will remove the CSS part.. cuz we are concentrating on javascript.. and for some infos.. I'm not really good at javascript, but as they say learning is a painful process..and YES, I really learn something here..thanks for the help guys..
##### Share on other sites

NOW THIS IS WHAT I CALL LEARNING!..THANK YOU VERY MUCH SIR.. I can easily learn if there is a reference code..instead of countless hours searching over the internet with no clue where I'm heading..And again Sir thank you for the codes, but I will not use it completely.. I will remove the CSS part.. cuz we are concentrating on javascript.. and for some infos.. I'm not really good at javascript, but as they say learning is a painful process..and YES, I really learn something here..thanks for the help guys..
for what its worth, the people here were giving you the links and instructions you needed, there was no endless searching required. Ultimately, if you can understand the code line by line, then I guess you could say you've learned it. Besides, don't teachers help students anymore these days, or are people just not asking?DD, you don't count, even though you are teacher.

##### Share on other sites

points well made. my only concern was that the OP would take these kind of solutions for granted, and consider them to be a learning experience in the classic sense of someone creating a custom function on their own to solve a problem not directly solved using some sort of built in method or library. I understand all people learn differently, but I think we could all agree the bottom up approach is usually the most successful one, as it usually provides the learner with a sense of being able to understand their code line by line. I mean, the OP gave up without really trying, and the impression that hours of searching provided no results implies that the only answers he was looking for were complete solutions, not trying to understand the few basic (debatable) concepts needed to require to put it all together.I mean, we're here to help build it from the ground up, but we're not (shouldn't be) here to do all the manual labor

Edited by thescientist
##### Share on other sites

That's why I wrestled with the plan. I was also under the impression that OP had given up. Most of the time I think I talk people through it.