please use javascript so answear the question.
Create a function called tell_user() that pops up an alertbox with some inspirational message. Call the function to make sure it works. Create a function called square_it() that takes one argument: an integer. The function will compute the square of the number and return this as its return value and then alert() the results to the user. parselnt (prompt(“Please enter an integer to square: “, “”)): Create a function that takes a single argument: a phone number. The function should return true if the phone number has either seven digits or ten digits, false otherwise. Create a function that takes a sentence. The function should return true if the string contains the substring “sample”, false otherwise.
Expert Answer
Problem 1:
<html>
<head>
<!– Scripting section –>
<script type=”text/javascript”>
//Function definition
function tell_user()
{
//Displaying a message box to user
alert(“An investment in knowledge always pays the best interest.”);
}
</script>
</head>
<!– Calling function at onload event of body –>
<body onload=”tell_user()”>
</body>
</html>
Sample Run:
______________________________________________________________________________________________
Problem 2:
<html>
<head>
<!– Scripting section –>
<script type=”text/javascript”>
//Function definition
function square_it(num)
{
//Computing square of a number
var result = num * num;
//Displaying a message box to user
alert(“Square(“+num+”) = ” + result);
}
//Function that process the square_it function
function processSquare()
{
//Reading input from user
var num = parseInt(prompt(“Please enter an integer to square: “, “”));
//Calling square_it function
square_it(num);
}
</script>
</head>
<!– Calling function at onload event of body –>
<body onload=”processSquare()”>
</body>
</html>
Sample Run:
__________________________________________________________________________________________________________________
Problem 3:
<html>
<head>
<!– Scripting section –>
<script type=”text/javascript”>
//Function definition
function chkPhoneNumber(phoneNumber)
{
//For counting number of digits
var cnt = 0;
//Counting digits
while( phoneNumber >= 1 )
{
//Extracting digit
phoneNumber = phoneNumber / 10;
//Accumulating count
cnt++;
}
//Checking number of digits
if(cnt == 7 || cnt == 10)
{
return true;
}
else
{
return false;
}
}
//Function that process phone number
function processPhoneNumber()
{
//Reading phone number from user
var phNum = parseInt(prompt(“Enter phone number: “, “”));
//Calling function by passing phone number
if(chkPhoneNumber(16889555))
{
alert(” Phone number has seven digits… “);
}
else if(chkPhoneNumber(5589589771))
{
alert(” Phone number has ten digits… “);
}
else
{
alert(” Not of valid length!!! “);
}
}
</script>
</head>
<!– Calling function at onload event of body –>
<body onload=”processPhoneNumber()”>
</body>
</html>
Sample Run:
______________________________________________________________________________________________
Problem 4:
<html>
<head>
<!– Scripting section –>
<script type=”text/javascript”>
//Function definition
function chkSentence(sentence)
{
//Finding index of sub-string sample in sentence
if(sentence.indexOf(“sample”) != -1)
{
return true;
}
else
{
return false;
}
}
//Calling function
alert(chkSentence(‘This is just a sample program’));
</script>
</head>
<body>
</body>
</html>
Sample Run: