/**
* Exercise:
*
* Function Scope
*
* Work in the console, or use "console.log()" to output to the console
* from the script
*
* You could also use console.assert(boolean) to test your functions
*
* Part 1: Write two functions that share the same, global variable
* - Verify this by logging the value of the variable to the console
* from within each function
*/
var x = 10
function add1(){
y = x + 10;
}
add1()
console.log(y)
function sub1(){
z = x + y;
}
sub1()
console.log(z)
console.log('_________________')
// write solution to part 1 here
/*
* Part 2: Write two functions that share a non-global variable
* - Verify this by logging the value of the variable to the console
* from within each function
*
* Hint: You can write functions inside of other functions...
*/
var x = 5
function add(){
y = 0;
y = x + 9;
}
add()
console.log(y)
function sub(){
z = x + y;
}
sub()
console.log(z)
// write solution to part 2 here
/*
* Bonus: Write a function (outer) that returns another function (inner)
*
* The outer function expects one Number parameter, x
* The inner function should add 1 to x and log it to the console
*
* Hint: You will need to run the outer function once
* in order to get the inner function
*/
function add2(){
y = 0;
y = x + 9;
}
add2()
console.log(y)
function sub2(){
z = x + y;
return z
function dev(){
z = y + x
console.log(z);
}
dev()
}
console.log(z)
// write solution to bonus here
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.