0% found this document useful (0 votes)
23 views2 pages

JavaScript Basics for 10th Graders

The document provides JavaScript examples for 10th class students, featuring five interactive scripts. These examples include checking if a number is positive or negative, determining if a number is even or odd, assessing voting eligibility based on age, finding the greater of two numbers, and evaluating pass or fail status based on marks. Each example is presented in HTML format with embedded JavaScript for user interaction.

Uploaded by

Fariha Marjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

JavaScript Basics for 10th Graders

The document provides JavaScript examples for 10th class students, featuring five interactive scripts. These examples include checking if a number is positive or negative, determining if a number is even or odd, assessing voting eligibility based on age, finding the greater of two numbers, and evaluating pass or fail status based on marks. Each example is presented in HTML format with embedded JavaScript for user interaction.

Uploaded by

Fariha Marjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Examples for 10th Class Students

Example 1: Check Positive or Negative


<!DOCTYPE html>
<html>
<head>
<title>Positive or Negative</title>
</head>
<body>
<h2>Check Positive or Negative</h2>
<script>
let num = prompt("Enter a number:");
num = Number(num);

if (num > 0) {
alert(num + " is Positive.");
} else if (num < 0) {
alert(num + " is Negative.");
} else {
alert("The number is Zero.");
}
</script>
</body>
</html>

Example 2: Check Even or Odd


<!DOCTYPE html>
<html>
<head>
<title>Even or Odd</title>
</head>
<body>
<h2>Check Even or Odd</h2>
<script>
let num = prompt("Enter a number:");
num = Number(num);

if (num % 2 === 0) {
alert(num + " is Even.");
} else {
alert(num + " is Odd.");
}
</script>
</body>
</html>

Example 3: Check Eligibility to Vote


<!DOCTYPE html>
<html>
<head>
<title>Voting Eligibility</title>
</head>
<body>
<h2>Check Voting Eligibility</h2>
<script>
let age = prompt("Enter your age:");
age = Number(age);

if (age >= 18) {


alert("You are eligible to vote.");
} else {
alert("You are not eligible to vote.");
}
</script>
</body>
</html>

Example 4: Check Greater Number


<!DOCTYPE html>
<html>
<head>
<title>Greater Number</title>
</head>
<body>
<h2>Find the Greater Number</h2>
<script>
let a = prompt("Enter first number:");
let b = prompt("Enter second number:");
a = Number(a);
b = Number(b);

if (a > b) {
alert(a + " is greater than " + b);
} else if (b > a) {
alert(b + " is greater than " + a);
} else {
alert("Both numbers are equal.");
}
</script>
</body>
</html>

Example 5: Check Pass or Fail


<!DOCTYPE html>
<html>
<head>
<title>Pass or Fail</title>
</head>
<body>
<h2>Check Pass or Fail</h2>
<script>
let marks = prompt("Enter your marks:");
marks = Number(marks);

if (marks >= 33) {


alert("You passed the exam.");
} else {
alert("You failed the exam.");
}
</script>
</body>
</html>

Common questions

Powered by AI

The process involves prompting the user to enter two numbers, which are converted to Number types. Using if-else conditions, the program compares the numbers: if one is greater, it displays which is greater; if they are equal, it indicates they are the same .

JavaScript handles the edge case of zero by specifically checking it in an else clause after determining the number is not positive or negative. The condition `else` covers all other cases that are neither greater nor less than zero, thus categorizing 0 as neither positive nor negative .

Conditional logic assumes user data aligns with expected formats and ranges, like assuming age is a positive number. Incorrect assumptions can lead to flaws in logic processing, as the code must handle all potential input variations, such as non-numeric characters leading to unintended results .

JavaScript determines voting eligibility by checking if a person's age is 18 or older. The program prompts the user to enter their age, which is then converted to a Number. If the age is greater than or equal to 18, an alert indicates eligibility; otherwise, it indicates ineligibility .

A JavaScript program uses a conditional statement to determine if a number is positive, negative, or zero. It prompts the user to enter a number, converts the input to a numeric type, and then uses if-else statements: if the number is greater than zero, it is positive; if less than zero, it is negative; otherwise, it is zero .

The JavaScript program evaluates passing or failing based on whether the student's marks are 33 or higher. It prompts the user for marks, converts them to a Number, and uses an if-else statement to display pass for marks >= 33, else fail .

Using JavaScript alerts is effective for straightforward user feedback because it is simple to implement and provides immediate, unavoidable notifications. However, for complex interactions in web applications, alerts can be disruptive and lack sophisticated styles .

To test if a number is even or odd in JavaScript, the program first prompts for a number input and converts it to a Number type. It then uses the modulus operator (%) to check divisibility by 2. If the remainder is 0, the number is even; otherwise, it is odd .

JavaScript programs use the `prompt()` function to dynamically gather user input and `Number()` to convert strings to numbers for conditional logic tasks, like checking even/odd numbers or comparing magnitudes .

Type conversion ensures accurate comparisons in JavaScript since user input from `prompt()` is initially a string. Converting to Number prevents unexpected behavior when comparing or performing arithmetic operations, as neglecting this can lead to logical errors in conditional statements .

You might also like