JavaScript Array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="element"><button id="add">Add</button>
<div id="list">
</div>
<button onclick="Map1();">Map</button>
<button onclick="Filter2();">Filter</button>
<button onclick="reduce();">Reduce</button>
<button onclick="ForEACH();">ForEach</button>
<button onclick="Find();">Find</button>
<button onclick="Include();">Include</button>
<script>
let task=[];
let element=document.getElementById("element");
let add=document.getElementById("add");
let list=document.getElementById('list');
// task.push(5,34,45,5,34);
//================
for(let i=0;i<=10;i++){
task.push(i);
}
task.push('A');
// task.push(5,34,4,5,354,800);
display();
add.onclick=()=>{
task.push(element.value);
display();
element.value="";
}
function display(){
list.innerHTML="";
task.forEach(element => {
list.innerHTML=list.innerHTML+`<h3>${element}</h3>`;
});
}
//===============Map==
function Map1(){
let newArray= task.map(item=>item*10);
console.log(newArray);
}
function Filter2(){
// let newArray= task.filter(item=>item===5);
let newArray= task.filter(item=>item%2===0);
console.log(newArray);
}
function reduce(){
// let newArray= task.filter(item=>item===5);
let newValue= task.reduce((total,item)=>total+item,0);
console.log(newValue);
}
//===================forEach==a[6]=====value=start_address*(6+1); 100+(6*1)=106
function ForEACH(){
task.forEach((item,index)=>{
console.log(`Index=${index} Value=${item}`);
});
}
function Find(){
let n=parseInt(prompt("Enter Number"));
let r= task.find((item)=>item==n);
console.log(r);
}
function Include(){
let n=parseInt(prompt("Enter Number"));
let r= task.includes(n);
alert(r);
}
</script>
</body>
</html>
Comments
Post a Comment