...

View Full Version : HTML Programming Maximum Number With Window Prompt?



dgeels
03-27-2012, 06:18 AM
This is what I have so far which prompts the user three numbers and then displays the max of the numbers and all three of them.

Now the last thing I was wanting to do is change it from three prompts to just one prompt. How could I prompt to type in three numbers in one prompt and get the max from those three numbers.? Any help would be greatly appreciated. Thank you in advance!





<head>
<title>Maximum Number</title>
</head>

<body>
<center><h2>Maximum Number</h2>
<script language="Javascript" type="text/javascript">

function Max(num1, num2,num3)
{
var largest=arguments[num1, num2, num3]
for (i=0; i<arguments.length; i++)
{
if ((num1>num2)&&(num1>num3))
largest=num1
else if((num2>num1)&&(num2>num3))
largest=num2
else
largest=num3
}
return(largest)
}

document.write("</br>")
var num1=prompt("Enter the first number: ", "")
var num2=prompt("Enter the second number: ", "")
var num3=prompt("Enter the third number: ", "")
var max= Max(num1, num2, num3)
document.write("You entered: ", num1, ", ", num2, ", ", num3)
document.write("</br>")
document.write("The Maximum number is: ", max)
</script></center>

</body>
</html>

xelawho
03-27-2012, 06:49 AM
I'm going to start applying for equivalency credits on these homework assignments - I'll graduate yet dagnammit.



<html>
<head>
<title>Maximum Number</title>
</head>
<body>
<center><h2>Maximum Number</h2>
<div id="res"></div>
</center>
<script type="text/javascript">
var nums=prompt("Enter three numbers separated by commas: ", "")
var numarray=nums.split(",")
var maxnum=Math.max.apply(Math, numarray);
document.getElementById("res").innerHTML="You entered: "+numarray[0]+", "+numarray[1]+", "+numarray[2]+"<br>";
document.getElementById("res").innerHTML+="The Maximum number is: "+maxnum;
</script>
</body>
</html>


notes:
- IRL you'd probably want to add some validation that they are in fact numbers being entered
- document.write() is either for hacks or losers, depending on who you ask
- prompt() isn't that far behind
- <center> runs a close third
- a decent javascript reference should point you towards things like the Math object, which will save you from writing your own functions to accomplish what you can do easily with its built in methods. If your book/website/teacher doesn't tell you about them it is time to get a new one.

Mishu
03-27-2012, 07:10 AM
You can simplify your code with by



<script>

var strNums = window.prompt('Enter 3 numbers separated by a space','');
if(strNums != null && strNums != ''){
var strOut = '';
var nums = strNums.split(/\s+/);
for(i=0; i<nums.length; i++){
if(Number(nums[i]) != 'NaN'){
strOut += nums[i]+' ';
}
}
document.write('You entered '+strOut);
document.write('Max = '+Math.max.apply(Math, nums));
}

</script>
but document.write should be used only for learning purposes. For writing to the page it's better to use the proper DOM methods like createElement() etc.

Philip M
03-27-2012, 09:24 AM
You need to allow for the situation where the user enters fewer or more than 3 numbers.


<script type = "text/javascript">

var strNums = window.prompt('Enter 3 numbers separated by a space','');
if(strNums != null && strNums != '') {
var strOut = '';
var nums = strNums.split(/\s+/);
for(i=0; i<3; i++) {
nums[i] = Number(nums[i]) || 0;
strOut += nums[i]+' ';
}
document.write('You entered '+strOut);
var max = Math.max(nums[0], nums[1], nums[2]);
document.write("The maximum value is " + max)
}

</script>


Quizmaster: At more than 250 miles in length, which motorway is the longest on the UK road network?
Contestant: Route 66

Mishu
03-27-2012, 09:33 AM
You need to allow for the situation where the user enters fewer or more than 3 numbers.



In a real situation that's correct and how you handle that would need to specified in the business rules for the website. I don't think the best option is to blindly assume the first 3 are the intended numbers or a 0 if fewer than 3 numbers. The user might be suffering from "fat fingers" and really meant the 1st, 2nd and 4th numbers to be their 3 numbers. So if they enterd 4 numbers I would ask then which ones do they really want.

Where something other that 3 numbers are entered I would normally suggest to the client that the user be made to enter the 3 numbers or cancel out.

Philip M
03-27-2012, 09:47 AM
In a real situation that's correct and how you handle that would need to specified in the business rules for the website. I don't think the best option is to blindly assume the first 3 are the intended numbers or a 0 if fewer than 3 numbers. The user might be suffering from "fat fingers" and really meant the 1st, 2nd and 4th numbers to be their 3 numbers. Where something other that 3 numbers are entered I would normally suggest to the client that the user be made to enter the 3 numbers or cancel out.

Ah, same old argumentative bullant/webdev1958, ever ready to start a pointless disagreement. It beats me why the mods do not eject you (yet again).

This is homework, yes? The idea is to identify the maximum of three numbers. There are no "business rules for the website". No business would use prompt() as the way of entering data. But your poorly-thought out code allowed the user to enter 2 or 4 numbers, and thus return a silly answer.

Mishu
03-27-2012, 09:51 AM
This is homework, yes?

I do not know. But in any case the op didn't state how it was to be handled and so I don't see the point in guessing how. I just explained what I would suggest, and why, in a real situation and that is to make the user enter 3 numbers if other than 3 numbers were entered.

Philip M
03-27-2012, 09:56 AM
I do not know. But in any case the op didn't state how it was to be handled and so I don't see the point in guessing how. I just explained what I would suggest, and why, in a real situation and that is to make the user enter 3 numbers if other than 3 numbers were entered.

What else would it be except homework? Please stop being so childish. But it would be very simple to check the length of the nums array and if it is not 3 return an error message. There are many ways of skinning a cat, and your habit of arguing the toss about nothing continues to weary me. Go away, troll!

Mishu
03-27-2012, 10:01 AM
What else would it be except homework?

But even if it is, so what? It could be the op later wants to change the number of entries from 3 to something else. With code I posted you only have to change the 3 in propmt box to something else. The rest of the js remains the same. With what you posted you would have to make at least 2 changes to your code.

The aim of my example was to make it as flexible as possible with minimal code changes if the number of entries needed to be changed.

Philip M
03-27-2012, 10:06 AM
But even if it is, so what? It could be the op later wants to change the number of entries from 3 to something else. With code I posted you only have to change the 3 in propmt box to something else. The rest of the js remains the same. With what you posted you would have to make at least 2 changes to your code.

The aim of my example was to make it as flexible as possible with minimal code changes if the number of entries needed to be changed.

Horsepoo and bluster! Why do you now want to start guessing? If the OP changes 3 to 4 in the prompt box in your code it is still poorly thought-out and amateurish.
So 2 changes are needed to my code? - shock! horror! For heaven's sake - grow up!

Mishu
03-27-2012, 10:29 AM
So 2 changes are needed to my code? - shock! horror! For heaven's sake - grow up!

But even if I added the code to ask again if other than the required numbers was entered I would still need to make only 1 change to my code if 3 was changed to something else.

Why do you have an issue with me showing the op example code where only 1 change would be needed if they wanted to change the number of entries?



So 2 changes are needed to my code?

it's actually at least 3.

1. The prompt text

2. the maximum value of i in your for loop

3. the number of array elements in your Math.max()




<script type = "text/javascript">

var strNums = window.prompt('Enter 3 numbers separated by a space','');
if(strNums != null && strNums != '') {
var strOut = '';
var nums = strNums.split(/\s+/);
for(i=0; i<3; i++) {
nums[i] = Number(nums[i]) || 0;
strOut += nums[i]+' ';
}
document.write('You entered '+strOut);
var max = Math.max(nums[0], nums[1], nums[2]);
document.write("The maximum value is " + max)
}

</script>

Mishu
03-27-2012, 11:15 AM
to the op:

This is an off the cuff example of making the user enter the required number of numbers and if you want to change the number of required numbers the only single change you need to make is resetting reqNums = 3


<script>

var reqNums = 3, nums = [];
do {
var strNums = window.prompt('Enter '+reqNums+' numbers separated by a space','');
count=0;
if(strNums != null && strNums != ''){
var strOut = '';
strNums.replace(/^\s+|\s+$/g, '');
nums = strNums.split(/\s+/);
for(i=0; i<nums.length; i++){
if(!isNaN(parseFloat(nums[i]))){
strOut += nums[i]+' ';
++count;
}
}
}
} while(count != reqNums && nums.length > 0 && strNums != null)
if(count == reqNums){
document.write('You entered '+strOut);
document.write('Max = '+Math.max.apply(Math, nums));
}

</script>I haven't fully tested it with all input possibilities but hopefully you can see the concept behind what I have done which is the aim of the example.

I haven't run pm's code in post 11, but it will need at least 3 changes to the code if you vary the required numbers.

Philip M
03-27-2012, 12:00 PM
Why do you have an issue with me showing the op example code where only 1 change would be needed if they wanted to change the number of entries?

It's actually at least 3.


Because you are an evil troll, banned multiple times from this forum (but for some unfathomable reason you seem to be allowed by the mods to crawl back in again), always seeking a confrontation or an argument.

It is not at least 3. It is exactly 3. So what? As I say, do try and grow up (although I realise that is probably impossible for you now). There is not the slightest suggestion from anyone but you that the OP wants to change the number of numbers - so as usual your objection is artificial and manufactured.

Mishu
03-27-2012, 12:15 PM
There is not the slightest suggestion from anyone but you that the OP wants to change the number of numbers -

I didn't say the op wants to change anything. I suggested that if in the future the op does want to change the number of entries then they would need to make only 1 change to the example I posted. I always aim to write as flexible and as low maintenace as possible code. That's how I was taught. I don't see the point of imposing 3 changes when 1 is all that is needed.

Philip M
03-27-2012, 12:18 PM
Once more, so what, evil troll?

Mishu
03-27-2012, 12:22 PM
Once more, so what

I was taught, but it should be common sense, that the more changes you need to make then the more likely mistakes will be made, especially if the code is passed onto someone else to maintain.

At the very least, it will take longer to make multiple changes than it would take to make a single change. In this example it's not a huge issue, but generally and in principle I like to write code as flexible and as low maintenace as possible for the above reasons.

Philip M
03-27-2012, 03:50 PM
In this example it's not a huge issue

It is not an issue at all, except in as far as you choose to make it so just to be disagreeable and argumentative.

Mishu
03-28-2012, 02:17 AM
It is not an issue at all,

So you agree with me :) but you seem to have an issue with me showing the op how to code it so that if the number of required numbers changes then only 1 change and not multiple changes need to be made to the code.

I see nothing wrong with showing the op and anyone that might be interested how to structure the code so that only 1 change is required and describing the benefits of having to make as few changes as possible.

At the very least someone might see how a do-while loop, which they might not even be aware of, works and so learn something new. Isn't that what these types of forums are meant to be about?

and btw - if what I posted is useless or fluff, don't you think the moderators would have deleted my post and code or are you now going to imply the moderators are not doing their jobs?

If all that you say is correct, why don't you attack the moderators for not agreeing with you instead of a poor little defenceless member like me since they are the judge and jury? Pick on someone your own size you, you, you big bully :)

WA
03-28-2012, 09:11 AM
Please take the pointless bickering off forums.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum