Skip to content
Akin C edited this page Apr 15, 2018 · 6 revisions

Welcome to the Javascript-hoisting-and-try-catch- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


Variable hoisting in Javascript describes the usage of variables within a scope. Javascript´s variables are function-scoped rather than block-scoped.

One exception regarding to block scoping is try...catch:

function anyFunction()
{
	var anyVariable = 0;
		
	try 
	{
	   throw "an exception"; 
	}
	catch (anyVariable) 
	{
	   anyVariable  = 100;
	}
	
	console.log(anyVariable);//Outputs 0
}

In the code above the variable anyVariable is used as an exception handler for the catch statement. In consequence the variable will not take the assigned value of 100 within that catch block. To achieve the new assignment, catch needs a different variable for error handling. For example:

//This listing contains only the changed aspect from the first listing above
	catch (err) 
	{
	   anyVariable  = 100;
	}

Content

The user interaction part should look like the content as seen below by starting "index.html" in a web browser.

ERROR: No image found!

The program works as follows:

  1. A random number between 10 and 1000 is generated.
  2. The User can guess with the help of two options in what interval the number lies.
  3. The result of guessing will be updated.
  4. If wanted a new random number can be generated.
  5. The failed guessings will be counted within a catch block, but should fail to do so.

To use the project just download the files and execute "index.html". Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.

Clone this wiki locally