Skip to content

Instantly share code, notes, and snippets.

@SanthoshRaju91
Last active May 14, 2018 03:18
Show Gist options
  • Save SanthoshRaju91/686dfd8e902cfdfba4495aa4d3cf89a5 to your computer and use it in GitHub Desktop.
Save SanthoshRaju91/686dfd8e902cfdfba4495aa4d3cf89a5 to your computer and use it in GitHub Desktop.
Practicing my algorithm skills

Finding value of deeply nested objects with object keys.

function findKeyValue(paymentObj, key) {
	const keySplit = key.split('.');
	let value = '';
	function findValue(obj, first, ...keys) {
    	if(typeof obj[first] === 'object') {
    		if(keys) {
              findValue(obj[first], ...keys);
    		}
        } else {             	
    		value = obj[first];
    	}
    }
  	findValue(paymentObj, ...keySplit);
  	return value;
}

Finding the first occurence in a string

const input = "ABCA";

function firstOccur(input) {
	let countHash = {};
  	
  	let inputAsArray = input.split('');
  	for(var i in inputAsArray) {
      	let key = inputAsArray[i];
    	if(Object.keys(countHash).includes(key)) {
        	return count[key];
        } else {
        	countHash[inputAsArray[i]] = 1;
        }
    }
  return 'none';
}

const output = firstOccur(input);

Finding number of occurences for a character in a given string

const input = "ABCA";

function firstOccur(input) {
	let countHash = {};
  	
  	let inputAsArray = input.split('');
  	for(var i in inputAsArray) {
      	let key = inputAsArray[i];
    	if(Object.keys(countHash).includes(key)) {
        	countHash[key] = countHash[key] + 1;
        } else {
        	countHash[inputAsArray[i]] = 1;
        }
    }
  return countHash;
}

const output = firstOccur(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment