Created
November 9, 2018 01:30
-
-
Save AminBusiness/9247aadb68ae16483eb277bc6dcf389c to your computer and use it in GitHub Desktop.
// source https://jsbin.com/gavovuh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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