Hello I am trying to learn the differences from Java and JavaScript so I am trying to take a working simple program in Java and make it work in JavaScript. I keep getting undefined. Here is the JavaScript I am trying:

<script type="text/javascript">

function _(x) {
    return document.getElementById(x);
    }

    function phraseOmatic () {

    //print to div id=phrase
    _("phrase").innerHTML =  phrase;

    //make three sets of words to choose from        
    var wordListOne = ["24/7","multi-Tier","30,000 foot","B-to-B","win-win","frontend","web-based","pervasive","smart","sixsigma","critical-path","dynamic"];

    var wordListTwo = ["empowered","sticky","value-added","oriented","centic","distibuted","clustered","branded","outside-the-box","positioned","networked","focused","leveraged","aligned","tageted","shared","cooperative","accelerated"];

    var wordListThree = ["process","tippingpoint","solution","architecture","core competency","strategy","mindshare","portal","space","vision","paradigm","mission"];

    //find out how many words are in each list
    var oneLength = wordListOne.length;
    var twoLength = wordListTwo.length;
    var threeLength = wordListThree.length;

    //generate three random numbers
    var rand1 = Math.floor(Math.random() * oneLength);
    var rand2 = Math.floor(Math.random() * twoLength);
    var rand3 =Math.floor(Math.random() * threeLength);

    //now build a phrase
    var phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];

    }

    window.addEventListener("load", phraseOmatic, false);

</script>

Recommended Answers

At a glance, it looks like line 10 should read:

document.getElementById("phrase").innerHTML = phrase;

I'd need to see the HTML to be sure.

Jump to Post

Shouldn't line 10 be after line 30? You are assigning the value of the var before you create it.

var phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
_("phrase").innerHTML =  phrase;

For that matter, you don't need the go-between var phrase …

Jump to Post

All 5 Replies

At a glance, it looks like line 10 should read:

document.getElementById("phrase").innerHTML = phrase;

I'd need to see the HTML to be sure.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function _(x) {
    return document.getElementById(x);
    }
    function phraseOmatic () {
    //print to div id=phrase
    _("phrase").innerHTML =  phrase + " but Why is it undefined? " + rand1 + " rand1 is undefined as well. I think my problem is in the Math.random being a floating point and I need and integer for the position of the array ";
    //make three sets of words to choose from        
    var wordListOne = ["24/7","multi-Tier","30,000 foot","B-to-B","win-win","frontend","web-based","pervasive","smart","sixsigma","critical-path","dynamic"];
    var wordListTwo = ["empowered","sticky","value-added","oriented","centic","distibuted","clustered","branded","outside-the-box","positioned","networked","focused","leveraged","aligned","tageted","shared","cooperative","accelerated"];
    var wordListThree = ["process","tippingpoint","solution","architecture","core competency","strategy","mindshare","portal","space","vision","paradigm","mission"];
    //find out how many words are in each list
    var oneLength = wordListOne.length;
    var twoLength = wordListTwo.length;
    var threeLength = wordListThree.length;
    //generate three random numbers
    var rand1 = Math.floor(Math.random() * oneLength);
    var rand2 = Math.floor(Math.random() * twoLength);
    var rand3 =Math.floor(Math.random() * threeLength);
    //now build a phrase
    var phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
    }
    window.addEventListener("load", phraseOmatic, false);
</script>
<title>phraseOmatic</title>
</head>

<body>
<div id="phrase"></div
The content of the document......
</body>

</html>

I use the function on line 5 through 7 to serve as my documentgetElementById so I don't have to type it in every time. I just put _() and pass it whatever.

Shouldn't line 10 be after line 30? You are assigning the value of the var before you create it.

var phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
_("phrase").innerHTML =  phrase;

For that matter, you don't need the go-between var phrase anyway.

Thanks LesF I moved line 10 down to the last item in my script block and it works. And, yeah I guess I could slim it down.

Be a part of the DaniWeb community

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