Hi!
Login or Register new account

JavaScript Can Validate Input

Try to guess what number is randomized! <1:100> :


and here is the code:

var rand = Math.floor((Math.random() * 100) + 1);

function Game() {
    var x, text;

    x = document.getElementById("numb").value; //x is now number you putted in the bracket

    if (isNaN(x) || x < 1 || x > 100) {
        text = "Input not valid, try again!";
    } else if (x < rand) {
        text = "You should try bigger number!";
    } else if (x > rand) {
        text = "You should try smaller number!";
    } else {
        text = "THATS IT! YOU WON!";

    }
    document.getElementById("demo").innerHTML = text;
}
        
		

Changing styles!

JavaScript can change the style of an HTML element.

CODE:

                                function myFunction1() {
                                var x = document.getElementById("demo2");
                                x.style.fontSize = "15px";
                                x.style.color = "red";
                                }
                                
                                
		

Other buttons are the copy of the original, and they are calling different functions with different styles, try it yourself!