1a. 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.
Program 1: HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Console and ALERT</title>
</head>
<body>
<script src="pgm1.js"></script>
</body>
</html>
Program 1: Java Script(pgm1.js)
console.log("Hello World");
let n1=parseFloat(prompt("Enter the first Number"));
let n2=parseInt(prompt("Enter the Second Number"));
res=n1+n2;
console.log(res);
alert("The sum is " + res);
Program 1b: 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.
// Declaration of Array
cities=["New Dehli","Mumbai", "Bengaluru","Kolkata", "Chennai"];
// Printing Array Element
console.log(cities);
//printing length of Array
console.log(cities.length);
//inserting the Element into array at the end
cities.push("Hyderabad")
console.log(cities);
//Deleting the Element from array
delete cities[0];
console.log(cities);
//Find and log the index of a specific city.
console.log (cities. indexOf("Mumbai"));
// Final Array Elements
console.log(cities);
2. Read a string from the user, Find its length. Extract the word "JavaScript" using
substring () or slice (). Replace one word with another word and log the new string.
Write a function isPalindrome(str) that checks if a given string is a palindrome (reads
the same backward).
Note: include this JavaScript into HTML
//Read a string from the user
str1=prompt("Enter the string with JavaScript in it")
console.log(str1)
//Find its length
console.log("Length of String Entered "+str1.length)
// Extract the word "JavaScript" using substring() or slice().
ind=str1.indexOf("JavaScript")
console.log("Index at which JavaScript found "+ind)
let res= str1.substring(ind, ind+10);
console.log("Extracting the JavaScript string "+res)
let newstring = str1.replace("JavaScript", "JS");
console.log("String after replacing the JavaScript: "+ newstring)
function isPalindrome(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
str2=prompt("Enter the String to check whether it is Palindrome or not");
var res1=isPalindrome(str2);
if (res1==true)
{
console.log(str2+ " is palindrome");
}
else
{
console.log(str2+ " is not palindrome")
}
3. Create an object student with properties: name (string), grade (number), subjects
(array), displayInfo() (method to log the student's details) Write a script to
dynamically add a passed property to the student object, with a value of true or
false based on their grade. Create a loop to log all keys and values of the student
object.
// Create an object student with properties
student={nam:"Rama",grade:58,subjects:["Science","Mathematics", "English"] };
// method to log the student's details
function displayinfo()
{
console.log(student);
}
displayinfo();
/*passed property to the student object, with a value of true or false based on their grade. */
if (student.grade>40)
console.log("Pass");
else
console.log("Fail")
// loop to log all keys
console.log ("The KEYS of Student object are")
for (let key in student)
{
console.log(key);
}
//loop to log all Values
console.log ("The VALUES of Student object are")
for (let key in student)
{
let value;
// get the value
value = student[key];
console.log(value);
}