0% found this document useful (0 votes)
20 views7 pages

Week 5 Codes

The document contains multiple HTML code snippets demonstrating various programming concepts in JavaScript, including conditional statements (if-else, switch), loops (while, do-while, for), and control flow (break, continue, nested loops). Each code snippet prompts for user input and provides different outputs based on the conditions set. The examples serve as practical illustrations of fundamental programming constructs.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Week 5 Codes

The document contains multiple HTML code snippets demonstrating various programming concepts in JavaScript, including conditional statements (if-else, switch), loops (while, do-while, for), and control flow (break, continue, nested loops). Each code snippet prompts for user input and provides different outputs based on the conditions set. The examples serve as practical illustrations of fundamental programming constructs.

Uploaded by

thembi malinga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CODE 1: IF-ELSE STATEMENT

<html>

<head>

<title>Conditional Statement</title>

</head>

<body>

<h3>

<script type="text/javascript">

var age=prompt("How old are you? ","");

if( age >= 55 ){

document.write("You pay the senior fare! ");

else{

document.write("You pay the regular adult fare. ");

</script>

</h3>

</body>

</html>

CODE 2: OPERATORS

<html>

<head>

<title>Conditional Operator</title>

</head>

<body bgcolor="lightblue">

<big>

<script type ="text/javascript">

var age = prompt("How old are you? ", "");

var price = (age > 55 ) ? 0 : 7.50;


alert("You pay $" + price + 0);

</script>

</big>

</body>

</html>

CODE 3: IF/ELSE BLOCK

<html>

<head>

<title>Conditional Flow Control</title>

</head>

<body>

<h2>

<script type="text/javascript">

var age=eval( prompt("How old are you? ",""));

if( age > 0 && age <= 12 ){

alert("You pay the child's fare. ");

else if( age > 12 && age < 60 ){

alert("You pay the regular adult fare. ");

else {

alert("You pay the senior fare! ");

</script>

</h3>

</body>

</html>
CODE 4: SWITCH STATEMENT

<html>

<head>

<title>The Switch Statement</title>

</head>

<body>

<script type="text/javascript">

var day_of_week=Math.floor((Math.random()* 7)+1);

// Get a random number between 1 and 7

// Monday is 1, Tuesday is 2, etc.

switch(day_of_week){

case 1:

case 2:

case 3:

case 4: alert("Business hours Monday through Thursday are from 9am to 10pm");

break;

case 5:

alert("Business hours on Friday are from 9am to 6pm");

break;

case 6:

alert("Business hours on Saturday are from 11am to 3pm");

break;

default:

alert("We are closed on Sundays and holidays");

break;

</script>

</body>

</html>
CODE 5: WHILE LOOP

<html>

<head>

<title>Looping Constructs</title>

</head>

<body>

<h2>While Loop</h2>

<script type="text/javascript">

var i=0; // Initialize loop counter

while ( i < 10 ){ // Test

document.writeln(i +” ”);

i++; // Increment the counter

} // End of loop block

</script>

</body>

</html>

CODE 6: DO-WHILE LOOP

<html>

<head>

<title>Looping Constructs</title>

</head>

<body>

<h2>Do While Loop</h2>

<script type="text/javascript">

var i=0;

do{

document.write(i);

i++;

} while ( i < 10 );
</script>

</body>

</html>

CODE 7: FOR LOOP

<html>

<head>

<title>Looping Constructs</title>

</head>

<body>

<h2>For Loop</h2>

<font size="+2">

<script type="text/javascript">

for(var i = 0; i < 10; i++){

document.write(i);

</script>

</font>

</body>

</html>

CODE 5: BREAK AND CONTINUE

<html>

<head>

<title>Looping Constructs</title>

</head>

<body>

<script type="text/javascript">

while(true) {
var grade=eval(prompt("What was your grade? ",""));

if (grade < 0 || grade > 100) {

alert("Illegal choice!");

continue; // Go back to the top of the loop

if(grade > 89 && grade < 101)

{alert("Wow! You got an A!");}

else if (grade > 79 && grade < 90)

{alert("You got a B");}

else if (grade > 69 && grade < 80)

{alert("You got a C");}

else if (grade > 59 && grade < 70)

{alert("You got a D");}

else {alert("Study harder. You Failed.");}

answer=prompt("Do you want to enter another grade?","");

if(answer != "yes"){

break; // Break out of the loop

</script>

</body>

</html>

CODE 6: NESTED LOOPS

<html>

<head>

<title>Nested loops</title>

</head>

<body>

<script type="text/javascript">
var str = "@";

for ( var row = 0; row < 6; row++){

for ( var col=0; col < row; col++){

document.write(str);

document.write("<br />");

</script>

</body>

</html>

CODE 6: NESTED LOOPS 2

<html>

<head>

<title>Nested loops 2</title>

</head>

<body>

<script type="text/javascript">

outerLoop: for ( var row = 0; row < 10; row++){

for ( var col=0; col <= row; col++){

document.write("row "+ row +"|column " + col, "<br />");

if(col==3){

document.write("Breaking out of outer loop at column " + col +"<br />");

break outerLoop;

document.write("************<br />");

</script>

</body>

</html>

You might also like