Skip to content

Instantly share code, notes, and snippets.

View suhailgupta03's full-sized avatar
:octocat:

Suhail Gupta suhailgupta03

:octocat:
View GitHub Profile
let string = `Hi! Recently India celebrated its Independence Day`
let pattern = /^Hi.*Day$/g
// .* means any character any number of times
// * means 0 or more times
console.log(pattern.test(string))
let string = `Hi! Recently India celebrated its
Independence Day.`
let pattern = /[ezx]/g
// here g means global
// it is used to find all the matches
console.log(pattern.test(string));
// test method returns true if the pattern is
// found in the string
// false if not found
let string = `Hi! Recently India celebrated its
Independence Day.`
let regex = /[izx]/
// [izx] means match any of the characters
// 'i', 'z', or 'x'
console.log(regex.test(string)) // true
let string = `Hi! Recently India celebrated its
Independence Day`
// If we want to know if the string ends with a particular
// word / character or not
// we can use the $ sign to check if the string ends with
// a particular word or not
// Like we have ^ to check if the string starts with a character / word
let string = "Hi! Recently India celebrated its Independence Day."
// If we want to know whether a string starts with a particular word or not,
// we can use ^ symbol in the beginning of the word to check it.
let pattern = /^H/
// ^ is the symbol for 'start of the string'
// by default the matches are case sensitive
// this means that it will be differentiate between 'H'
// and 'h'
let string = "Recently India celebrated its Independence Day."
// Problem: I want to know if in the above sentence "ebr" occurs togethor or not
// Solution: Use regex to find the pattern
let regex = /ebr/
// regular expression means a pattern
// pattern that we want to find
// Here the pattern is ebr
// regular expression starts with / and ends with /
let listWithDuplicateValues = [1,1,1,1,2,3,4,5,6,5];
let frequencyMap = {};
/**
* {
* 1:4,
* 2: 1,
* 3: 1
* }
*/
for(let i = 0; i < listWithDuplicateValues.length; i++) {
let studentAgeList = [
20, 21, 22, 20, 23, 25, 27, 22, 29, 30
];
console.log(studentAgeList)
// unique ages = [20, 21, 22, 23, 25, 27, 29, 30]
var ageSet = new Set();
// we use set when we want to store
// unique values and we don't care about
// the order
let list = [1,2,3,4,5];
let newList = []; // [2,3,4,5,6]
// we can use a map function to achieve this
// map function takes a callback function as an argument
// and returns a new array
let result = list.map(function(item) {
return item + 1
})
// FILE - 1
function greet() {
console.log('Hello');
}
module.exports = greet;
// this is the way to export a function
// from a module
// module means file