9/08/2018

Data Types in JavaScript with Examples

Primitive Data Types in JavaScript with Examples and full code in JS. When we talk about any language like C, C++, and Java, the most basic and most important characteristics of that is the set of data types of that language. Javascript support maximum 6 types of primitive data types which are Boolean, Null, Undefined, Number, String, and Symbol (new in ECMAScript 6), But we are focusing only 3 types of mainly using data types in javascript. JavaScript mainly focus on three primitive data types are–

  1. Numbers
  2. Strings
  3. Boolean

So What we are going to learn in this article, Here is a list of a content that we are going to learn in this Javascript datatypes article.

Index of Content


  • Picture of an Article
  • The concept of Data Types in JS
    1. Dynamic
    2. when number values added to a string
  • Data types in Javascript
    1. Numbers
    2. Strings
    3. Boolean
    4. Null
    5. Undefined
  • Examples of Data Types in Javascript
  • Javascript Array
  • Javascript objects
  • The typeof Operator
  • About Author

Javascript Data types with list of datatypes used in JS with Logo

Here, JavaScript also supports composite data type, is an object. An object is a collection of values like numbers, string, objects. JavaScript support dual nature of objects:

  1. JavaScript Object can represent an unordered collection of named values.
  2. And ordered a collection of numbered values. Like values are indexed with numeric values.

JavaScript Concept of Data Types:


Before working with data types one should know the behavior of Data Types. We assume that you already know the How statement Execute in JS.

i) Data types in JavaScript are Dynamic:

Data Types in JavaScript are dynamic means we define a variable once and that can hold different data type values. Like if we have a variable x then it can contain a numeric value and also a string value.

Example:

<!DOCTYPE html>
<html>
<body>
<p>Dynamic data types</p>
<p id="DynamicData"></p>


<script>
var x;             
x = 5;             
x = "Programming With Basics"; 
document.getElementById("DynamicData").innerHTML = x;
</script>
</body>
</html>

Here we define x and provide an integer value and again the same variable is provided string value. Now the value of x is string value then the result is “Programming With Basics”.

ii) when number values added to a string:

When a numeric value added to the string. JavaScript has a facility to handle this situation. Automatically it assumes int value is a string and append it with a string value.

Example:

<!DOCTYPE html>
<html>
<body>
<p>adding number and string</p>
<p id="DataTypeDemo"></p>
<script>
var AddNumAndString = 26 + "Programming with basics";
document.getElementById("DataTypeDemo").innerHTML = AddNumAndString;
</script>

</body>
</html>Now we see it didn’t gives error. Result will be 26Programming with basics.

Let’s define Data Types in Brief:

1. Strings Datatypes in JS:


The string is a series or collection of Characters.

Ex: “Programming with basics”

Also, the string can be defined in single quotes. We can use a string with conditions statements for example If, If-Else, and Switch statements.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Strings in JavaScript</h2>
<p id="StringsInJS"></p>
<script>
var OurSite = "Programming With Basics";
var OurTutorial = 'DataTypes';
document.getElementById("StringsInJS").innerHTML =OurSite + ":" + OurTutorial;
</script>
</body>
</html>

2. Numbers in Javascript:


The numeric data is all digit values including decimal values, like 29, 105.98 etc. In JavaScript there are no integer or floating point values, JavaScript treats all numbers as floating point values.

Example:

<!DOCTYPE html>
<html>
<body>
<h>Numbers</h>
<p id="Numbers"></p>
<script>
var a = 10;
var b = 11;
var c = 12;
document.getElementById("Numbers").innerHTML =a + "<br>" + b + "<br>" + c;
</script>
</body>
</html>

3. JavaScript Booleans:


Boolean data types have two values only, either true or false.

Ex:  var x=true;
        var x=false;

Example:

<!DOCTYPE html>
<html>
<body>
<h>Boolean Demo</h>
<p id="BooleanDemo"></p>
<script>
var a = 10;
var b = 10;
var c = 15;
var x;
if(a==b||b==c)
{
x=true;
}
else
{
x=false;
}
document.getElementById("BooleanDemo").innerHTML =x;
</script>
</body>
</html

JavaScript Arrays:

JavaScript arrays are defined within square brackets, and the items within brackets are separated by a comma. As we defined earlier array based on numeric values, its index starts from 0.

Ex: We defined an array of all we have in our blog:
Var OurSite = [“C”, ”C++”, ”Java”, ”JS”, ”Hackerrank”];
Here C is at 0 indexes, C++ at index 1 and so on.

Example:

<!DOCTYPE html>
<html>
<body>
<h>Arrays in JavaScript</h>
<p id="Array"></p>
<script>
var oursite = ["C", "C++", "JavaScript", "Java"];
var text = "";
var i;
for (i = 0; i < oursite.length; i++) {
    text += oursite[i] + "<br>";
}
document.getElementById("Array").innerHTML = text;
</script>
</body>
</html>

JavaScript Objects:

Objects are key: value pairs, separated by a comma, are written inside curly brackets.

Ex:  var book = {Name:“JavaScript”, Pages:500, Price=400 }

Example:

<!DOCTYPE html>
<html>
<body>
<h>JavaScript Objects</h>
<p id="demo"></p>
<script>
var Book = {
    Name : "John",   
    Price  : 500,
    Pages : 400 
};
document.getElementById("demo").innerHTML =
Book.Name + " has " + Book.Price + " with no of pages" + Book.Pages;
</script>
</body>
</html>

4. Undefined in Javascript:


A variable without a value, has value undefined.
<!DOCTYPE html>
<html>
<body>
<h>Undefined Data Types</h>
<p id="undefineddemo"></p>
<script>
var x;
document.getElementById("undefineddemo").innerHTML =x;
</script>
</body>
</html>

5. Null in Javascript:


Null means no value. If we want to assign a null value to a variable.
Var a = null;

Know More: List of All Events in JS

The typeof Operator

typeof operator finds the data type of a variable.

Example:

<!DOCTYPE html>
<html>
<body>
<h> typeof operator <h>

<p id="demo"></p>

<script>
var x= 5;
var y ="PWB"
var z=null;
var k=undefined ;
document.getElementById("demo").innerHTML =
typeof x + "<br>" +
typeof y + "<br>" +
typeof z + "<br>" +
typeof k;
</script>
</body>
</html>

Result :

number
string
object
undefined

Here we observe that typeof returns number for a numeric variable, string for a string variable. Undefined for undefined value variable. But returned Object for null type, this will be a bug of JavaScript. The only difference between null and undefined is their data types, either their working behavior is the same.

Read More: For Loop in Javascript.

About Author


Data types in Javascript, this in-depth article is written by Suman Yadav.  She already wrote an article on Functions, What are a Functions in JS. You can also write to us, Submit your article and be a member of our A Programming Solutions Blog. Join us by sending a well-written article for more information visit Write for Us.
Read More »

8/31/2018

What Are Different Types of Sorting Used in C Programming?

There is a list of 5 types of sorting techniques used in C programming Language. In programming, sorting of elements is highly effective to come up with arranged data. Different types of sorting used in c programming. It makes searching a data fast and easy along with saving time for a programmer. The two important types of sorting include internal sorting and external sorting. Main memory is used to store the data associated with these sortings. Here, a proper selection of an algorithm is effective to minimize the CPU cycle and save the time and effort in writing a C program. Don’t just look for bubble sort in C with an example, to embrace a simple solution. There are other forms of sorting algorithms helpful in programming. Here we are going to a list of different type of sorting in C language.

Index of Content



  • Picture of sorting in C Language
  • Selection Sort
  • Program for Selection Sort
  • Bubble Sort
  • Bubble sort GIF
  • Program for Bubble Sort
  • Insertion Sort
  • Program for Insertion Sort
  • Merge Sort
  • Program for Merge Sort
  • Quick Sort
  • Program for Quick Sort


List of Different Types of Sorting Used in C Language

C programming includes following common sorting algorithms for effective data management:

Selection Sort in C


It starts with finding the minimum or maximum data in a list. It is compared with each element in the list and swapped with the one where the selected minimum has to be placed. Every time after the right placement, the algorithm again finds a new minimum out of the remaining data and repeats the process to come up with a sorted list in the end.

Example: Selection Sort Program in C Language with an explanation

Bubble Sort in C


Two adjacent elements in a list are compared and then arranged in a sorted way with the minimum data above. Then, in the next pass, the process is performed on the next pair apart from the sorted data. All out-of-order elements are sorted using.

How Bubble Sort works GIF image of bubble sort in C Language


Example: Bubble Sort Program in C Language with an explanation

Insertion Sort in C


In this algorithm, you start with the first element and then traverse through every element in an array. In the process, if an element is found out-of-order, it is inserted in the right position.

Example: Insertion Sort Program in C Language with an explanation

Merge Sort in C


One of the popular algorithms for sorting, Merge sort follows a recursive pattern to come with a sorted array. The given list is split into two halves and repeatedly invokes merge sort on elements. After all sorted elements, it is merged to form a final array.

Example: Merge Sort Program in C Language with an explanation

Quick Sort in C


Here you need to select an element (pivot) from a list, for example, the last element. Compare and place smaller elements before and higher elements after that pivot. Now repeat the process with the sections containing the smaller and higher elements to sort them out one by one. In the end, you will get a sorted group of data.

Example: Quick Sort  Program in C Language with an explanation

Most of the sorting algorithms compare and place data at its right place. Above mentioned sorting algorithms help C programmers to come up with a sorted list of data. A sorted data offers simplified functions and operations of data in a C program.
Read More »

8/01/2018

Javascript Events Tutorial with Complete List of Events

Events in JavaScript, When User or browser tries to manipulate pages then events occurred to handle the JavaScript’s interaction with HTML. As we know, JavaScript works with HTML, So, Everything that happens with pages loading, clicking a button, minimizing the window, click to mouse etc. all are the events. Like if we display any message to the user on a button click, this happens through the events. there are many things you need to study before reading javascript Events. You must know about Functions in Javascript.

All HTML elements like button, textbox, images can contain events that can be triggered using JavaScript code. And all these events are the part of DOM(Document Object Model). Here we will discuss some most frequent Events used in JavaScript:

Table of Content


  • Events in Javascript
  • Event list in JS Graphical 
  • Onclick Event in Javascript
  • OnMouseOver in Javascript
  • Example of Events
  • List of Event in Javascript
  • Keyboard Event operation perform
  • Event Listeners Flow
  • Author

Mouse and Keyboard Javascript Event Listeners list

Onclick Event


OnClick Event occurs when a left mouse button is clicked, Like if you want to show any message or warning or data validation or something on button click you can do using Onclick event. As we display a message box in the example:

Example

<html>
   <head>   
      <script type="text/javascript">     
            function Welcome() {
               alert("Welcome to Programming With Basics")
            }
      </script>   
   </head>
   <body>
      <form>
        <input type="button" onclick="Welcome()" value="Welcome to PWB"/>
      </form>   
   </body>
</html>

OnMouseOver :


Onmouseover event triggers when the mouse comes over the element contains the mouseover event. For example, if you want to highlight or want to change the color of the text when the mouse comes over the element then this event is used.

Example:

<html>
   <head>
      <script type="text/javascript">     
            function over() {
            var x = document.getElementById("div1");
            x.style.color = 'red';
            }     
      </script>   
   </head>
   <body>
      <div id="div1" onmouseover="mouseover()">
         <h2> Mouse Over </h2>
      </div>     
   </body>
</html>

Read: How Controls Flow Works in Javascript.

Keyboard Event and Mouse Events: You can perform keyboard events test bt visiting a https://www.w3.org/2002/09/tests/keys-cancel2.html or you can also check how many keyboard or mouse events are active in a webpage for more information check below picture.

javascript Keyboard listener properties

Press Ctrl + Shift + I or right click of any webpage click to the event listener, you will see a list of event listeners available for the web page. See the event listener in hacker rank programming solutions for day 8.

hackerrank solution event listeners list on browser

Below is the list of above-defined events, there are many more events in JavaScript:



EventDescription
onclickTriggers on mouse click.
OfflineTriggers when the document goes offline
onblurTriggers when the window loses focus
oncanplayTriggers when media can start play, but might has to stop for buffering
OnabortTriggers on an abort event
onafterprintTriggers after the document is printed
onbeforeprintTriggers before the document is printed
oncanplayTriggers when media can start play, but might has to stop for buffering
onbeforeonloadTriggers before the document loads
oncanplaythroughTriggers when media can be played to the end, without stopping for buffering
oncontextmenuTriggers when a context menu is triggered
onclickTriggers on a mouse click
onchangeTriggers when an element changes
ondblclickTriggers on a mouse double-click
ondragoverTriggers at the start of a drag operation
ondragleaveTriggers when an element is being dragged over a valid drop target
ondragenterTriggers when an element has been dragged to a valid drop target
ondragendTriggers at the end of a drag operation
ondragTriggers when an element is dragged
onendedTriggers when media has reach the end
onemptiedTriggers when a media resource element suddenly becomes empty.
ondurationchangeTriggers when the length of the media is changed
ondragstartTriggers at the start of a drag operation
ondropTriggers when dragged element is being dropped
onerrorTriggers when an error occur
onformchangeTriggers when a form changes
onfocusTriggers when the window gets focus
onforminputTriggers when a form gets user input
oninvalidTriggers when an element is invalid
oninputTriggers when an element gets user input
onhaschangeTriggers when the document has change
onkeypressTriggers when a key is pressed and released
oninvalidTriggers when an element is invalid
onkeydownTriggers when a key is pressed
onloadeddataTriggers when media data is loaded
onkeydownTriggers when a key is pressed
onkeyupTriggers when a key is released
onloadeddataTriggers when media data is loaded
onmouseoutTriggers when the mouse pointer moves out of an element
onmousedownTriggers when a mouse button is pressed
onmousemoveTriggers when the mouse pointer moves
onmessageTriggers when the message is triggered
onloadstartTriggers when the browser starts to load the media data
onmouseupTriggers when a mouse button is released
onplayingTriggers when media data has start playing
onmousewheelTriggers when the mouse wheel is being rotated
onoineTriggers when the document comes online
onofflineTriggers when the document goes offline
onpagehideTriggers when the window is hidden
onpageshowTriggers when the window becomes visible
onpauseTriggers when media data is paused
onplayingTriggers when media data has start playing
onplayTriggers when media data is going to start playing
onpopstateTriggers when the window's history changes
onratechangeTriggers when the media data's playing rate has changed
onprogressTriggers when the browser is fetching the media data
onreadystatechangeTriggers when the ready-state changes
onstorageTriggers when a document loads
onstalledTriggers when there is an error in fetching media data
onseekingTriggers when a media element's seeking attribute is true, and the seeking has begun
onscrollTriggers when an element's scrollbar is being scrolled
onseeked
Triggers when a media element's seeking attribute is no longer true, and the seeking has ended
onresizeTriggers when the window is resized
onredoTriggers when the document performs a redo
onsuspend
Triggers when the browser has been fetching media data, but stopped before the entire media file was fetched
onundoTriggers when a document performs an undo
onwaitingTriggers when media has stopped playing, but is expected to resume
ontimeupdateTriggers when media changes its playing position
onvolumechangeTriggers when media changes the volume, also when the volume is set to mute

Event Listeners Flow:


Check the Event Listener Propagations, How Events work when opening a webpage when we click on a button, link etc.

Flow of Mouse Event Listener in Javascript

Author


This in-depth article of Event in JavaScript is written by Suman Yadav.  You can also write to us, Submit your article and be a member of our A Programming Solutions Blog. Join us by sending a well-written article for more information visit Write for Us.
Read More »

7/31/2018

The Secret of Successful Unity Game Programming

Unity Game Programming Success Secret, In this fast-paced world of advancement, there has been constant development in the field of technology. With few spare moments left out for any person at the end of the day, one hardly gets any time for choosing a good recreation. This is where the online(Unity Game)  and mobile games come to rescue people from boredom. However, the fact is some games turn out to gain insane popularity that most of the times make people go crazy for Unity Game.

They search for excuses to play these games(Unity Game) and fascinate about them too. Some games even get a huge amount of fan following right before they get released even. On the other hand, many games fail to live up to the expectations. There are some who are pushed towards extinction and are never desired to be heard of again. Now the question comes to the forefront,” Why do certain games gain insane popularity and why some games fail to do so?”

unity game programming poster

“What are the elements which go into the creation of such games and make it a runaway success? In simpler words, what are the things which make your online or mobile game on unity 3D programming make a huge amount of money? We can now think of considering two of so many parameters that decide the success of any game and that are:

  1. The players of the game desire to come back to play back the game again.
  2. The player of feels like sharing the game with every person on his contact lists.
The first aspect here can be quoted in other words as stickiness. The online game needs to have that sticky feeling which makes people comes back to play again and again. Once any game gets this aspect embedded in it, gaining popularity is only a matter about some days. Such games designed in unity 3D programming(Programming of Unity Game) gain popularity as they form a playing habit among the users.

This ultimately makes the game popular. So big companies get into deeper levels and are not obsessed with devising a product that is technically perfect. They just get into changing certain fundamental aspect in the users; “habits.” You can even perceive it in this way that all big companies build a specific habit in the customers and consumers. And why do they give so much of importance to habit forming?

It is because habit tends to get settled into the daily routines and people do these without making any analysis. This way the gaming firm does not use any additional reminders for reminding you to continue playing the game regularly; people do it every day. The second aspect that plays a huge role in having successful unity 3D programming is the phrase ”Sharing is Caring.”

The element of sharing is one very sophisticated yet subtle method if increasing the popularity of the game. The gaming firm cares about you and wants you to gain more rewards. Hence, they use this strategy for making you have a higher level of the game and improving the score of their success too. This is one wonderful way through which companies are dealing with unity 3D programming gain in their popularity.

This works in multiple ways though. When any game gets many numbers of people playing it, it gets viral. This also adds to the stickiness of the game and makes it viral all over the internet. The companies use the premium pricing strategy. This is heightened when a social context is added within which other users would prefer spending money. When the matter arises about the success of unity 3D programming, there are some more secrets that you can use for sure.
Things that are worth trying in unity 3D programming

  • When beginning with this programming, some programmers might forget that they are using the Play mode. You need to choose “Preferences” and then select “Colors” followed by “Playmode Tint.” This will make the gamers have a more colorful gaming experience.
  • To have an easy and simple positioning of the camera, you can find the exact angle that you would prefer by using navigation in the scene view. Choose the main camera and then choose “align with a view.” You can also use the shortcut of “Ctrl/Cmd+Shift+F.
  • Do you feel like a lack in the assets in the project view? Type the SEARCH terms in the Project View and then later the search to “Asset Store” from “Asset.”
  • While making an object to rotate, you can hold the Ctrl/Cmd for snapping the rotation effect. This method is also applicable to any object that is in motion. The values in default snap can be changed under “Snap settings” in the “Edit menu.
  • You can also is another snapping secret. You can hold the V key down and choose and place objects by using “Vertex Snapping.” This can be of great help when the gaming construction is on par with the modular geometry.
  • Did you find the perfect game values while playing and testing in Play mode? Click on the cog-shaped image present on the top right corner of the inspector’s component and then choose “Copy Component.

Author Bio:


Sunny Chawla is a Marketing Manager at AIS Technolabs – a Web design and Development Company. Helping global businesses with unique and engaging tools for their business. He would love to share thoughts on Ecm Services, Web Designing, and Unity 3D Programming.
Read More »