Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
soniprem831 authored Jul 11, 2024
1 parent 76468fc commit 69e5eaf
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
//named-function
// document.querySelectorAll(".drum")[0].addEventListener("click",handleclick);
// document.querySelectorAll(".drum")[0].addEventListener("click",handleclick);
// function handleclick(){
// alert("I got clicked!");
// }

// anonymous function
var numberOfDrumButton=document.querySelectorAll(".drum").length;
for(var i=0;i<numberOfDrumButton;i++)
for(var i=0;i<numberOfDrumButton;i++){
document.querySelectorAll(".drum")[i].addEventListener("click",function(){
// alert("I got clicked!");
// this->to select clicked one
// this.style.color="white";
var buttonInnerHtml=this.innerHTML;
switch (buttonInnerHtml) {
//Detecting key clicked
makesound(buttonInnerHtml);
buttonAnimation(buttonInnerHtml);
});
}

document.addEventListener("keypress",function(Event){
//Detecting key-board button press
makesound(Event.key);
buttonAnimation(Event.key);
});

function makesound(key){
switch (key) {
case 'w':
var tom1=new Audio("sounds/tom-1.mp3");
tom1.play();
Expand Down Expand Up @@ -47,7 +60,18 @@ document.querySelectorAll(".drum")[i].addEventListener("click",function(){
console.log("no sound");

}
});
}

function buttonAnimation(currentKey){
var activeButton = document.querySelector("."+currentKey);

activeButton.classList.add("pressed");

setTimeout(function(){
activeButton.classList.remove("pressed");
},100);
}

// var audio=new Audio("sounds/tom-1.mp3");
// audio.play();
// higher order function
Expand All @@ -70,3 +94,30 @@ document.querySelectorAll(".drum")[i].addEventListener("click",function(){
// function calculator(num1,num2,operator){
// return operator(num1,num2);
// }






// callback function:-

// function anotherAddEventListener(typeOfEvent,callback) {
// //Detect Event Code....

// var eventThatHappened={
// eventType:"keypress",
// key:"P",
// durationOfKeyPress:2
// }

// if(eventThatHappened.eventType===typeOfEvent){
// callback(eventThatHappened);
// }

// }

// anotherAddEventListener("keypress",function (event){
// console.log(event);
// });

0 comments on commit 69e5eaf

Please sign in to comment.