I need to do a name game for class. I'm clueless where to start. I'm so new at Javascript, I just don't know what to do. Can you help? I want to know how to do this and understand why.

You will create JavaScript code that will allow a user to input his/her name and it will convert the name to Shirley Ellis's The Name Game. For example: The user inputs the name John, clicks a button, and what is generated is:

Robert Robert Bo Bobert
Banana Fanna Fo Fobert
Fe Fi Mo Mobert
Robert!

Requirements:

Add your name to the file, as demonstrated in the Commenting Your Code page in this module.
The code will need to replace the first letter of the input name. First it will replace the first letter with a B, then with an F, then with an M.
If the name input is a vowel (a-e-i-o-u), then the B, F and M need to be placed in front of the name. For example, if the name input is Ashley, then the output would be:
Ashley Ashley Bo Bashley
Banana Fanna Fo Fashley
Fe Fi Mo Mashley
Ashley!
You will use the following string methods:
indexOf
Substring
CharAt
toUpperCase
toLowerCase
Comment each method and function created, as demonstrated in the Commenting Your Code page in this module.
Submit your HTML file with embedded JavaScript (name the file Extra_yourLastName.html) to the Extra Credit link under Assignments.

Recommended Answers

All 5 Replies

I figured out how to do uppercase Javascript, but I don't know how to make it work.

Well, you're obviously supposed to learn something from this assignment, so I'm not going to write all the code for you ... but I will give you a place to start.

var inputName = "Tim";

var nameVar1 = "";

var firstLetterOfName = inputName.substr(0,1); //find FIRST LETTER OF NAME using Substring

firstLetterOfName = firstLetterOfName.toLowerCase();  //convert it to lower case

if ( firstLetterOfName =="a" || firstLetterOfName =="e" || firstLetterOfName =="i" || firstLetterOfName == "o"){ 

//if the first letter is a vowel, tack a B on the front
nameVar1 = "B"+inputName.toLowerCase();
}else{
//or else get rid of the first letter and replace it with a B

nameVar1 = "B" + inputName.substr(1,inputName.length-1)
}

var line1 = inputName + " " + inputName + " Bo " + nameVar1;

alert(line1);
Member Avatar for jmichae3

http://www.ecma-international.org/publications/standards/Ecma-262.htm

here is the JS standard specification document. in it you will find all the functions and everything. to do browser DHTML with jquery, you will need to probably get Danny Goodman's Javascript bible 7th ed. I like 2nd ed. much better though.

by the way,you can simply do this instead of using 2 lines to do the same thing:

var firstLetterOfName = inputName.substr(0,1).toLowerCase(); //find FIRST LETTER OF NAME using Substring

that is valid js code

Member Avatar for jmichae3

to get your input, I suggest you use a form for input.

<form name="f" action="thispage.html" method="get">
<input type="text" name="myname" maxlength="100">
<input type="button" onclick="doit()" value="Generate me some fun!">
<div id="out"></div>
</form>
<script type="text/javascript">
function doit() {
    var inputname=document.f.myname.value;
    //... accumulate output within line1 using line1 +=
    document.getElementById('out').innerHTML=line1; // do output
}
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.