create event driven from javascript program for the following make use of appropriate variables javascript in built string functions and control structures
Here are some event-driven JavaScript programs that use variables, built-in functions, and control structures:
Accept a number and validate if it is a number
Accept an integer and display the result by multiplying it by 3
Accept two integers and display the larger number
Accept a string and calculate its length
Accept a string and display it in lowercase and uppercase
Check if the length of a string is 4 or greater
Accept a string and count and display the number of vowels in it
Event-driven programming is a programming method where the program's flow is determined by events. This means that the program waits for an event to occur before continuing execution. Event-driven programming is used in graphical user interfaces and network servers.
Sure! Here's an example of an event-driven JavaScript program that utilizes variables, string functions, and control structures:
```javascript
// Prompt the user for their name
let name = prompt("Please enter your name:");
// Check if the name is empty or null
if (name !== null && name !== "") {
// Capitalize the first letter of the name
let capitalized = name.charAt(0).toUpperCase() + name.slice(1);
// Generate a random number between 1 and 10
let randomNumber = Math.floor(Math.random() * 10) + 1;
// Create an event string based on the random number
let event = "";
if (randomNumber <= 5) {
event = `${capitalized} is invited to a birthday party!`;
} else {
event = `${capitalized} is going to a concert!`;
}
// Display the event to the user
alert(event);
} else {
// Display an error message if the name is empty or null
alert("Invalid name. Please try again.");
}
```
In this example, the program prompts the user for their name using the `prompt()` function. It then checks if the name is not empty or null using an `if` statement. If the name is valid, it capitalizes the first letter of the name using the `charAt()` and `toUpperCase()` functions.
Next, it generates a random number between 1 and 10 using the `Math.random()` and `Math.floor()` functions. Based on the random number, it creates an event string using string interpolation (using backticks and `${}`) and appropriate string functions (`slice()` and concatenation).
Finally, it displays the event to the user using the `alert()` function. If the name is empty or null, it displays an error message.
This program demonstrates the use of variables, string functions (such as `charAt()`, `toUpperCase()`, and `slice()`), and control structures (`if` statement) to create an event-driven JavaScript program.
Answers & Comments
Answer:
Here are some event-driven JavaScript programs that use variables, built-in functions, and control structures:
Event-driven programming is a programming method where the program's flow is determined by events. This means that the program waits for an event to occur before continuing execution. Event-driven programming is used in graphical user interfaces and network servers.
Answer:
Sure! Here's an example of an event-driven JavaScript program that utilizes variables, string functions, and control structures:
```javascript
// Prompt the user for their name
let name = prompt("Please enter your name:");
// Check if the name is empty or null
if (name !== null && name !== "") {
// Capitalize the first letter of the name
let capitalized = name.charAt(0).toUpperCase() + name.slice(1);
// Generate a random number between 1 and 10
let randomNumber = Math.floor(Math.random() * 10) + 1;
// Create an event string based on the random number
let event = "";
if (randomNumber <= 5) {
event = `${capitalized} is invited to a birthday party!`;
} else {
event = `${capitalized} is going to a concert!`;
}
// Display the event to the user
alert(event);
} else {
// Display an error message if the name is empty or null
alert("Invalid name. Please try again.");
}
```
In this example, the program prompts the user for their name using the `prompt()` function. It then checks if the name is not empty or null using an `if` statement. If the name is valid, it capitalizes the first letter of the name using the `charAt()` and `toUpperCase()` functions.
Next, it generates a random number between 1 and 10 using the `Math.random()` and `Math.floor()` functions. Based on the random number, it creates an event string using string interpolation (using backticks and `${}`) and appropriate string functions (`slice()` and concatenation).
Finally, it displays the event to the user using the `alert()` function. If the name is empty or null, it displays an error message.
This program demonstrates the use of variables, string functions (such as `charAt()`, `toUpperCase()`, and `slice()`), and control structures (`if` statement) to create an event-driven JavaScript program.
[tex]thank \: u[/tex]