Skip to content

Instantly share code, notes, and snippets.

View shoaibmehedi7's full-sized avatar
:octocat:
Working from home

MD Shoaib Mehedi shoaibmehedi7

:octocat:
Working from home
View GitHub Profile
const students = [
{"name":"shoaib","age":20},
{"name":"alex","age":10},
{"name":"mehedi","age":5},
{"name":"linda","age":12},
{"name":"lilly","age":2},
{"name":"william","age":1},
]
var x = 10;
function createFunction1() {
var x = 20;
return new Function('return x;'); // this |x| refers global |x|
}
function createFunction2() {
var x = 20;
function f() {
// Simple way
const person = { name: "Shoaib", Age: 25, Gender: "Male" };
for (const property in person) {
console.log(`${property}: ${person[property]}`);
}
// Another way
const anotherPerson = {
Name: 'Shoaib',
Age: 25,
// reverse using basic functions
function reverse(string) {
return string.split("").reverse().join("");
}
console.log(reverse('shoaib'));
// reverse using loop
function reverse(string) {
var newString = "";
for (var i = string.length - 1; i >= 0; i--) {
const students = [
{name: 'Shoaib', roll: 2},
{name: 'Mehedi', roll: 10},
{name: 'Alex', roll: 5}
];
function search(student) {
return student.name=== "Mehedi";
}
let arrOne = [1,3,5]
let arrTwo = [2,4,6]
// first way
let combine = arrOne.concat(arrTwo)
console.log(combine)
// second way
combine = [...arrOne,...arrTwo]
console.log(combine)
// sort alphabetically
const name = ['Shoaib', 'Mehedi', 'Alex', 'Jane'];
const arr = [1, 30, 4, 21, 100000];
name.sort();
console.log(name);
// sort numerically
arr.sort(function(a, b) {
return a - b;
});
function mostRepeated(array){
return array.sort((a,b) =>
array.filter(v => v===a).length
- array.filter(v => v===b).length
).pop();
}
console.log(mostRepeated(['shoaib', 'alex', 'mehedi', 'alex'])); // apple
var object = {
res: 0,
add: function(a, b) {
this.res = a + b;
return this;
},
multiply: function(a) {
this.res = this.res * a;
return this;
const students = [
{"name":"shoaib","age":20},
{"name":"alex","age":10},
{"name":"mehedi","age":5},
{"name":"linda","age":12},
{"name":"lilly","age":2},
{"name":"william","age":1},
]
let filter = students.filter((student)=>{
return student.age <= 10