Practica 2
Practica 2
Practica 2
DESARROLLO WEB
Problema1 :
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Sum the Integers from 1 to 10</title>
<script>
var sum; // stores the total
var x; //counter control variable
x = 1;
sum = 0;
while ( x <= 10 )
{
sum += x;
++x;
} // end while
document.writeln( "The sum is: " + sum );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Exercise 7.7b: ex07_07b.html -->
<html>
<head>
<meta charset = "utf-8">
<title>Finding Code Errors</title>
<script>
var gender;
gender = window.prompt( "Enter gender"
+ " (1 = Woman, 2 = Man)", "1" );
if ( gender == 1 )
document.writeln( "Woman");
else
document.writeln( "Man" );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Exercise 8.7a: ex08_07a.html -->
<html>
<head>
<meta charset = "utf-8">
<title>Finding Code Errors</title>
<script>
var c;
var product;
c = 1;
product = 1;
while ( c <= 5 )
{
product *= c;
++c;
} // end while
document.writeln( "The product is: " + product );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 7.7: average.html -->
<!-- Counter-controlled repetition to calculate a class average. -->
<html>
<head>
<meta charset = "utf-8">
<title>Class Average Program</title>
<script>
var total; // sum of grades
var
var
var
var
// Initialization Phase
total = 0; // clear total
gradeCounter = 1; // prepare to loop
// Processing Phase
while ( gradeCounter <= 10 ) // loop 10 times
{
// prompt for input and read grade from user
grade = window.prompt( "Enter integer grade:", "0" );
// convert grade from a string to an integer
gradeValue = parseInt( grade );
// add gradeValue to total
total = total + gradeValue;
// add 1 to gradeCounter
gradeCounter = gradeCounter + 1;
} // end while
// Termination Phase
average = total / 10; // calculate the average
// display average of exam grades
document.writeln(
"<h1>Class average is " + average + "</h1>" );
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<!-- Fig. 7.9: average2.html -->
<!-- Sentinel-controlled repetition to calculate a class average. -->
<html>
<head>
<meta charset = "utf-8">
<title>Class Average Program: Sentinel-controlled Repetition</title>
<script>
var
var
var
var
var
// Initialization phase
total = 0; // clear total
gradeCounter = 0; // prepare to loop
// Processing phase
// prompt for input and read grade from user
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
// convert grade from a String to an integer
gradeValue = parseInt( grade );
while ( gradeValue != -1 )
{
// add gradeValue to total
total = total + gradeValue;
// add 1 to gradeCounter
gradeCounter = gradeCounter + 1;
// prompt for input and read grade from user
grade = window.prompt(
"Enter Integer Grade, -1 to Quit:", "0" );
// convert grade from a String to an integer
gradeValue = parseInt( grade );
} // end while
// Termination phase
if ( gradeCounter != 0 )
{
"<h3>Preincrementing</h3>" );
"<p>" + c ); // prints 5
prints 6
" " + ++c );
" " + c + "</p>" ); // prints 6
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Exercise 8.5: ex08_05.html -->
<html>
<head>
<meta charset = "utf-8">
<title>Mystery</title>
<script>
document.writeln( "<table>" );
for ( var i = 1; i <= 10; i++ )
{
document.writeln( "<tr>" );
for ( var j = 1; j <= 5; j++ )
document.writeln( "<td>(" + i + ", " + j + ")</td>" );
document.writeln( "</tr>" );
} // end for
document.writeln( "</table>" );
</script>
</head><body />
</html>
<!DOCTYPE html>
<!-- Fig. 8.1: WhileCounter.html -->
<!-- Counter-controlled repetition. -->
<html>
<head>
<meta charset= "utf-8">
<title>Counter-Controlled Repetition</title>
<script>
var counter = 1; // initialization
while ( counter <= 10) // repetition condition
{
document.writeln( "<p style = 'font-size: " +
counter + "ex'>HTML5 font size " + counter + "ex</p>" );
++counter; // increment
} //end while
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.2: ForCounter.html -->
<!-- Counter-controlled repetition with the for statement. -->
<html>
<head>
<meta charset="utf-8">
<title>Counter-Controlled Repetition</title>
<script>
// Initialization, repetition condition and
// incrementing are all included in the for
// statement header.
for ( var counter = 1; counter <= 7; ++counter )
document.writeln( "<p style = 'font-size: " +
counter + "ex'>HTML5 font size " + counter + "ex</p>" );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.5: Sum.html -->
<!-- Summation with the for repetition structure. -->
<html>
<head>
<meta charset = "utf-8">
<title>Sum the Even Integers from 2 to 100</title>
<script>
var sum = 0;
for ( var number = 2; number <= 100; number += 2)
sum += number;
document.writeln( "The sum of the even integers " +
"from 2 to 100 is " + sum );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
} //end for
document.writeln( "</tbody></table>" );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.7: SwitchTest.html -->
<!-- Using the switch multiple-selection statement. -->
<html>
<head>
<meta charset = "utf-8">
<title>Switching between HTML5 List Formats</title>
<script>
var
var
var
var
var
break;
default:
validInput = false;
break;
} //end switch
if ( validInput === true )
{
document.writeln( listType + startTag );
for ( var i = 1; i <= 10; ++i )
document.writeln( "<li>List item " + i + "</li>" );
document.writeln( endTag );
} //end if
else
document.writeln( "Invalid choice: " + choice );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.9: DoWhileTest.html -->
<!-- Using the do...while repetition statement. -->
<html>
<head>
<meta charset = "utf-8">
<title>Using the do...while Repetition Statement</title>
<script>
var counter = 1;
do {
document.writeln( "<h" + counter + ">This is " +
"an h" + counter + " level head" + "</h" +
counter + ">" );
++counter;
} while ( counter <= 6 );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.12: ContinueTest.html -->
<!-- Using the continue statement in a for statement. -->
<html>
<head>
<meta charset = "utf-8">
<title>
Using the continue Statement in a for Statement
</title>
<script>
for ( var count = 1; count <= 10; ++count )
{
if ( count == 5 )
continue; // skip remaining loop code only if count == 5
document.writeln( count + " " );
} //end for
document.writeln( "<p>Used continue to skip printing 5</p>" );
</script>
</head><body></body>
</html>
<!DOCTYPE html>
<!-- Fig. 8.11: BreakTest.html -->
<!-- Using the break statement in a for statement. -->
<html>
<head>
<meta charset = "utf-8">
<title>
Using the break Statement in a for Statement
</title>
<script>
for ( var count = 1; count <= 10; ++count )
{
if ( count == 5 )
break;
document.writeln( count + " " );
} //end for
document.writeln(
"<p>Broke out of loop at count = " + count + "</p>" );
</script>
</head><body></body>
</html>