Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds isAnagram algorithm in Javascript #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Adds isAnagram algorithm in Javascript #39

wants to merge 2 commits into from

Conversation

waghcwb
Copy link
Contributor

@waghcwb waghcwb commented Oct 24, 2018

Fixes #35

@shingz96
Copy link

Since Anagrams don't necessarily have to use all the letters, but they normally do, it will be nice if allow both scenarios

function isAnagram(word1, word2, useAll){
	//default need use all
	//Anagrams don't necessarily have to use all the letters, but they normally do.
	if(typeof useAll == "undefined" || typeof useAll != "boolean"){
		useAll = true;
	}
	
	//check string only
	if(typeof word1 != "string" && typeof word1 != "string"){
		throw Error("Invalid argument! Only String type is allow to compare!");
	}
	
	//normalise to array
	let arr1 = word1.replace(/\s/g, "").toLowerCase().split('').sort();
	let arr2 = word2.replace(/\s/g, "").toLowerCase().split('').sort();

	//check anagrams that must use exatcly all 
	if(useAll || arr1.length == arr2.length){
		return arr1.join('') == arr2.join('');
	}
	
	//determine which is part of another
	let whole = arr1.length > arr2.length ? arr1 : arr2;
	let part = arr2.length < arr1.length ? arr2 : arr1;
	
	for(let i = 0; i < whole.length; i++){
		let index = part.indexOf(whole[i]);
		part.splice(index,1);
	}
	
	return part.length == 0;
}

//example use
isAnagram('why','how') // expected output: false
isAnagram('funeral','real fun') // expected output: true
isAnagram('funeral','real funs') // expected output: false
isAnagram('funeral','real funs',false) // expected output: true

@waghcwb
Copy link
Contributor Author

waghcwb commented Oct 24, 2018

@shingz96,

Thank you, I've updated the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants