Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help on basic code

    var num1 = window.prompt("insert your first number");
    num1 = parseFloat(num1);
    var num2 = window.prompt("insert your second number");
    num2 = parseFloat(num2);
    var num3 = window.prompt("insert your third number");
    num3 = parseFloat(num3);
    var num4 = window.prompt("insert your fourth number");
    num4 = parseFloat(num4);
    var num5 = window.prompt("insert your fifth number");
    num5 = parseFloat(num5);

    var num1Cubed = cube(num1Cubed);

    var num1Cubed = cube(num2Cubed);

    var num1Cubed = cube(num3Cubed);

    var num1Cubed = cube(num4Cubed);

    var num1Cubed = cube(num5Cubed);


    document.write (num1 + " " + "cubed is" + " " + num1Cubed + <br> + num2 + " " + "cubed is" + " " + num2Cubed + <br> + num3 + " " + "cubed is" + " " + num3Cubed + <br> + num4 + " " + "cubed is" + " " + num4Cubed + <br> + num5 + " " + "cubed is" + " " + num5Cubed + <br> + "the avrage of the orginal numbers by a mean standard is" + " " + avrageOfNums;

    cube(num1,num2,num3,num4,num5);

    function cube(num1,num2,num3,num4,num5)
    {
    var num1Cubed = num1 * num1 * num1;
    var num2Cubed = num2 * num2 * num2;
    var num3Cubed = num3 * num3 * num3;
    var num4Cubed = num4 * num4 * num4;
    var num5Cubed = num5 * num5 * num5;
    return num1Cubed,num2Cubed,num3Cubed,num4Cubed,num5Cubed;
    }

    avrage(num1,num2,num3,num4,num5);

    function avrage(num1,num2,num3,num4,num5)
    {
    var avrageOfNums = num1 + num2 + num3 + num4 + num5 / 5;
    avrageOfNums = parseFloat(avrageOfNums);
    return avrageOfNums
    }




    can any1 tell me where i am going wrong and explain if possible how to correct it

  2. #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Computer scientists have invented loops not to repeat the same thing many times...

    A function can be called with different arguments. Then something like this is enough to calculate cube !
    Code:
    function cube(n){return n*n*n}
    A function can not return a sequence of variables like um1Cubed, num2Cubed, num3Cubed, num4Cubed, num5Cubed. Its can return a value, an array or an object.

    The arguments of a function are always available in the arguments array. Then the average function can be write
    Code:
    function average(){var lng=arguments.length,sum=0;
       while (lng--) sum+=arguments[lng];
       return sum/lng;
    }
    Such a function is available for one or more arguments and particularly with function average(num1,num2,num3,num4,num5).

    To make progress, be really lazy and write the least possible ...
    Last edited by 007julien; 02-04-2014 at 11:56 AM.

  3. #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 007julien View Post
    Computer scientists have invented loops not to repeat the same thing many times...

    A function can be called with different arguments. Then something like this is enough to calculate cube !
    Code:
    function cube(n){return n*n*n}
    A function can not return a sequence of variables like num1Cubed, num2Cubed, num3Cubed, num4Cubed, num5Cubed. Its can return a value, an array or an object.

    The arguments of a function are always available in the arguments array. Then the average function can be write
    Code:
    function average(){var lng=arguments.length,sum=0;
       while (lng--) sum+=arguments[lng];
       return sum/lng;
    }
    Such a function is available for one or more arguments and particularly with function average(num1,num2,num3,num4,num5).

    To make progress, be really lazy and write the least possible ...


    It is for an assignment i need write a function and then return it to a variable outside of the function itself and display it


    as for the cubing should i do each one in a separate function to work then

  4. #4
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Try this
    Code:
    <!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">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    
    </style>
    </head>
    <body>
    <div id="rsp"></div>
    <script type="text/javascript">
    var nmb=[],ord=['first','second','third','fourth','fifth'];
    
    function cube(n){return n*n*n;
    }
    function average(){var lng=arguments.length,sum=0;
       while (lng--) sum+=+arguments[lng];
       return (sum/arguments.length);
    }
    function write(n){document.getElementById('rsp').innerHTML+='<p>'+n+' cubed is '+cube(n)+'</p>';
    }
    
    for (var i=0;i<ord.length;i++) {
    	nmb[i]=window.prompt('Insert your '+ord[i]+' number');
    	write(nmb[i])}
    
    document.write('The of original number average is : '+average(nmb[0],nmb[1],nmb[2],nmb[3],nmb[4]))
    </script>
    </body>
    </html>
    Last edited by 007julien; 02-04-2014 at 03:41 PM.

  5. #5
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,430
    Thanks
    30
    Thanked 878 Times in 876 Posts
    Quote Originally Posted by nickbowen1991 View Post
    ....i do each one in a separate function to work then
    No

    Study this:
    Code:
    <script type="text/javascript">
    function cube(num){
    	return(num * num * num);
    }
    
    var num1 = window.prompt("insert your first number");
    var num2 = window.prompt("insert your second number");
    var num3 = window.prompt("insert your third number");
    var num4 = window.prompt("insert your fourth number");
    var num5 = window.prompt("insert your fifth number");
    
    document.write(
    num1+' cubed is '+cube(num1)+'<br />'
    +num2+' cubed is '+cube(num2)+'<br />'
    +num3+' cubed is '+cube(num3)+'<br />'
    +num4+' cubed is '+cube(num4)+'<br />'
    +num5+' cubed is '+cube(num5)+'<br />');
    </script>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Tags for this Thread

Posting Permissions

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