Hello, I am definitely a beginner. I need some help with this application. I need to be able to have an alert box pop up and as the user 'Enter degrees in Fahrenheit\n Or enter 999 to end entries'. I am not sure what I am doing wrong.

Thank you in advance for anyones help on this. :)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Convert F degrees to C degrees</title>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>    
    <script>

        var entry;
        function convertToCelsius(fahrenheit) {
        return (fahrenheit - 32) / 1.8;
}

        do {
            entry = prompt('Enter degrees in Fahrenheit\n Or enter 999 to end entries',
                           999);
            entry = parseInt(entry);

        if (entry != 999) {
            alert('Fahrenheit = ' + entry + ' degrees' +
            '\nCelcius = ' + convertToCelsius(entry) + ' degrees');
    }
}
while (entry != 999);
    </script>
</head>
<body>
<section>
    <h1>This page is displayed after the JavaScript is executed</h1>
</section>
</body>
</html>

I'm not sure what's the problem ? I don't get any problem. Is it not working ?

Member Avatar for diafol

Can't see what's wrong either. You don't state the problem!

BTW, this is a pretty clunky was to do it. A html form would be better - less intrusive. Popups and alerts are frustrating for a lot of people.

Member Avatar for diafol

Here's an 'old' script of mine - I've just converted it from jquery, so it's probably not as clean or optimized as it could be:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Convert Temperature</title>
</head>
<body>
<form name="myform">
    <label for="temp">Temperature</label>
    <input type="text" id="temp" name="temp" value="" />
    &deg;<span id="span1">F</span>
    <input type="text" id="temp2" name="temp2" value=""  />
    &deg;<span id="span2">C</span><br />
    <br />
    <label for="FC"><input type="radio" name="conv" value="FC" id="FC" checked /> &deg;F &rarr; &deg;C</label>
    <label for="CF"><input type="radio" name="conv" value="CF" id="CF" /> &deg;C &rarr; &deg;F</label>
    <button id="convertBtn">Convert</button>
</form>
<script>
    document.getElementById("convertBtn").onclick=function()
    {
        convertTemp();
        return false;
    };

    var rads = document.getElementsByName('conv');
    for(i=0, len=rads.length;i<len;i++)
    {
        rads[i].onclick=function()
        {
            swapTempLabel(this.value);
        };
    };

    function swapTempLabel(radioValue)
    {
        if(radioValue == 'FC')
        {
            document.getElementById("span1").innerHTML = 'F';
            document.getElementById("span2").innerHTML = 'C';   
        }else{
            document.getElementById("span1").innerHTML = 'C';
            document.getElementById("span2").innerHTML = 'F';   
        }   
    }

    function convertTemp()
    {
        var t1 = parseFloat(document.getElementById("temp").value); 
        if(isNaN(t1))
        {
            document.getElementById("temp").value = '0';
            t1 = 0; 
        }
        var t2 = document.getElementById("temp2");
        if(document.getElementById('FC').checked)
        {
            t2.value = parseFloat((t1 - 32) / 1.8).toFixed(2);
        }else{
            t2.value = parseFloat((t1 * 1.8) +32).toFixed(2);   
        }
        document.getElementById("temp").focus();
        document.getElementById("temp").select();
    }
</script>
</body>
</html>

When reading threads like this, I can understand the code, that is one step forward, maybe two :)

Actually, the code works but I think I had in my mind that I wanted to be able to add validation to the application to make sure that entry is a valid number and not a letter. Any suggestions?

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.