Skip to content

Instantly share code, notes, and snippets.

@vladislavaSim
Last active November 22, 2021 20:56
Show Gist options
  • Save vladislavaSim/05d8c4c628b7fc83703649b5dcace270 to your computer and use it in GitHub Desktop.
Save vladislavaSim/05d8c4c628b7fc83703649b5dcace270 to your computer and use it in GitHub Desktop.
object_counter_of_symbols_in_the_string
//make an object where keys are symbols and values are numbers how many times those symbols are used in the string
let str = 'Ave Maria, gratia plena, Dominus tecum, benedicta tu in mulieribus et benedictus fructus ventris tui Jesus'
let arr = str.split(''); //transform the string into array splitting on every symbol
let counter = {}; //assign the empty object
for(let item of arr) { //iterate every symbol in the array
if(counter[item] === undefined) { //if there are no such key yet
counter[item] = 1 //then we assign this key with value 1
} else { // and if there are such key already
counter[item]++ //then we count it as + 1
}
}
console.log(counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment