Skip to content

Instantly share code, notes, and snippets.

View HashirHussain's full-sized avatar
🎯

Hashir Hussain HashirHussain

🎯
View GitHub Profile
@HashirHussain
HashirHussain / duplicateCount.js
Created January 5, 2024 08:48
Count the number of Duplicates
/**
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
@HashirHussain
HashirHussain / domainName.js
Created January 4, 2024 05:44
Write a function to parses out just the domain name and returns it as a string.
/**
Write a function that when given a URL as a string, parses out just the domain name and returns it as a string.
For example:
url = "http://github.com/carbonfive/raygun" -> domain name = "github"
url = "http://www.zombie-bites.com" -> domain name = "zombie-bites"
url = "https://www.cnet.com" -> domain name = cnet"
**/
function domainName(url) {
@HashirHussain
HashirHussain / moveZeros.js
Last active January 3, 2024 16:42
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
/**
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
e.g.
moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
**/
function moveZeros(arr) {
//code me
}
@HashirHussain
HashirHussain / capitalizeFirstLetter.js
Created January 1, 2024 14:32
Complete the capitalizeFirstLetter below while being sure to capitalize the initial letter of each word in the provided string.
/**
Complete the capitalizeFirstLetter below while being sure to capitalize the initial letter of each word in the provided string.
e.g.
const str = "How can mirrors be real if our eyes aren't real"
str.capitalizeFirstLetter() //-> "How Can Mirrors Be Real If Our Eyes Aren't Real"
**/
String.prototype.capitalizeFirstLetter = function () {}
@HashirHussain
HashirHussain / uncompress-bfe.dev.js
Created October 4, 2023 09:23
uncompress bfe.dev solution in javaScript
function uncompress(str) {
var result = str;
while (/\d/.test(result)) {
var start = result.lastIndexOf("(")
var end = result.indexOf(")")
var rounds = parseInt(result[start - 1], 10);
var repeats = result.substring(start + 1, end).repeat(rounds)
@HashirHussain
HashirHussain / myMap.js
Created October 4, 2023 05:29
Array.prototype.map clone
Array.prototype.myMap = function(callback) {
const result = [];
for(let i = 0; i < this.length; i++) {
result.push(callback(this[i], i, this));
}
return result;
}
@HashirHussain
HashirHussain / undefinedToNull.js
Last active March 28, 2024 07:15
convert undefined to null
function undefinedToNull(arg) {
if (arg == undefined || arg == null) return null
if (Array.isArray(arg)) return arg.map(undefinedToNull)
if (arg.constructor === Object) {
Object.keys(arg).forEach(key => {
arg[key] = undefinedToNull(arg[key])
})
}
@HashirHussain
HashirHussain / react-standards-checklist
Created June 1, 2021 08:33
React standards checklist
1. `key` should be stable, predictable and unique amongst the list items and their children.
2. Split Presentational and container components.
3. Presentational components only hold the markup.
4. Always make the presentational component a functional component.
5. A container holds the state or other business conditions.
6. Use `React.PureComponent` for Container and `React.memo` for Presentational component.
7. Use ternary operator as little as possible in both components and the render method.
8. Avoid using context in the application.
9. Always define default props.
10. Avoid using nested state.
@HashirHussain
HashirHussain / numberToOrdinal.js
Created March 22, 2021 10:11
Convert number to ordinal format
function numberToOrdinal(input) {
try {
if(typeof input !== 'number') {
throw('Please provide valid number');
}
const lastDigit = input % 10;
let ordinal = '';
if(lastDigit === 1 && input !== 11) {
ordinal = 'st';
} else if (lastDigit === 2 && input !== 12) {
/*We know the purpose of classes in sense of programming concepts.
In JavaScript `class` is the makeover upon traditional prototype-inheritance.
*/
//ES5 Approach:
function Person(first, last) {
this.first = first;
this.last = last;
}