Java Script25
Java Script25
Develop a JavaScript program to Capitalize the first letter of each Word of a sentence
(String)
function capitalizeFirstLetterOfEachWord(sentence) {
if (word.length > 0) {
} else {
return word;
}).join(' ');
console.log(capitalizedSentence);
function startsWithJava(str) {
return str.startsWith('Java');
// Example usage:
function reverseString(str) {
return str.split('').reverse().join('');
// Example usage:
html
<!DOCTYPE html>
<html>
<head>
<title>Alert Example</title>
<script>
function showAlert(message) {
alert(mezssage);
</script>
</head>
<body>
</body>
</html>
3b. Develop a JavaScript program to display a confirm message on a button click.
html
<!DOCTYPE html>
<html>
<head>
<title>Confirm Example</title>
<script>
function showConfirm() {
// Display a confirmation dialog
const userConfirmed = confirm("Are you sure you want to proceed?");
html
<!DOCTYPE html>
<html>
<head>
<title>Prompt Example</title>
<script>
function showPrompt() {
// Display a prompt dialog to get user input
const userInput = prompt("Please enter your name:");
<!DOCTYPE html>
<html>
<head>
<title>Multiplication and Division</title>
<script>
function multiplyNumbers() {
const num1 = parseFloat(document.getElementById("number1").value);
const num2 = parseFloat(document.getElementById("number2").value);
// Check if the inputs are valid numbers
if (isNaN(num1) || isNaN(num2)) {
alert("Please enter valid numbers.");
return;
}
// Perform multiplication
const result = num1 * num2;
function divideNumbers() {
// Get the values from the textboxes
const num1 = parseFloat(document.getElementById("number1").value);
const num2 = parseFloat(document.getElementById("number2").value);
// Perform division
const result = num1 / num2;
<!DOCTYPE html>
<html>
<head>
<title>Highlight Links</title>
<style>
a{
color: initial;
}
</style>
</head>
<body>
<h2>Links</h2>
<a href="https://example.com" class="high">High Priority Link 1</a>
<a href="https://example.com">Normal Link 1</a>
<a href="https://example.com" class="high">High Priority Link 2</a>
<a href="https://example.com">Normal Link 2</a>
<script>
function highlightLinks() {
// Select all links with class 'high'
const highLinks = document.querySelectorAll('a.high');
<!DOCTYPE html>
<html>
<head>
<title>Update Paragraph</title>
</head>
<body>
<h2>Update Paragraph</h2>
<p id="paragraph">This is the initial content of the paragraph.</p>
<input type="text" id="textInput" placeholder="Enter new text">
<button onclick="updateParagraph()">Update</button>
<script>
function updateParagraph() {
const newText = document.getElementById('textInput').value;
document.getElementById('paragraph').innerHTML = newText;
}
</script>
</body>
</html>
5b. Develop a JavaScript program to check for an attribute (‘href’ in ) and get its
value/display on the Web page on a button click.
html
<!DOCTYPE html>
<html>
<head>
<title>Check href Attribute</title>
</head>
<body>
<h2>Check href Attribute</h2>
<a id="link" href="https://example.com">Example Link</a><br>
<button onclick="checkHrefAttribute()">Check href Attribute</button>
<div id="result"></div>
<script>
function checkHrefAttribute() {
const linkElement = document.getElementById('link');
6a. Develop a JavaScript program to sort a list of numbers (Use arrays and functions)
function sortNumbers(numbers) {
numbers.sort(function(a, b) {
return a - b;
});
return numbers;
6b. Develop a JavaScript program to create a hotel object using object literal syntax having
properties (name, location, room rate, discount etc), constructors and method (offer-price).
Create few objects to demonstrate the access of properties and an associated method
const hotel = {
name: "Grand Hotel",
location: "City Center",
roomRate: 200,
discount: 10,
offerPrice: function() {
const discountedPrice = this.roomRate - (this.roomRate * (this.discount / 100));
return discountedPrice;
}
};
// Accessing properties of the hotel object
console.log("Hotel Name:", hotel.name);
console.log("Location:", hotel.location);
console.log("Room Rate:", hotel.roomRate);
console.log("Discount:", hotel.discount + "%");