Skip to content

Instantly share code, notes, and snippets.

@natafaye
Last active February 9, 2022 02:48
Show Gist options
  • Save natafaye/fbd6ff2acf1c3620f7ad23a428fca290 to your computer and use it in GitHub Desktop.
Save natafaye/fbd6ff2acf1c3620f7ad23a428fca290 to your computer and use it in GitHub Desktop.
// The same thing as the other one, I just changed the syntax of the first two lines to ES6 Classes, if that makes it clearer
class List {
countSpecDigits(integersList, digitsList){
// keep a tally of the digits that I see that I'm supposed to count
const tallyList = digitsList.map(number => [number, 0]);
// loop over the list of integers
for(const integer of integersList) {
const integerDigitList = String(integer).split("").map(Number);
for(const digit of integerDigitList) {
// make our check
// make sure to ignore NaN
if( digitsList.includes(digit) ) {
// go into tally list, find the right array and increment to the tally spot
const tallyPair = tallyList.find(pair => pair[0] === digit);
tallyPair[1] += 1;
}
}
}
return tallyList;
}
}
function List(){
this.countSpecDigits=function(integersList, digitsList){
// keep a tally of the digits that I see that I'm supposed to count
const tallyList = digitsList.map(number => [number, 0]);
// loop over the list of integers
for(const integer of integersList) {
const integerDigitList = String(integer).split("").map(Number);
for(const digit of integerDigitList) {
// make our check
// make sure to ignore NaN
if( digitsList.includes(digit) ) {
// go into tally list, find the right array and increment to the tally spot
const tallyPair = tallyList.find(pair => pair[0] === digit);
tallyPair[1] += 1;
}
}
}
return tallyList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment