Computer Science, asked by dcdcsdn, 1 year ago

Write a javascript code to input your name and age. If name is Divya and age is 16, the display the statement "Divya is 16 years old."

Answers

Answered by AbhijithPrakash
30

<HTML>

<HEAD>

<TITLE>JavaScript</TITLE>

</HEAD>

<BODY>

<SCRIPT Language="Javascript" TYPE="text/Javascript">

var name

var age

name=prompt("Enter your Name :","0")

age=prompt("Enter your Age :","0")

document.write(name+ " is " +age+ " years old")

</SCRIPT>

</BODY>

</HTML



you can check this ..


dcdcsdn: helpful..but where is the use of if statement??
AbhijithPrakash: ??
Answered by Afreenakbar
0

Answer and Explanation:

Here's an example JavaScript code that takes input of a name and age, and displays a statement only if the name is "Divya" and the age is 16:

javascript code:-

// prompt the user to enter their name and age

const name = prompt("Enter your name:");

const age = parseInt(prompt("Enter your age:"));

// check if the name is "Divya" and age is 16

if (name === "Divya" && age === 16) {

console.log(`${name} is ${age} years old.`);

}

Explanation:

  1. We first use the prompt() function to get input from the user for their name and age. We store the input values in name and age variables, respectively.
  2. We then use an if statement to check if the name variable is equal to the string "Divya" AND the age variable is equal to the number 16. If the condition is true, we use console.log() to display the statement "Divya is 16 years old." using string interpolation with template literals. If the condition is false, nothing is displayed.
  3. The parseInt() function is used to convert the age input from a string to a number for comparison. If the user enters a non-numeric value for age, parseInt() will return NaN, which will cause the if statement condition to evaluate to false.

Similar Questions:

https://brainly.in/question/10249452

https://brainly.in/question/15516113

#SPJ3

Similar questions