Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: a bit of a conundrum

  1. #1
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296

    a bit of a conundrum

    using javascript and only the operators +, *, /, -, <, >, <=, >=, =, ==, !=

    can you compare three user input integers and output the largest and the smallest.

    If anyone has read my last couple of posts they have been about Javascript - I have been really trying to concentrate on enhancing my knowledge and ability of it.

    this is an assignment Im doing and I can only use the above operators and only 'if' statements - no 'if else' statements. (Im not cheating on this assignment by asking, as Im learning and teaching myself)

    I wish I had paid more attention in math class in highschool - there is either a relationship between numbers that my mind hasn't processed yet or a rule about number relations that I didn't learn in school - haha
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

  2. #2
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Look at the Comparison Operator section on this page - http://www.w3schools.com/js/js_operators.asp

    I'd also suggest instead of just getting the answer straight out from someone here, you either code up an attempt or do it with pseudo code and post it here. From there we could give tips or other assistance in good conscience.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  3. #3
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296
    ahhhhh - w3schools is one of my most used bookmarks, but I can only use the operators that I stated, or rather - I can only use the techniques learned in the chapter that the assignment concludes.

    The psuedocode - this could go many ways depending on how the code needs to be structured - it would start out , 'if blah blah blah

    but that is the problem - Im not even sure how to structure the 'if' statement - how I am thinking of it is .......

    if (number1 is greater than number2 and number2 is greater than number3)
    then write number1 is the largest and number2 is the smallest;


    and this statement would be repeated until the largest and smallest were were unequivically determined, the thing is that I can write java code to perform this task no problem - but I am having trouble doing it and only using the parameters in the chapter that the assignment is in.

    Its not writing the code that is the difficult part - it is writing it and not traversing the limitations set on me by the assignment instructions.
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

  4. #4
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Well, that is why I'm asking you to write something up. It will help you think through the problem and allow us to help you without just giving you an answer.

    Right now the example you have given does not fit in with the project requirements since you would need the logical 'and' operator.

    A simpler method may be to put the three values into an array. Then all you have to do is traverse the array and do a simple sort (I'd recommend doing a google for a java bubble sort) putting the largest values up front. Then just return the first two values.

    You'd only need a simple if statement to do the actual value comparison.

    Here is a simplified version of what you'd have to do

    Array with three elements
    // element values
    [3][1][2]

    Iterate through the array
    temp var // to store values temporarily for swap, you may be able to get away without having this.
    If arrayelement[index 1] < arrayelement[index 2]
    temp var = arrayelement[index 1] // store first element so the value isn't lost during swap
    arrayelement[index 1] = arrayelement[index 2] // swap the larger element to the beginning
    arrayelement[index 2] = temp var // put the lower value back into the array after the larger one


    The first iteration through would see that element one (3) is greater than element two (1) and leave it alone. The next comparison would see that element two (1) is less than element three (2) and swap it. Then you'd just display elements one and two.

    There are plenty of examples of doing this with an array of ints out on the net. This solution is also scalable to support more than three values. =)
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  5. #5
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296
    Sorry its been awhile since I have been back to this.

    Arrays are a no no since they stretch outside the limits set by the assignment.

    here is my attempt at it so far:

    Code:
    <script type = text/javascript>
    
    	  var firstNumber,
    	      secondNumber,
    	      thirdNumber,
    	      number1,
    	      number2,
    	      number3,
    	      sum,
                  average,
    	      product;
    
    		
    		firstNumber = window.prompt("Please enter your first number");
      		secondNumber = window.prompt("Now enter the second number");
    		thirdNumber = window.prompt("And now the last number ...");
    
    		  number1 = parseInt(firstNumber);
    		  number2 = parseInt(secondNumber);
    		  number3 = parseInt(thirdNumber);
    
    		  sum = number1 + number2 + number3;
    		  average = (number1 + number2 + number3) / 3;
    		  product = (number1 * number2) * number3;
    
    		if (number1 + number2 > number2 + number3) 
    		  window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number1 + " is the largest\n"
    				+ number3 + " is the smallest");
    
    
    		if (number1 + number3 > number3 + number2) 
    		  window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number1 + " is the largest\n"
    				+ number2 + " is the smallest");
    
    		if (number2 + number3 > number3 + number1)
    		   window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number2 + " is the largest\n"
         				+ number1 + " is the smallest");
    
    
    		if (number2 + number1 > number1 + number3) 
    		   window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number2 + " is the largest\n"
    				+ number3 + " is the smallest");
    
    		if (number3 + number1 > number1 + number2)
    		   window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number3 + " is the largest\n"
    				+ number2 + " is the smallest");
    
    
    		if (number3 + number2 > number2 + number1) 
    		   window.alert(+ sum + " is the sum, \n" 
    			       + average + " is the average, \n" 
    			       + product + " is the product \n"
    				+ number3 + " is the largest\n"
    				+ number1 + " is the smallest");
    
    
    				
    
    
    	</script>
    the problem with this is that once the 'OK' button is pressed it reevaluates the integers 2 more times - one of the three is usually correct - but obviously it has to be correct the FIRST AND ONLY time it runs.

    I'm really stumped here ...................

    My theory here, when I tried this was that if the sum of a+b were greater than a+c then a would be the largest since it was the common integer in both the equations.

    Theoretically it makes sense but in practice it doesn't seem to work that way.

    I have tried many things and can't get the right answer, though I love to solve problems - my hope is, honestly that someone will post the answer (within the guidelines set out in the original post) so I can evaluate it and learn from it - I can't learn anything while Im stuck in a static state of confusion ........... haha
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

  6. #6
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    What you need is to nest the if's. Compare 1 and 2 in the outter most, then 1 and 3, and then 2 and 3. Three layers of nested if's will handle the issue.

  7. #7
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296
    Im sure that I used nested IF's at one time and I overwrote that file - sounds like that I probably made some sort of a mistake with them.
    I'm gonna work on that now - let you know how it works out this time ...

    Cheers for the speedy reply ...

    sometimes in the 'Web Development' forum you wait for days for a response - haha
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

  8. #8
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296
    This is my latest attempt:

    Code:
    <script type = "text/javascript">
    
    	var firstNumber,
    	    secondNumber,
    	    thirdNumber;
    
    		firstNumber = window.prompt("Please enter your first number: ", "0");
    		secondNumber = window.prompt("Please enter a second number: ", "0");
    		thirdNumber= window.prompt("And now enter your last number: ", "0");
    
    		firstNumber = parseInt( firstNumber );
    		secondNumber = parseInt( secondNumber );
    		thirdNumber = parseInt( thirdNumber );
    
    
    	// ------------ determines the largest of the three numbers -----------
    
    		//executes if first number is the highest 
    		
    		if ( firstNumber > secondNumber ){
    
    		           if ( firstNumber > thirdNumber )
    				document.writeln( firstNumber + " is the largest!<br>" );
    
    		}
    			
    
    		//executes if second number is the highest
    				
    		if ( secondNumber > firstNumber ){
    
    			if ( secondNumber > thirdNumber )
    			document.writeln( secondNumber + " is the largest!<br>" );
    
    		}
    			
    
    		//executes if third number is the highest
    
    		if ( thirdNumber > firstNumber ){
    
    			if ( thirdNumber > secondNumber )
    				document.writeln( thirdNumber + " is the largest!<br>" );
    
    		}
    			
    
    	// ------------ determines the lowest of the three numbers --------------
    
    		//executes if first number is the lowest
    
    		if ( firstNumber < secondNumber ){
    
    			if ( firstnumber < thirdNumber )
    				document.writeln( firstNumber + " is the smallest!" );
    
    		}
    
    		//executes if second number is the lowest
    
    		if ( secondNumber < firstNumber ){
    
    			if ( secondNumber < thirdNumber )
    				document.writeln( secondNumber + " is the smallest!" );
    
    		}
    
    		//executes if the third number is the lowest
    
    		if ( thirdNumber < firstNumber ){
    
    			if ( thirdNumber < secondNumber )
    				document.writeln( thirdNumber + " is the smallest!" ); 
    
    		}
    
    
    	</script>
    If the firstNumber is the largest and the thirdNumber is the smallest it works fine.

    Any other combination only outputs and displays the largest number!

    Can someone explain this to me???
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

  9. #9
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    In you "lowest" section, in the first nested if, you have firstnumber instead of firstNumber.

  10. #10
    Senior Member IcSilk's Avatar
    Join Date
    Aug 2001
    Posts
    296
    Never mind answering the last question - I really should have experimented with it more before posting it - just didn't think that I would have enough patients to go through it all again - haha

    this is the code that ultimately worked:
    Code:
    <script type = "text/javascript">
    
    	var firstNumber,
    	    secondNumber,
    	    thirdNumber;
    
    		firstNumber = window.prompt("Please enter your first number: ", "0");
    		secondNumber = window.prompt("Please enter a second number: ", "0");
    		thirdNumber= window.prompt("And now enter your last number: ", "0");
    
    		firstNumber = parseInt( firstNumber );
    		secondNumber = parseInt( secondNumber );
    		thirdNumber = parseInt( thirdNumber );
    
    
    	// ------------ determines the largest of the three numbers -----------
    
    		//executes if first number is the highest  
    		
    		if ( firstNumber > secondNumber ){
    
    			if ( firstNumber > thirdNumber ){
    				document.writeln( firstNumber + " is the largest!<br>" );
    
    			
    			if ( secondNumber > thirdNumber )
    				document.writeln( thirdNumber + " is the smallest!" );
    
    			if ( thirdNumber > secondNumber )
    				document.writeln(secondNumber + " is the smallest!" );
    		}
    			}
    
    		
    		//executes if second number is the highest
    
    		if ( secondNumber > firstNumber ){
    
    			if ( secondNumber > thirdNumber ){
    				document.writeln( secondNumber + " is the largest!<br>" );
    
    			
    			if ( firstNumber > thirdNumber )
    				document.writeln( thirdNumber + " is the smallest!" );
    
    			if ( thirdNumber > firstNumber )
    				document.writeln(firstNumber + " is the smallest!" );
    		}
    			}
    
    
    		//executes if third number is the highest
    
    		if ( thirdNumber > firstNumber ){
    
    			if ( thirdNumber > secondNumber ){
    				document.writeln( thirdNumber + " is the largest!<br>" );
    
    			
    			if ( firstNumber > secondNumber )
    				document.writeln( secondNumber + " is the smallest!" );
    
    			if ( secondNumber > firstNumber )
    				document.writeln(firstNumber + " is the smallest!" );
    		}
    			}
    		
    		
    			
    
    		
    
    
    	</script>
    what I would love to know if someone will enlighten me - is, even though that this code works - is there a shorter more efficient way of writing it, while still staying within the assignment parameters???
    "In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •