Results 1 to 6 of 6

Thread: JavaScript

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User CYPER's Avatar
    Join Date: May:2004
    Location:
    Posts: 9,357

    JavaScript



    :

    4 window.prompt - "" "" . .

    /:

    ""


    ""



    .

    :

    Code:
    var heightArray = new Array (4);
    var lengthArray = new Array (4);
    var radiusArray = new Array (4);
    var shape = new Array (4);
    var counter2 = 0;
    var rectangle = 1;
    
    for (var counter = 0; counter < 4; counter = counter + 1)
    {shape[counter] = window.prompt('Please enter r for rectangle or c for circle.')}
    
    for (var counter1 = 0; counter1 < 4; counter1 = counter1 + 1)
    if (shape[counter1] == 'r')
    {heightArray[counter2] = window.prompt('Please enter the height of rectangle number ' + rectangle + ' in centimetres')
    lengthArray[counter2] = window.prompt('Please enter the length of rectangle number ' + rectangle + ' in centimetres')
    counter2 = counter2 + 1
    rectangle = rectangle + 1}
    Gigabyte X570 AORUS Ultra | 5950X | Arctic Liquid Freezer II 240 | Corsair Vengeance LPX 2x8GB 3200Mhz | Samsung 980 Pro 1TB + Samsung 970 Pro 1TB | Gigabyte AORUS GeForce RTX 3080 Ti Master 12GB | Fractal Design Meshify C Dark TG | Corsair AX860i | Samsung U32H850 32" 4K

  2. #2
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215
    , ...

    HTML Code:
    var heightArray 	= new Array (4);
    var lengthArray 	= new Array (4);
    var radiusArray 	= new Array (4);
    var shape 			= new Array (4);
    var arrArea			= new Array(4);
    
    var circCount 		= 0;
    var rectCount 		= 0;
    
    i = 0;
    while( i < 4 )
    {
    	chr = window.prompt('Please enter r for rectangle or c for circle.');
    	if( chr == 'c' || chr == 'r' )
    	{
    		shape[i] = chr;
    		i++;
    	}
    }
    
    for ( var counter1 = 0; counter1 < 4; counter1 = counter1 + 1)
    {
    	if (shape[counter1] == 'r')
    	{
    		heightArray[rectCount] = parseFloat( window.prompt('Please enter the height of rectangle number ' + ( rectCount+1 ) + ' in centimetres') );
    		lengthArray[rectCount] = parseFloat( window.prompt('Please enter the length of rectangle number ' + ( rectCount+1 ) + ' in centimetres') );
    		
    		arrArea[counter1]	  = heightArray[rectCount]*lengthArray[rectCount];
    		
    		rectCount++;
    	}
    	else if( shape[counter1] == 'c' )
    	{
    		radiusArray[circCount] = parseFloat( window.prompt('Please enter the radius of circle number ' + ( circCount+1 ) + ' in centimetres') );
    		
    		arrArea[counter1]	  = 3.14 * radiusArray[circCount] * radiusArray[circCount];
    		
    		circCount++;
    	}
    	else
    	{
    		alert( "undefined shape no "+(counter1+1)+": "+shape[counter1] );
    	}
    }
    
    for( q = 0; q<4; q++ )
    {
    	switch( shape[q] )
    	{
    		case 'c':
    			document.write( "The area of shape "+(q+1)+" ( circle ) is "+arrArea[q].toFixed( 2 )+"<br />");
    			break;
    		case 'r':
    			document.write( "The area of shape "+(q+1)+" ( rectangle ) is "+arrArea[q].toFixed( 2 )+"<br />");
    			break;
    		default:
    			alert( "undefined shape no "+(q+1)+": "+shape[q] );
    			break;
    	}
    }

  3. #3
    Registered User CYPER's Avatar
    Join Date: May:2004
    Location:
    Posts: 9,357
    ,
    , . genn

    , :


    2

    function roundToTwoPlaces (originalNumber)

    {
    var roundedNumber = 0.0;
    roundedNumber = Math.round(originalNumber * 100) / 100;
    return roundedNumber;
    }


    function convertToCentimetres (xxx)
    {
    if (units == 'Inches' || units == 'inches')
    {
    return xxx * 2.54
    }
    else
    {return xxx * 1
    }
    }

    var heightArray = new Array (4);
    var lengthArray = new Array (4);
    var radiusArray = new Array (4);
    var shape = new Array (4);
    var area = new Array(4);
    var cCount = 0;
    var rCount = 0;


    var units = window.prompt('Please enter Inches or Centimetres for the desired units input')

    for (var counter1 = 0; counter1 < 4; counter1 = counter1 + 1)
    {
    shape[counter1] = window.prompt('Please enter the name of shape number ' + (counter1 + 1));
    }


    for (var counter2 = 0; counter2 < 4; counter2 = counter2 + 1)
    if (shape[counter2] == 'rectangle' || shape[counter2] == 'Rectangle')
    {
    heightArray[rCount] = parseFloat( window.prompt('Please enter the height of rectangle number ' + (rCount + 1) + ' in ' + units) );
    lengthArray[rCount] = parseFloat( window.prompt('Please enter the length of rectangle number ' + (rCount + 1) + ' in ' + units) );

    area[counter2] = convertToCentimetres(roundToTwoPlaces(he ightArray[rCount]*lengthArray[rCount]));

    rCount = rCount + 1;}
    else
    if(shape[counter2] == 'circle' || shape[counter2] == 'Circle')
    {radiusArray[cCount] = parseFloat( window.prompt('Please enter the radius of circle number ' + (cCount + 1) + ' in ' + units) );
    area[counter2] = convertToCentimetres(roundToTwoPlaces(3. 142 * radiusArray[cCount] * radiusArray[cCount]));
    cCount = cCount + 1;}
    else
    {document.write('There is something wrong with input number ' + (counter2 + 1) + '. Please try again by refreshing the page ' + '<BR>')}
    {document.write('<BR>')}

    for (var counter3 = 0; counter3 < 4; counter3 = counter3 + 1)
    {document.write('The area of shape number ' + (counter3 + 1) + ' which is ' + shape[counter3] + ' is ' + area[counter3] + ' square centimetres ' + '<BR>')}
    Gigabyte X570 AORUS Ultra | 5950X | Arctic Liquid Freezer II 240 | Corsair Vengeance LPX 2x8GB 3200Mhz | Samsung 980 Pro 1TB + Samsung 970 Pro 1TB | Gigabyte AORUS GeForce RTX 3080 Ti Master 12GB | Fractal Design Meshify C Dark TG | Corsair AX860i | Samsung U32H850 32" 4K

  4. #4
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215
    , ... . - 3 . , ?

    - window.confirm, . - . Cancel - ... , ...

    , , . , .

    ... . - " " " ". , 4 ... , /. , ... , user-friendly. - . . "" , . ...
    , input, ,

  5. #5
    Registered User CYPER's Avatar
    Join Date: May:2004
    Location:
    Posts: 9,357

    :)

    genn, . - , - JavaScript , ( Java) . - , , . , functions - -. , - - case swtich , , .

    Gigabyte X570 AORUS Ultra | 5950X | Arctic Liquid Freezer II 240 | Corsair Vengeance LPX 2x8GB 3200Mhz | Samsung 980 Pro 1TB + Samsung 970 Pro 1TB | Gigabyte AORUS GeForce RTX 3080 Ti Master 12GB | Fractal Design Meshify C Dark TG | Corsair AX860i | Samsung U32H850 32" 4K

  6. #6
    Registered User genn's Avatar
    Join Date: Feb:2003
    Location:
    Posts: 1,215
    switch ... case

    if()
    ...
    else if()
    ...
    else
    ...


    toFixed( < > ). , document.write,


Posting Permissions

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

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |