Practical 5: Output Functions
Apply output functions: innerHTML, document.write( ) and window.alert( ) to display the output;
alert() (The Pop-up)
alert("Hello Class!");
document.write() (Writing on the page)
document.write("I am writing on the screen.");
innerHTML (Changing an existing tag) Explain: You need an ID to tell JS exactly where to change the text.
<p id="abc">Old Text</p>
<script>
document.getElementById("abc").innerHTML = "New Text!";
</script>
Practical 6: Input Functions
Write a JavaScript code that uses input functions prompt( );
let name = prompt("What is your name?");
alert("Hello " + name);
Practical 7 & 8: Math in JavaScript
Convert arithmetic expression into JavaScript code;
Use the arithmetic operators of JavaScript to solve an arithmetic problem;
let x = 10;
let y = 5;
let result = (x + y) * 2; // Math: (10 + 5) x 2
document.write("The answer is: " + result);
Practical 9: Assignment Operators
Use the following assignment operators in a JavaScript code:
a. assignment operator (=),
b. compound assignment operator (+ =, - =, * =, / =, % =);
= : Sets the value (The "Equal" sign).
+= : Adds to the current value.
let score = 10;
score += 5; // Now score is 15 (10 + 5) score = score + 5
score -= 2; // Now score is 13 (15 - 2) score = score - 2
document.write("Final score: " + score);
No comments:
Post a Comment