CSCI 4230
Software Tools
Fall 1999
Suugested Solution to Homework #6

(1)    For example,

<html>
<!--
       perfectnumber.html
       written by Kwok-Bun Yue      October 7, 1999
       See whether a number is perfect or not.
-->

<head>
<title>Perfect number</title>
<script language=Javascript>
<!--
function isPerfectNumber(N) {
    var sum = 1;
    var i;
    for (i=2; i<=N /2; i++) {
        if (N % i == 0) sum += i;
    }
    return (N == sum);
}

function showResult(theForm) {
   theForm.result.value = isPerfectNumber(theForm.inputNum.value);
}
// -->
</script>
</head>

<body bgcolor=#88ccff>
<h2>Perfect Number Test</h2>
<p>
<form name="thisForm">
Input a number:
<input type=text name=inputNum size=10 onChange="showResult(this.form)">
<br>
Is the number perfect?
<input type=text name=result size=10>
</form>
</body>
</html>

(2)    For example,

<body onload="showQuote();">
Random Quote:
<p>
<div style="position:absolute" id="quote"></div>
<script>

//Change the parameters below.
var fontsize=22;
var refreshInterval=5000;

function nextQuote()
{   nextQuote.count++;
    if (nextQuote.count >= nextQuote.Quotes.length)
    {   nextQuote.count = 0;
    }
    return nextQuote.Quotes[nextQuote.count];
}

nextQuote.Quotes = new Array("I am hungry!!",
                            "Pass the burgers!!",
                            "CGI is fun.",
                            "Give me Perl or give me Java.",
                            "CGI.pm is good for your health.",
                            "No Active-X here.",
                            "Do not drink coffee today.");

nextQuote.count = nextQuote.Quotes.length-1;

function nextColor()
{   nextColor.count++;
    if (nextColor.count >= nextColor.colors.length)
    {   nextColor.count = 0;
    }
    return nextColor.colors[Math.floor(Math.random() * nextColor.colors.length)];
}

nextColor.colors = new Array("red", "blue", "green", "yellow", "white", "brown", "navy");

function showQuote(){
    var quoteHtmlCode = '<div style="font-family:Arial;background-color:'+nextColor()+'; font-size:'+fontsize+'px">'+nextQuote()+'</div>';
    if (document.layers){
        document.quote.document.write(quoteHtmlCode);
        document.quote.document.close();
    }
    else if (document.all)
       quote.innerHTML=quoteHtmlCode;
    setTimeout("showQuote()",refreshInterval);
}
</script>
</body>