Skip to content
Snippets Groups Projects
Commit 4e364242 authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding week1 lecture code examples

parent cee515fe
No related branches found
No related tags found
No related merge requests found
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Input/Output</title>
</head>
<body>
<script>
"use strict";
document.writeln("<strong>" + "Bill Calculation System</strong><br />");
var costPerCredit, numberOfCredits, tuitionCost;
/* Reading values from the user */
costPerCredit = Number(prompt("Enter cost per credit:"));
numberOfCredits = prompt("Enter number of credits:");
/* Computing cost */
tuitionCost = costPerCredit * numberOfCredits;
document.writeln("<strong>Tuition Cost: </strong>" + tuitionCost);
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Network</title>
</head>
<body>
<script>
"use strict";
/* You need to allow browser pop-ups to run this program */
var network = prompt("Enter network name (abc, cbs)");
alert("Network specified is: " + network);
var webSite;
switch (network) {
case "abc":
webSite = "http://www.abc.com/";
break;
case "cbs":
webSite = "http://www.cbs.com/";
break;
default:
webSite = "http://www.cnn.com/";
break;
}
window.open(webSite);
</script>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>JS Example</title>
</head>
<body>
<script>
"use strict";
main();
function main() {
document.writeln("Value: " + evaluate(200));
}
function evaluate(x) {
document.writeln("Value of y: " + y + "<br>"); // undefined but does not throw an error
if (x >= 100) {
var y = x * 100;
}
return y; // accessible
}
</script>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Square Root Table</title>
</head>
<body>
<script>
"use strict";
var currValue = 0,
maximumValue;
/* Reading a value from the user and verifying is correct */
do {
maximumValue = Number(prompt("Enter a value"));
if (maximumValue < 0)
alert("Invalid value: " + maximumValue);
} while (maximumValue < 0);
/* Generating the table */
// document.writeln writes a string of text followed by a newline
// character to a document. Try also document.write(...)
document.writeln("<table border=\"10\">");
document.writeln("<caption><strong>Square Root Table</strong></caption>");
document.writeln("<tr><th>Number</th><th>Square Root</th></tr>");
while (currValue <= maximumValue) {
document.writeln("<tr><td>" + currValue + "</td><td>" +
Math.sqrt(currValue) + "</td></tr>");
currValue = currValue + 1;
}
document.writeln("</table>");
</script>
</body>
</html>
\ No newline at end of file
<!doctype html>
<head>
<meta charset="utf-8" />
<title>JS Example</title>
</head>
<body>
<script>
"use strict";
/* strict mode requires var */
var bookTitle = "JavaScript";
/* Following variable name not allowed in strict mode */
/* var interface = 10; */
alert(bookTitle);
</script>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>String Methods</title>
</head>
<body>
<script>
"use strict";
main();
function main() {
var str1 = prompt("Enter string value");
var str2 = prompt("Enter string value"),
result;
/* string comparison */
if (str1 < str2) {
document.writeln(str1 + " < " + str2);
} else if (str1 > str2) {
document.writeln(str1 + " > " + str2);
} else {
document.writeln("str1 equal to str2");
}
if (str1.includes(str2)) {
document.writeln("<br>" + str1 + " includes " + str2);
} else {
document.writeln("<br>" + str1 + " does not include " + str2);
}
if (str1.startsWith("the")) {
document.writeln("<br>" + str1 + " starts with \"the\"");
} else {
document.writeln("<br>" + str1 + " does not start with \"the\"");
}
if (str1.endsWith("ing")) {
document.writeln("<br>" + str1 + " ends with \"ing\"");
} else {
document.writeln("<br>" + str1 + " does not end with \"ing\"");
}
document.writeln("<br>\"Fear the turtle\".indexOf(\"the\")&rarr;" +
"Fear the turtle".indexOf("the"));
document.writeln("<br>\"Fear the turtle\".indexOf(\"boat\")&rarr;" +
"Fear the turtle".indexOf("boat"));
document.writeln("<br>\"Fear the turtle\".indexOf(\"\")&rarr;" + "Fear the turtle"
.indexOf(""));
document.writeln("<br>\"Fear the turtle\".lastIndexOf(\"e\")&rarr;" +
"Fear the turtle".lastIndexOf("e"));
document.writeln("<br>" + str1 + ".repeat(5)&rarr;" + str1.repeat(5));
document.writeln("<br>" + "\"Feartheturtle\".slice(2, 7) " + "Feartheturtle".slice(
2, 7));
document.writeln("<br>" + "\"Feartheturtle\".slice(-7) " + "Feartheturtle".slice(-7));
str1 = prompt("Enter comma separated string");
var strArray = str1.split(","),
i;
document.writeln("String components:<br>")
for (i = 0; i < strArray.length; i++) {
document.writeln(strArray[i] + " Length: " + strArray[i].length + "<br>");
}
}
</script>
</body>
</html>
\ No newline at end of file
console.log(isNaN(0));
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<article class = "names">
<div>
<p>
Text that is styled
</p>
</div>
</article>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>charAt Example</title>
</head>
<body>
<script>
"use strict";
var name = "Testudo";
var letter = name.charAt(1); // autoboxing takes place
document.writeln("Letter is " + letter + "<br>");
// JavaScript engine processing for above code
name = "Testudo";
var x = new String(name);
letter = x.charAt(1);
//x = null; // x will be destroyed once is no longer needed
document.writeln("Letter is " + letter);
</script>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Wrapper object type example</title>
</head>
<body>
<script>
"use strict";
var name = "Testudo";
var objName = new String("Testudo");
if (name == objName) {
document.writeln('Same value');
} else {
document.writeln('Different value');
}
document.writeln("<br>")
if (name === objName) {
document.writeln('Same type!');
} else {
document.writeln('Different type');
}
</script>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment