Commit d598a3f4 authored by dbhuller's avatar dbhuller

added examples for switch and while/do-while loops

parent 6f12efb0
......@@ -87,6 +87,21 @@
document.write("<br>")
arrForOf(myArr);
</script>
<h1>Switch Statement</h1>
<script>
switchExample(0);
switchExample(1);
switchExample(2);
switchExample(3);
switchExample(4);
</script>
<h1>While/Do-While Loop</h1>
<script>
document.write("While Loop:<br> ");
whileExample();
document.write("Do-While loop: ")
doWhileExample();
</script>
</body>
</html>
\ No newline at end of file
......@@ -38,8 +38,8 @@ function add(a, b) {
function conditionalDemo(arg) {
let myArg = "Deep";
if(arg == myArg) {
document.write("You entered the same arg. Entered: " + arg + ", " + " Expected: " + myArg);
if (arg == myArg) {
document.write("You entered the same arg. Entered: " + arg + ", " + " Expected: " + myArg);
} else {
document.write("You didnt enter the same arg. Entered: " + arg + ", " + " Expected: " + myArg);
}
......@@ -47,14 +47,68 @@ function conditionalDemo(arg) {
function arrForIn(arr) {
document.write("With for-in: ");
for( let val in arr) {
document.write(val);
}
for (let val in arr) {
document.write(val);
}
}
function arrForOf(arr) {
document.write("With for-of: ")
for(let val of arr) {
document.write(val);
}
for (let val of arr) {
document.write(val);
}
}
function switchExample(switchVal) {
switch (switchVal) {
case 0:
document.write("zero<br>");
break;
case 1:
document.write('one<br>');
break;
case 2:
document.write('two<br>');
break;
case 3:
document.write('three<br>');
break;
case 4:
document.write('four<br>');
break;
case 5:
document.write('five<br>');
break;
case 6:
document.write('six<br>');
break;
case 7:
document.write('seven<br>');
break;
case 8:
document.write('eight<br>');
break;
case 9:
document.write('nine<br>');
break;
}
}
function whileExample() {
let x = 0;
while (x < 10) {
document.write(x + "<br>");
x++;
}
}
function doWhileExample() {
let result = '';
let i = 0;
do {
i = i + 1;
result = result + i;
} while (i < 5);
document.write(result);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment