The most viewed items
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo">forEach Loop</p>
<script>
var cities, text;
cities = ["Yangon", "Mandalay", "Naypyidaw", "Meikhtila"];
text = "<ul>";
cities.forEach(jsFunction);
text+= "</ul>";
function jsFunction(value){
text += "<li>" + value + "</li>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo1"></p>
<p id = "demo2"></p>
<script>
//good habit in crating array
var a = ["aaa", "bbb", "ccc", "ddd"];
document.getElementById("demo1").innerHTML = a;
//bad habit in creating array
var b = new Array("aaa", "bbb", "ccc", "ddd");
document.getElementById("demo2").innerHTML = b;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo1"></p>
<p id = "demo2"></p>
<script>
var a = ["aaa", "bbb", "ccc", "ddd"];
a[0] = "eee";
document.getElementById("demo1").innerHTML = a;
var b = new Array("aaa", "bbb", "ccc", "ddd");
document.getElementById("demo2").innerHTML = b[2];
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Code Testing</title>
</head>
<body>
<p id = "demo">forEach Loop</p>
<script>
var cities, text;
cities = ["Yangon", "Mandalay", "Naypyidaw", "Meikhtila"];
text = "<ul>";
cities.forEach(jsFunction);
text+= "</ul>";
function jsFunction(value){
text += "<li>" + value + "</li>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
const tnwNames = ["Moe", "Soe", "Noe", "Mya"];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNames.join(", ");
}
function tnwFun2(){
document.getElementById("demo").innerHTML = "The original array is : " + tnwNames.join(", ");
let tnwNewvalue = document.getElementById("newname").value;
tnwNames.unshift(tnwNewvalue);
document.getElementById("demo1").innerHTML = "The new array is : " + tnwNames.join(", ");
}
const tnwNames = ["Hla", "Mya", "Soe", "Moe", "Noe"];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNames.join(", ");
}
function tnwFun2(){
document.getElementById("demo").innerHTML = "The original array is :
" + tnwNames.join(", ");
let result = tnwNames.splice(2, 2, "Aung", "Kyaw", "Tun");
document.getElementById("demo1").innerHTML = "The removed elements are :
" + result.join(", ");
document.getElementById("demo2").innerHTML = "The new array is :
" + tnwNames.join(", ");
}
//1. sort alphabetically
/**
const countries = ["Myanmar", "Singapore", "Malaysia", "USA", "Thailand", "Vietname"];
function tnwFun1(){
document.getElementById("demo").innerHTML = countries.join("
");
}
function jsSort(){
let result = countries.sort();
document.getElementById("demo").innerHTML = result.join("
");
}
function jsReverse(){
let result = countries.reverse();
document.getElementById("demo").innerHTML = result.join("
");
}
//2. Sort randomly
const tnwNums = [20, 17, 30, 38, 40, 50, 28, 12];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join("
");
}
function tnwFun2(){
let result = tnwNums.sort(tnwFun3);
document.getElementById("demo").innerHTML = result.join("
");
}
function tnwFun3(a, b){
return 0.5 - Math.random();
} */
// 3. Sort Numerically
const tnwNums = [20, 17, 30, 38, 40, 50, 28, 12, 4];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join("
");
}
function jsSort(){
let result = tnwNums.sort(function(a, b){return a - b;});
document.getElementById("demo").innerHTML = result.join("
");
}
function jsReverse(){
let result = tnwNums.sort(function(a, b){return b - a;});
document.getElementById("demo").innerHTML = result.join("
");
}
const tnwNums = [20, 39, 48, 58, 68, 70, 85, 90];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join(", ");
}
function tnwFun2(){
let result = tnwNums.some(tnwFun3);
document.getElementById("demo").innerHTML = tnwNums.join(", ");
document.getElementById("demo1").innerHTML = result;
}
function tnwFun3(num){
let chkInput = document.getElementById("chknum").value;
return num > chkInput;
}
const tnwNums = [20, 29, 30, 39, 40, 47, 50, 70];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join(", ");
}
function tnwFun2(){
let result = tnwNums.slice(1, 4);
document.getElementById("demo").innerHTML = "The original is: " + tnwNums.join(", ");
document.getElementById("demo1").innerHTML = "The new array is: " + result.join(", ");
}
const tnwNums = [20, 30, 40, 50, 60, 70, 90];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join(", ");
}
function tnwFun2(){
let result = tnwNums.shift();
document.getElementById("demo").innerHTML = tnwNums.join(", ");
document.getElementById("demo1").innerHTML = "The removed element is: " + result;
}
const tnwNums = [20, 30, 40, 50, 60, 70];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNums.join("
");
}
function tnwFun2(){
let result = tnwNums.reverse();
document.getElementById("demo").innerHTML = result.join("
");
}
const tnwNum = [20, 9, 20, 8, 100];
function tnwFun1(){
document.getElementById("demo").innerHTML = tnwNum.join(", ");
}
function tnwFun2(){
var result = tnwNum.reduceRight(tnwFun3);
document.getElementById("demo").innerHTML = result;
}
function tnwFun3(numRight, sum){
return numRight - sum;
}