Question

In: Computer Science

2. The HTML document on the following page consists of JavaScript code executed when the document...

2. The HTML document on the following page consists of JavaScript code executed when the document is loaded. It prompts the user for his/her name (first and last names) then outputs the greeting “Hello Mr. _____!” or “Hello Ms. _____!” (where _____ is the user’s last name) depending on whether the first name is recognized as a male name. (In the code, only three male names are checked; a realistic program would check many more names.) We allow for the case where the user has entered his/her middle name(s) as well (despite being prompted only for first and last names). So, when the full name is converted to an array of strings, the important elements are the one at index 0 and the last element (at index 1 or greater). Function start, invoked when the document is loaded, does most of the work. The other function, maleName, returns true if the name passed to it is recognized as a male name.

Note that, in JavaScript, strings are compared by value not by reference (as in C or C++). Thus, if str1 and str2 are strings, then

str1 == str2

compares them for equality while

str1 != str2

compares them for inequality.

In the following listing, missing code is identified by letters. These letters are repeated on the next page; you there supply the missing code.

<html>

<head>

<title>Quiz 2</title>

<script>

var maleNames = ["Albert", "John", "Chris"];

function start()

{

    var name, nameArray,

        greeting = "Hello ";

    name = _a_____________________________________________

                          "John Doe" );

    nameArray = _b_______________;

    if ( maleName( nameArray[0] ) )

      greeting = _g_______________________;

    else

      greeting = _d_______________________;

    greeting =

      greeting.concat( _e______________________________ );

    greeting = greeting.concat( "!" );

    window.alert( greeting );

}

function maleName( name )

{

    var i = 0;

    while ( _h__________________________________________ )

      i ++;

    if ( i == maleNames.length )

      return false;

    else

      return true;

}

</script>

</head>

<body onload = "start()">

</body>

</html>

a Open a prompt dialog box that has the prompt

Enter your name (first and last)

and has the default value

John Doe

b Convert the string assigned to name to an array of its substrings delimited by spaces.

g Concatenate “Mr. “ to the current value of greeting (viz., “Hello “).

d Concatenate “Ms. “ to the current value of greeting (viz., “Hello “).

e The last element of array nameArray

h The condition that index i hasn’t gone beyond the end of the array maleNames and name is different from maleNames[i].

Solutions

Expert Solution

Solution:

<html>
<head>
        <title>Quiz 2</title>

        <script>
                var maleNames = ["Albert", "John", "Chris"];

    function start()
    {
        var name, nameArray,
        greeting = "Hello ";

        // 
        name = prompt("Enter your name (first and last): ", "John Doe");
        // b
        nameArray = name.split(" ");

        if ( maleName( nameArray[0] ) )
            // g
            greeting = greeting.concat("Mr. ");

        else
            //d
            greeting = greeting.concat("Ms. ");

        // e
        greeting = greeting.concat( nameArray[nameArray.length - 1] );
        greeting = greeting.concat( "!" );

        window.alert( greeting );
    }

    function maleName( name )
    {
        var i = 0;

        // h
        while ( i < maleNames.length && maleNames[i] != name )
          i ++;

        if ( i == maleNames.length )
          return false;

        else
          return true;
    }
        </script>

</head>

<body onload="start()">

</body>

</html>

Output:

case 1: Male name entered

Case 2: Female name entered:

Code Screenshot:


Related Solutions

The code to create a Search/Filter Data with Javascript or html from html page.
The code to create a Search/Filter Data with Javascript or html from html page.
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
1. An HTML document that’s generated by a web application is a ________________________ web page. 2....
1. An HTML document that’s generated by a web application is a ________________________ web page. 2. An easy way to log the progress of an application in Chrome’s Console panel is to insert __________________ methods at critical points in the code. 3. The childNodes property of a DOM object returns a/an ______________________ of the child nodes for that object. 4. The ___________________ method of an array can be used to concatenate the elements of the array into a single string.​...
Html code of Doctors info page
Html code of Doctors info page
Please provide HTML code for the following: - Create a page that lists a set of...
Please provide HTML code for the following: - Create a page that lists a set of audio files and shows their duration - Create a page that lists a set of video files and plays a different video when you click on the play icon
JAVASCRIPT HTML I'm looking to make a function that will take a postal code in a...
JAVASCRIPT HTML I'm looking to make a function that will take a postal code in a text box. The function jobs is to basically make sure the first letter of the postal code starts with these letters. ('A') ('N") ('W') ('F'). If the first letter of the postal code does not match up then an error to the user is sent.
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Make sure it works on jsfiddle and keep the code seperate html: css: javascript: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Assignment You should already have some experience with jQuery and some simple experience with JSON (from charting). In this assignment we will be creating some JSON data, parsing it, and displaying it. Step 1 – If you are not familiar with JSON you should complete the JSON tutorial at w3schools Step 2- You will now create a JSON file to represent some data of your...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
javaScript html receives an entry of a character string in a text box, when a button...
javaScript html receives an entry of a character string in a text box, when a button is clicked, the count of vowels in the string stored in the textbox is displayed. The html file contains one function: vowelcount(). vowelcount(): returns the number of uppercase and lowercase English language vowel letter that occurs in the string entry in the textbox. //html: <!--    YOUR ID    YOUR NAME --> <html> <head> <script> function vowelcount() {        /* YOUR CODE HERE...
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT