HOME / BLOG / BLOG-POST

How to create a random color generator using javascript?

 

<script>
    // paste this code to see magic
    var propmt = window.prompt("Enter any number only: ");
var test = isNaN(propmt);
if (propmt == ""){
    document.write("<h2 class='alert alert-danger'>Please enter a valid value!</h2>");
    }
    else{
        if (propmt == null){
            document.write("<h2 class='alert alert-danger'>&times; You have cancelled the process!</h2>");
            } else{
            for(i=1; i<=propmt; i++){
                document.write("<div id='"+i+"'>" + i + "</div>");
                var randomColor = Math.floor(Math.random()*16777215).toString(16); //main function
                var target = document.getElementById(i);
                target.style.background ="#"+randomColor;						
                }
            }
            
        }	
</script>

 


about authors

Sandip Das

Web Developer

Once a legend said “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler.



POST COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *