Javascript Common Problems & Their Solutions


Google
As I am learning JavaScript from Microsoft E-Book Programming in HTML5 with JavaScript and CSS3 Book Link .

  • 1st Problem, You want to show “Hello [username]” ([username] is the name of user) If the user is logged in and “Hello World” if he/she isn’t. To solve this problem you can

var myLabel = document.GetElementById("label");
myLabel.InnerHTML("Hello " + (username || "World"));

  • 2nd Problem, You want to convert a string number (‘123’) into an integer number. This can be solved using the built-in Function Number()

 

var myStringNumber = "123";
var myNumber = Number(myStringNumber);

3rd Problem, You want to confirm that the user entered data is number or anything else. You can solve this by using JavaScript Built-in Function isNaN(). NaN stands for Not a Number.


var userEnteredNumber = prompt("Please enter your age?","");

if(isNaN(userEnteredNumber)){
alert("Please Enter a valid number");
return;
}
else{
// Things to do when user entered his age in correct format(Type)
}

I will be adding more tips and tricks as I learn new. So, bookmark this

Advertisements

One thought on “Javascript Common Problems & Their Solutions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s