/    Sign up×
Community /Pin to ProfileBookmark

Javascript beginner, help on CS lab

I have a CS lab due tomorrow and since I suck at javascript, it will be passed in incompleted. If there is an individual out there with time on their hands to give this lab a crack at, it would be appreciated. I just need the javascript code so I can copy and paste it into notepad to produce the result.

The output page should look something like this:

ANNOUNCING:

S

Sc

Sco

Scoo

Scoob

Scooby

Scooby!

Here are the lab instructions:

  • 1. Write a program that prompts (uses the prompt function you saw in assignment 6) the
    user for his/her name and announces that person. You will need to
    use the length and substr( ) methods, as well as a for loop. Use length to count the number
    of letters in the name, use the for loop to loop that number of times, and in each pass
    through the loop, use the substr( ) method to produce the user’s name and then portions
    thereof. You may use first name only or both first and last names.

  • 2. Modify the program created above to print every second line of the given name a
    different colour. Make sure to use a light and a dark colour. Hint: use an if statement along
    with the % (mod) operator and work with the iteration variable.

  • 3. Finally, modify your program in the following ways:
    a. Place your program code for printing the name in a function called:
    announceUser(userName)
    b. Add an input box and a button
    c. Call your function from the button’s event handler using the text in the
    input box.
    Hint:

  • 1. You will need to use the <FONT> tag creatively for step 2.
  • to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @CrazyMerlinNov 21.2006 — for part 1:

    in a page header
    <i>
    </i>&lt;script type="text/javascript"&gt;

    <i> </i>var name = '';

    <i> </i>function ask()
    <i> </i>{
    <i> </i> name = prompt("Hey, what's your name?", "Enter name");
    <i> </i> announce(name);
    <i> </i>}

    <i> </i>function announce(name)
    <i> </i>{
    <i> </i> var len = name.length;
    <i> </i> for(i = 0; i &lt;= len; i ++)
    <i> </i> {
    <i> </i> document.write(name.substr(0, i) + "&lt;br&gt;");
    <i> </i> }
    <i> </i>}
    &lt;/script&gt;


    in the page body tag add onload="ask()" : <body onload="ask()">


    for part 2 change the for loop to this:
    <i>
    </i>for(i = 0; i &lt;= len; i ++)
    {
    if(i % 2 == 0)
    {
    document.write("&lt;font color='#ff0000'&gt;" + name.substr(0, i) + "&lt;/font&gt;&lt;br&gt;");
    }
    else
    {
    document.write(name.substr(0, i) + "&lt;br&gt;");
    }
    }


    for part 3:
    <i>
    </i>&lt;html&gt;

    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt;
    &lt;title&gt;New Page 1&lt;/title&gt;
    &lt;meta name="GENERATOR" content="Microsoft FrontPage 6.0"&gt;
    &lt;meta name="ProgId" content="FrontPage.Editor.Document"&gt;
    &lt;script type="text/javascript"&gt;

    <i> </i>function announceUser()
    <i> </i>{
    <i> </i> var name = document.getElementById('name').value;
    <i> </i> var len = name.length;
    <i> </i> for(i = 0; i &lt;= len; i ++)
    <i> </i> {
    <i> </i> if(i % 2 == 0)
    <i> </i> {
    <i> </i> document.write("&lt;font color='#ff0000'&gt;" + name.substr(0, i) + "&lt;/font&gt;&lt;br&gt;");
    <i> </i> }
    <i> </i> else
    <i> </i> {
    <i> </i> document.write(name.substr(0, i) + "&lt;br&gt;");
    <i> </i> }
    <i> </i> }
    <i> </i>}
    &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
    &lt;form&gt;
    Name : &lt;input type="text" size="30" id="name"&gt;&lt;input type="button" value="Announce!" onclick="announceUser()"&gt;
    &lt;/form&gt;
    &lt;/body&gt;

    &lt;/html&gt;




    Oh what fun to be a teen at school again!

    //erlin!
    Copy linkTweet thisAlerts:
    @inneedofhelpauthorNov 22.2006 — Wow, that was fast! Thanks a ton!
    ×

    Success!

    Help @inneedofhelp spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...