part1.js 661 B

12345678910111213141516171819202122232425262728
  1. const fs = require('fs')
  2. let data = JSON.parse(fs.readFileSync('./Day 2/data.json'))
  3. let invalid = []
  4. function find_occurences(str, char_to_count) {
  5. return str.split(char_to_count).length - 1;
  6. }
  7. data.forEach(element => {
  8. let info = element[0].split(' ')
  9. let info2 = info[0].split('-')
  10. let count = find_occurences(element[1], info[1])
  11. if (count >= info2[0]) {
  12. if (count <= info2[1]) {
  13. console.log(element)
  14. } else {
  15. invalid.push(element)
  16. }
  17. } else {
  18. invalid.push(element)
  19. }
  20. console.log(element)
  21. });
  22. let answer = data.length - invalid.length
  23. console.log('Valid ' + answer)