/    Sign up×
Community /Pin to ProfileBookmark

find smallest of several integers

hi im trying to find the smallest of several integers, assuming that the first value specifies the values of input from the user.

im having lots of trouble with this, can somebody please help me out!
Thanks

<?xml version=”1.0″ encoding=”iso-8859-1″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html xmlns = “http://www.w3.org/1999/xhtml“>
<head>
<title>tutorial 4, Exercise 9.6</title>
<script type = “text/javascript”>

var smallest, // smallest number entered by user
firstnumber, // first number entered by user
intNumber1, // first integer to be counted
intNumber, // how many numbers
i, // counts the numbers

intNumber =
window.prompt( “How many numbers”, “0” ) ;

intNumber1 = parseInt( intNumber );

smallest = parseInt( intNumber );

for (var i = 2; i <= intNumber; i++)

{

intNumber1 =
window.prompt( “Next integer”, “0” ) ;

}

window.alert(“The smallest number is ” + smallest );

</script>
</head>
<body>
</body>

</html>

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliMar 24.2005 — you can do this in several ways, here is one that came to mind at this hour of night...:-)

[code=php]
function Process(){
var tnum = prompt("How many numbers?","");
runProcess(tnum);
}
function runProcess(tnum){
var x=0;
var smallestNum=0,largestNum=0;
var inputArr = new Array();
while(x<tnum){
var nNum = Number(prompt("Enter ["+(++x)+"] number",""));
if(nNum>largestNum){
largestNum = nNum;
}
if(smallestNum==0 || smallestNum>nNum){
smallestNum = nNum;

}
inputArr[x-1] = nNum;
}
alert("smallestNum = "+smallestNum+"nlargestNum = "+largestNum);
}
[/code]
Copy linkTweet thisAlerts:
@sykeauthorMar 24.2005 — Thanks for your help Khalid Ali, but im pretty sure theres a way to do it very similar to mine. Would you have any ideas on how to get it to work with code similar to mine??

Thanks
Copy linkTweet thisAlerts:
@KorMar 24.2005 — 
but im pretty sure theres a way to do it very similar to mine.
[/quote]

similar to what? I see no method in your code lines.

If you dont like the Khalid Ali's code, you may try another way. Store and add your entries in an array. Sort the array (using sort() method). Pick up the first element of the sorted array (index 0). It will be always the smallest of all.
Copy linkTweet thisAlerts:
@KorMar 24.2005 — [code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
onload=function(){
var howmany = window.prompt( "How many numbers", "0" );
var values = new Array();
for(var i=0;i<howmany;i++){
values[i] = Number(window.prompt( "Next integer", "0" ));
}
values.sort();
alert("The smallest number is " + values[0] );
}
</script>
</head>
<body>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@sykeauthorMar 24.2005 — thanks Kor, that was a pretty short code

?
Copy linkTweet thisAlerts:
@CharlesMar 24.2005 — [font=monospace]

<script type="text/javascript">

<!--

Array.prototype.min = function () {var m = this.pop(), n, i = 1; while (n = this.pop()) {m = Math.min (m, n)}; return m}

alert ([1,3,5,4,2].min())

// -->

</script>[/font]

And please note, XHTML is [i]not[/i] HTML. In XHTML the script element takes PCDATA, in HTML the SCRIPT element takes CDATA. The problem is that JavaScript is most definately CDATA. You need to use:

[font=monospace]<script type="text/javascript">

<!--

<![CDATA[

Array.prototype.min = function () {var m = this.pop(), n, i = 1; while (n = this.pop()) {m = Math.min (m, n)}; return m}

alert ([1,3,5,4,2].min())

]]>

// -->

</script>[/font]

Or better yet, just use HTML. There is no reason to use XHTML and a few reasons not to.
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 24.2005 — It's harly worth it to extend Array.prototype for this. var someNumbers = [ 20, 10, 30];
var smallest = Math.min.apply(null, someNumbers);
Copy linkTweet thisAlerts:
@CharlesMar 24.2005 — I don't see [font=monospace]Math.min.apply[/font] in the official spec. It does seem useful and somewhat supported but be careful with it.
Copy linkTweet thisAlerts:
@FangMar 24.2005 — Welcome back [B]Charles[/B]; your technical expertise has been missed.
Copy linkTweet thisAlerts:
@CharlesMar 24.2005 — My "technical expertise" perhaps; but not the rest of me, no doubt.

I typically give up the board for Lent, so as to open up more time for prayer and meditation. But Lent is just way too early this year and the meditation part hasn't gone too well. So I tossed in the towel a few days early.
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 24.2005 — You won't find Math.min.apply because apply() and its sibling call() are generic methods and apply (pardon the pun) to all JS functions because they are properties of the Function prototype.

Older browsers don't support more than two arguments for Math.min but push() and pop() aren't very backwards compatible either.
Copy linkTweet thisAlerts:
@CharlesMar 24.2005 — Yes, I've found it now. It seems to have been added with JavaScript 1.3. But note, Array.prototype.push and Array.prototype.pop were original with Navigator 4.
Copy linkTweet thisAlerts:
@sykeauthorMar 25.2005 — the code is was looking for was this:

[code=php]<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>tutorial 4, Exercise 9.6</title>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<script type = "text/javascript">

var smallest, // smallest number entered by user
firstNumber, // first number entered by user
intNumber1, // first integer to be counted
intNumber, // how many numbers
i, // counts the numbers

firstNumber =
window.prompt( "How many numbers", "0" ) ;

intNumber1 = parseInt( intNumber1 );

smallest = parseInt( intNumber );

smallest =
window.prompt( "Next integer", "0" ) ;

for (var i = 1; i < firstNumber; i++)

{

intNumber1 =
window.prompt( "Next integer", "0" ) ;
if(intNumber1 < smallest)
{
smallest = intNumber1
}

}

window.alert("The smallest number is " + smallest );

</script>
</head>
<body>
</body>
</html>
[/code]


but thanks to everyone who helped
Copy linkTweet thisAlerts:
@Khalid_AliMar 25.2005 — Since we are talking about it, array method sort() should be used with caustion, that it sorts the strings within the array not the numbers. which means it will sort everything based upon the first char , second char and third char etc. e.g

var array1 = new Array(19,1,12,2,3);

alert(array1.sort()) will display

1,12,19,2,3

which I am sure is not the intent some times.
Copy linkTweet thisAlerts:
@CharlesMar 25.2005 — Since we are talking about it, array method sort() should be used with caustion, that it sorts the strings within the array not the numbers. which means it will sort everything based upon the first char , second char and third char etc. e.g

var array1 = new Array(19,1,12,2,3);

alert(array1.sort()) will display

1,12,19,2,3

which I am sure is not the intent some times.[/QUOTE]
However, [font=monospace]Array.prototype.sort[/font] needs just a little help and all is well: &lt;script type="text/javascript"&gt;
&lt;!--
a = [19,1,12,2,3];

alert (a.sort(function (a,b) {return a - b}))
// --&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@Khalid_AliMar 25.2005 — true, but if I wanted to change, then using Math.min.apply as not a big of a change either,

I hoped that you understand that I was referring to core functionality of a method. Any addition to it(core functionality) is not really what I was referring to.
×

Success!

Help @syke spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...