From 97ebb564a36bc9a24eee7c9896530c9f588477d7 Mon Sep 17 00:00:00 2001 From: Umit Ilhan Date: Sun, 29 Sep 2019 12:25:23 -0400 Subject: [PATCH] Week 10 practice --- homework1.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 homework1.js create mode 100644 index.html diff --git a/homework1.js b/homework1.js new file mode 100644 index 0000000..90b3203 --- /dev/null +++ b/homework1.js @@ -0,0 +1,64 @@ +// Problem 1:Create a JavaScript code with global and local variable. Call your local variable outside of its function. Why do you receive an error? +console.log("problem 1 -----------"); +let global = "global variable"; + +for(let i = 0; i<3; i++){ + let local = "local variable"; + console.log("inside", local); +} + +console.log("outside", global); +// ANSWER: we can't call local variable from outside. we get error "local variable is not defined". because if you assign varibale inside any loob or function. you can use only inside this loob or function +// console.log("outside", local); +// -------------------------- +// Problem 2:Create a program where a button acts like a counter. Everytime an user clicks on it, it counts up (starting from 0). Make sure you use closures (create two functions, one outer and one inner). +console.log("problem 2 -----------"); +const p2like = document.getElementsByClassName("p2like")[0]; +let likeCounter = document.getElementsByClassName("likecounter")[0]; +var counter = 0; +var like = (function(){ + return function(){ + return counter +=1; + } +})(); +p2like.addEventListener("click",function(){ + like(); + likeCounter.innerHTML = counter; +}) + + + +// Problem 3: Create a program with a button. When an user clicks on the button, the program executes an alert stating “the user has clicked!” +console.log("problem 3 -----------"); +const p3click = document.getElementsByClassName("p3click")[0]; +console.log(p3click); +p3click.addEventListener("click", function(){ + console.log("clicked"); + alert("the user has clicked!"); +}) + +// Problem 4: Create a form where the user is asked to enter their first and last name. Also, create a reset button, so that when an user clicks reset, the form is reset(their input is deleted). +const fullName = document.getElementById("fullName"); +const firstName = document.getElementById("firstName"); +const lastName = document.getElementById("lastName"); +const createButton = document.getElementById("create"); +const deleteButton = document.getElementById("delete"); +console.log(createButton); +createButton.addEventListener("click", function(){ + alert("Your first and last name " + firstName.value + " " + lastName.value); +}); +deleteButton.addEventListener("click",function(){ + fullName.reset(); +}) + +// Problem 5: Consider the code below. Try running this code in your browser. You will be outputted with an error. Fix the code so “Welcome Guest” appears. + +function addalert(content){ + document.getElementById("demo").innerHTML = content; +} +try { + addalert("Welcome guest!"); +} +catch(err) { + document.getElementById("demo").innerHTML = err.message; +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..d5712d6 --- /dev/null +++ b/index.html @@ -0,0 +1,39 @@ + + + + + + + Home work 10 + + +

Answer of practice problems for Week 10

+
+

You can see answer in the console.

+
+
+

Problem 2

+

Like Counter: 0

+ +
+
+

Problem 3

+ +
+
+

Problem 4

+
+ + +
+ + +
+
+

Problem 5

+

+
+ + + + \ No newline at end of file