Jump to content

javascript problem..


dowp213
 Share

Recommended Posts

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.)

Edited by Deirdre's Dad
Link to comment
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?
Link to comment
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.. :)
Link to comment
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?.. :)
Link to comment
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.
Link to comment
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
Link to comment
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.

Link to comment
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?..
Link to comment
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
Link to comment
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>

Link to comment
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.. :)
Link to comment
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. :)
Link to comment
Share on other sites

Yeah, I struggled with this one. The more I thought about the project, the more I realized there were a bunch of "little" things needed to make it work right that are not generally taught in basic lessons, and which I personally learned quite randomly over many months (even years, some of them). So I thought it might be helpful to anyone following this thread to see them illustrated. To name a few:* It is probably not intuitively obvious to a novice developer that the project at hand will be best served by creating multiple arrays, so as to exploit the array.sort method. My own experience (and probably others' as well) told me at once that the Math.max method could be made to work, but that the resulting code would ugly.* It is not intuitively obvious that most array.sort functions sort numbers "alphabetically" unless you pass a callback function. Most of us stop reading tutorials as soon as we think we understand the concept, so even if a tutorial comes right out and says something important, a reader may have stopped reading before reaching that section. Human nature.* When copying elements from one array into another in a for-loop, the "obvious" technique is to combine the iterating index with the assignment operator. I mean this:array2[x] = array1[x];This works fine if you're copying all the elements in the source array, but it is not intuitively obvious that if you use this technique to copy only certain elements, you will inadvertently create a bunch of empty elements that could muck things up down the road. And that is why array.push is the better technique in this situation.* You could read about the isFinite method a hundred times and not really get what it's for. Intuition suggests that trying to explicitly cast an alpha string into a number would throw an error, or maybe return a zero--not some crazy "number" whose value seems to be "NAN" but cannot compared to a "NAN" string because JavaScript calls it a number. (Everyone got that?) :)* The utility of grouping form elements in a fieldset so as to simplify the getElementsByTagName process. Without it, in this instance, we would need to add a conditional to the for-loop to separate the text inputs from the button input. Only input elements require this kind of extra parsing, so (again) the need for it is not likely to spring to the mind of a novice developer. Likewise, a way of avoiding it altogether may not spring immediately to mind. I suppose adding the conditional would not have been an unpardonable sin, and in some cases it cannot be avoided. I just thought it might be useful to know that sometimes the way you structure your markup can simplify your JavaScript. The more instinctive approach is to adapt the script to the markup, not the other way around. This is especially the case if most of your programming experience does not involve interacting with a GUI.I would never deny the value of learning things slowly, by trial and error, headbang after headbang. It's the way I learned a lot of what I know, maybe most of what I know, and probably the way the more experienced developers on this board learned much of what they know. But sometimes it's useful for someone to come along and say, "Stop headbanging yourself. There's a simple way of doing this, it almost always works, and it's the way everyone else has been doing it since before time was invented."I'm the same way with the students I teach in my college writing classes. Some things are best learned gradually and idiosyncratically, partly because everyone's brain works a little differently, and a solution that fits my head may not fit yours. (E.g., not everyone can outline a paper before drafting it. Some writers have to jump right in and make an outline later.) Other things are best learned by having someone throw it in your face.

Edited by Deirdre's Dad
Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...