Last active
December 11, 2022 21:10
-
-
Save victory-sokolov/38812b8508a324d71ee1 to your computer and use it in GitHub Desktop.
JavaScript Cheat Sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
================== | |
//Arrays | |
================== | |
//Get Max Value from Array | |
var arr = [25,1,54,45]; | |
var res = Math.max.apply(Math, arr); | |
//Random Element from an Array | |
var arr = ["Apple","Orange","Waterlemon"]; | |
var rand = arr[Math.floor(Math.random()*arr.length)]; | |
//Filter Array from biggest value to lowest | |
var arr = [1,2,5,7,18]; | |
var res = arr.sort(function(a,b){ | |
return a < b; | |
}); | |
//Get length of each element in Array | |
var names = ['HTML', 'CSS', 'JavaScript']; | |
var nameLengths = names.map(function(name){ | |
return name.length; | |
}); | |
//First letter to Uppercase | |
var names = ["viktor", "andrew", "vlad", "john"]; | |
var toUpper = names.map(function(name) { | |
var firstLetter = name[0]; | |
return firstLetter.toUpperCase() + name.substring(1); | |
}); | |
/ Shuffles array in place. | |
* @param {Array} a items The array containing the items. | |
*/ | |
function shuffle(a) { | |
var j, x, i; | |
for (i = a.length; i; i -= 1) { | |
j = Math.floor(Math.random() * i); | |
x = a[i - 1]; | |
a[i - 1] = a[j]; | |
a[j] = x; | |
} | |
} | |
var myArray = ['1','2','3','4','5','6','7','8','9']; | |
shuffle(myArray); | |
//Character remaining in Textarea | |
var txt = document.getElementById("txtarea"); | |
txt.setAttribute("maxlength", 50); | |
var maxLength = 50; | |
var demo = document.getElementById("demo"); | |
var demoTxt = "Character remaining: " + maxLength; | |
demo.innerHTML = demo.innerHTML + demoTxt; | |
txt.addEventListener("keyup", function() { | |
if(txt.value.length <= maxLength) { | |
var result = demo.innerHTML = " Character remaining: " + (maxLength-txt.value.length); | |
} | |
}); | |
//If Keyboard key pressed | |
addEventListener('keypress',function(){ | |
if(event.which === 13) {} | |
}); | |
//Removing duplicated item from string | |
var uniqueList=string.split(',').filter(function(item,i,allItems){ | |
return i==allItems.indexOf(item); | |
}).join(','); | |
======================== | |
String & numbers examples | |
======================== | |
//reverse numbers with loop | |
var n = 352, reverse = 0, remainder; | |
while (n>0) { | |
remainder = n%10; | |
reverse = reverse * 10 + remainder; | |
n = Math.floor(n / 10); | |
} | |
console.log(reverse); | |
========== | |
//DOM Manipulation | |
========= | |
//Extract href from class | |
var domains = document.querySelectorAll('.a-href'); | |
for(var i = 0; i < domains.length; i++) { | |
console.log(domains[i].getAttribute('href')); | |
} | |
//Select are to copy | |
var emailLink = document.querySelector(".js-emaillink"); | |
var btn = document.querySelector(".js-emailcopybtn"); | |
btn.addEventListener("click", function(){ | |
var range = document.createRange(); | |
range.selectNode(emailLink); | |
window.getSelection().addRange(range); | |
}); | |
//Check if strings contains a keyword from array | |
ar tp = ['youtube','video']; | |
var lnk = 'www.youtube.com/video-channel.html'; | |
tp.forEach(function (keyword) { | |
if (lnk.indexOf(keyword) > -1) console.log(lnk); | |
else console.log("not found in"); | |
}); | |
//Regular expression: | |
var lnk = 'www.youtube.com/video-channel.html'; | |
var RE = /(youtube|video)/; | |
if (RE.test(lnk)) console.log('keyword found'); | |
else console.log('keyword not found'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Reverse String | |
function reverse(s){ | |
return s.split("").reverse().join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment