Skip to content

Instantly share code, notes, and snippets.

@AminBusiness
Created November 9, 2018 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AminBusiness/9247aadb68ae16483eb277bc6dcf389c to your computer and use it in GitHub Desktop.
Save AminBusiness/9247aadb68ae16483eb277bc6dcf389c to your computer and use it in GitHub Desktop.
<script id="jsbin-javascript">
// Challenge 5. Max Character
// Returns a character that is most common in a string
//
function maxCharacter(str)
{
const charMap = {};
let maxNum = 0;
let maxchar = '';
str.split('').forEach(function(char) {
if(charMap[char])
{
charMap[char]++;
}
else
{
charMap[char] = 1;
}
});
//For in loop is used to loop through an object rather than array
for(let char in charMap)
{
//debugger;
if (charMap[char] > maxNum)
{
maxNum = charMap[char];
maxChar = char;
}
}
console.log(charMap)
}
const output = maxCharacter('javascrddddipt');
console.log(output);
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Challenge 5. Max Character
// Returns a character that is most common in a string
//
function maxCharacter(str)
{
const charMap = {};
let maxNum = 0;
let maxchar = '';
str.split('').forEach(function(char) {
if(charMap[char])
{
charMap[char]++;
}
else
{
charMap[char] = 1;
}
});
//For in loop is used to loop through an object rather than array
for(let char in charMap)
{
//debugger;
if (charMap[char] > maxNum)
{
maxNum = charMap[char];
maxChar = char;
}
}
console.log(charMap)
}
const output = maxCharacter('javascrddddipt');
console.log(output);</script>
// Challenge 5. Max Character
// Returns a character that is most common in a string
//
function maxCharacter(str)
{
const charMap = {};
let maxNum = 0;
let maxchar = '';
str.split('').forEach(function(char) {
if(charMap[char])
{
charMap[char]++;
}
else
{
charMap[char] = 1;
}
});
//For in loop is used to loop through an object rather than array
for(let char in charMap)
{
//debugger;
if (charMap[char] > maxNum)
{
maxNum = charMap[char];
maxChar = char;
}
}
console.log(charMap)
}
const output = maxCharacter('javascrddddipt');
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment