FSD LAB - Program - 1 - To - 4
FSD LAB - Program - 1 - To - 4
1. a. Write a script that Logs "Hello, World!" to the console. Create a script that
calculates the sum of two numbers and displays the result in an alert box.
b. Create an array of 5 cities and perform the following operations:
Log the total number of cities. Add a new city at the end. Remove the first city.
Find and log the index of a specific city.
1. Install Node.js:
o Install: Run the downloaded installer and follow the on-screen instructions
to install Node.js. This will also install npm (Node Package Manager), which is
useful for managing JavaScript packages.
o Text Editor: Use a text editor like Visual Studio Code or any other text
editor of your choice.
o Save Your Script: Save the above JavaScript code in a file with a .js
extension, e.g., script.js.
shell
node script.js
This will execute the JavaScript code and you will see the output in
the console.
By following these steps, you can run and test the JavaScript programs on your
Windows desktop.
node -v
experiment1a.js
console.log("Hello, World!");
// Script to calculate the sum of two numbers and display the result in an alert box
function calculateSum(a, b) {
return sum = a + b;
// Example usage
} else {
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script src="experiment1a.js"></script>
</body>
</html>
o/p:
Some other way like whole thing implemented in single html file
1a.html
=======================
<!DOCTYPE html>
<head>
<title>Dynamic Input Example</title>
<script>
console.log("Hello, World!");
// Script to calculate the sum of two numbers and display the result in an alert box
// Validate input
// Example usage
</script>
</head>
</html>
o/p:
1b.js
cities.push(newCity);
console.log("Removing the first city:", cities[0]); // Log the first city before removal
cities.shift();
console.log("Index of", searchCity + ":", cityIndex !== -1 ? cityIndex : "City not found");
1bb.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="1b.js"></script>
</body>
</html>
Execution
Next run
npm install prompt-
JavaScript code
const prompt = require('prompt-sync')();
function isPalindrome1(str) {
for (let i = 0; i < str.length / 2; i++) {
if (str.charAt(i) != str.charAt(str.length - 1 - i)) {
return false;
}
}
return true;
}
HTML code
<!DOCTYPE html>
<head>
<title>String Manipulation</title>
<script>
function isPalindrome1(str) {
{ return false;
return true;
function isPalindrome(str) {
cleanedStr.split('').reverse().join('');
isPalindrome(userInput);
// Log results
// Display alerts
</script>
</head>
</html>
o/p:
One student
<!DOCTYPE html>
<head>
<script>
function createStudent() {
let student = {
};
return student;
function displayInfo(student) {
console.log("Student Details:");
// Loop through the properties of the student object and log the keys and values
console.log(`${key}: ${student[key]}`);
}
// Log whether the student passed based on the 'passed' function
console.log(" ");
displayInfo(student);
</script>
</head>
</html>
Many students
<!DOCTYPE html>
<head>
<script>
function createStudent() {
let student = {
passed: function() {
};
return student;
function displayInfo(student) {
console.log("Student Details:");
// Loop through the properties of the student object and log the keys and values
console.log(`${key}: ${student[key]}`);
console.log(" ");
displayInfo(student);
</script>
</head>
</html>
o/p:
4. Create a button in your HTML with the text "Click Me". Add an event listener to log
"Button clicked!" to the console when the button is clicked. Select an image and add
a
mouseover event listener to change its border color. Add an event listener to the document
that logs the key pressed by the user.
HTML Code
<!DOCTYPE html>
<head>
</head>
<body>
<script>
document.getElementById('clickButton').addEventListener('click', function() {
console.log("Button clicked!");
});
// Event listener for mouseover on the image to change its border color
document.getElementById('exampleImage').addEventListener('mouseover', function() {
document.addEventListener('keydown', function(event) {
});
// Event listener for mouseout on the image to change its border color
document.getElementById('exampleImage').addEventListener('mouseout', function() {
});
</script>
</body>
</html>
o/p: