Tuesday, January 20, 2026

JAVSCRIPT AND SOLUTION

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);


Practical 10: Increment & Decrement

Use the increment (++) and decrement (--) operators in a JavaScript code;

The Concept: Fast ways to add or subtract exactly 1.

++ : Adds 1.

-- : Subtracts 1.

let count = 0;

count++; // Now count is 1
count++; // Now count is 2
count--; // Now count is 1

document.write("Count is: " + count);

No comments:

Post a Comment