Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript program has something wrong ,I don't know why the length of log is not 10?

Javascript program has something wrong ,I don’t know why the length of log is not 10?
the input s :

const s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

var findRepeatedDnaSequences = function(s) {
  var set = new Set()
  var seqSet = new Set()
  for (let i = 0; i < s.length - 11; i++) {
    let sub = s.substr(i, i + 10)
    console.log(sub)
    if (set.has(sub)) {
      seqSet.add(sub)
    } else {
      set.add(sub)
    }
  }
  return Array.from(set)
};

findRepeatedDnaSequences(s)

the console result:Javascript program has something wrong ,I don’t know why the length of log is not 10?

AAAAACCCCC
AAAACCCCCAA
AAACCCCCAAAA
AACCCCCAAAAAC
ACCCCCAAAAACCC
CCCCCAAAAACCCCC
CCCCAAAAACCCCCCA
CCCAAAAACCCCCCAAA
CCAAAAACCCCCCAAAAA
CAAAAACCCCCCAAAAAGG
AAAAACCCCCCAAAAAGGGT
AAAACCCCCCAAAAAGGGTTT
AAACCCCCCAAAAAGGGTTT
AACCCCCCAAAAAGGGTTT
ACCCCCCAAAAAGGGTTT
CCCCCCAAAAAGGGTTT
CCCCCAAAAAGGGTTT
CCCCAAAAAGGGTTT
CCCAAAAAGGGTTT
CCAAAAAGGGTTT
CAAAAAGGGTTT

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The arguments you’re giving to substr() are appropriate for substring(). Since substr() is deprecated, you should just change to substring() and you’ll get the results you want.

const s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

var findRepeatedDnaSequences = function(s) {
  var set = new Set()
  var seqSet = new Set()
  for (let i = 0; i < s.length - 11; i++) {
    let sub = s.substring(i, i + 10)
    console.log(sub)
    if (set.has(sub)) {
      seqSet.add(sub)
    } else {
      set.add(sub)
    }
  }
  return Array.from(set)
};

findRepeatedDnaSequences(s)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading