Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
<html>
<head>
<title>Trouver le plus petit nombre</title>
<script type='text/javascript'>
function Min_nombre(A,B,C,D)
{
	var A=parseFloat(A);
	var B=parseFloat(B);
	var C=parseFloat(C);
	var D=parseFloat(D);
	var res;
	
				   if(A > B && A > C && A > D )
						 res = B,C,D;
				   if(B > A && B > C && B > D)
						 res = A,C,D;
				   if(C > A && C > B && C > D )
						 res = A,B,D;							 
				   else res = A,B,C; //shows only 1st variable :-(				   

						console.log(res);
					
 window.document.getElementById('resultat').value = res;
}

</script>
</head>

<body>
<p>Definition du nombre MIN</p>
<input type='text' name='A' id='A' VALUE="A">
<input type='text' name='B' id='B' VALUE="B">
<input type='text' name='C' id='C' VALUE="C">
<input type='text' name='D' id='D' VALUE="D">
<input type='text' name='Resultat' id='resultat'>
<input type='submit' value='GO' />
</body>
</html>


-This code is searching for 3 smallest number in 4 given-
When i m launching this code the var res is displaying only the first VAR A and the other two doesn t. Where i made mistake ? I used wrong syntax or logic?
Posted
Updated 21-Oct-14 10:23am
v2
Comments
BacchusBeale 21-Oct-14 20:28pm    
Here's another way to separate 3 minimum numbers:
function Min_nombre()
{
var A=parseFloat(document.getElementById('A').value);
var B=parseFloat(document.getElementById('B').value);
var C=parseFloat(document.getElementById('C').value);
var D=parseFloat(document.getElementById('D').value);
var res=[A,B,C,D];
res=res.sort(compareNumbers);
res.pop();//remove last element
console.log(res);
document.getElementById('resultat').value = res;
}

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
function compareNumbers(a, b) {
return a - b;
}

Simple solution would be:
C#
res = "A=" + A + "; B=" + B + "; C=" + C;

First operand in concatenation is string, so it will convert other (non-string) operands to strings and concatenate them.

For more universal and advanced approaches, please see these useful discussions:
http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format[^],
http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery[^].

—SA
 
Share this answer
 
v4
Comments
Kornfeld Eliyahu Peter 21-Oct-14 16:54pm    
Your solution will result in a nicely formatted output, however for simple output all OP has to do is make res into an array by enclosing A, B and C in square brackets - res = [A, B, C], the rest will work automatically in OP's case...
voronitski.net 21-Oct-14 17:02pm    
yes , i will recode it
Sergey Alexandrovich Kryukov 21-Oct-14 18:06pm    
This is nothing but the illustration of the idea...
I saw you suggestion, up-voted it. The idea with the list can be combined with "nice formatting" by putting some strings (obtained by concatenation of strings with numeric objects, as in my sample) in the list.
—SA
Kornfeld Eliyahu Peter 22-Oct-14 2:21am    
And a good idea - that's why I 5ed!
Sergey Alexandrovich Kryukov 22-Oct-14 2:25am    
Thank you, Peter.
—SA
I'm guessing you want to display the three variables A, B and C as a string. Javascript doesn't use the ',' operator for concatenation, you must use the '+' operator.

Since the values are numeric, you can't just use '+' by itself to make a string, since that would add the values together - you have to concatenate with strings in between:
JavaScript
res = A + ' ' + B + ' ' + C;


or you can put them in an array and join them:
JavaScript
res = [A, B, C].join(' ');


or you can dump the array directly to the console without joining it as a string:
JavaScript
res = [A, B, C];
console.log(res);
 
Share this answer
 
If you go and debug your code you will see that the res = A, B, C part assigned only A and left out B and C...That's one of the small beauties of JavaScript - do as it please and set no errors...
I assume that what you wanted is a comma separated list of the values...
First - create an array with the proper syntax:
JavaScript
else res = [A,B,C];

Now you can assign it to the value property of the input box just before and magically you will got a comma separated list of values...But nothing magical about it, it's the result of JavaScript converting your array to string (string is the type of the value property)...
If you want the force that conversation (eg you want to set a new variable never initialized) you can do it like this:
JavaScript
var newVar = "" + res;

The preceding string (event an empty string) will force JavaScript to convert res (which is an array) to string and that convert will result in a comma separated list...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Oct-14 18:08pm    
Sure, a 5.
—SA
Kornfeld Eliyahu Peter 22-Oct-14 2:22am    
Thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900