how to call a function using the output of a prior function in javascript

stuck on this little part in JavaScript. I currently have a program that when a button is pressed it takes an input file and reads it line by line and determines which lines are palindromes and then outputs them to the console. My goal is to then take those palindromes and run the next function on them (called frequency) and output the results of the function to the console. However, I can’t figure out how to do that.

Here is my code:

window.addEventListener('load', function() {
  document.getElementById("myBtn").addEventListener("click", function() {
    var reader = new FileReader();
    reader.addEventListener('load', function() {
      const sentences = this.result.split(/\r?\n/);
      const palindromes = sentences.filter((line) => {
        return palindrome(line);
      console.log('all sentences:', sentences);
      console.log('only palindromes:', palindromes);
}, true);

function palindrome(str) {
  if (str === '')
    return false;
  var re = /[\W_]/g;
  var lowRegStr = str.toLowerCase().replace(re, '');
  var reverseStr = lowRegStr.split('').reverse().join('');
  return reverseStr === lowRegStr;

function frequency(str) {
//some code
<label for="upload">Upload file to find palindromes:</label>
<br />
<input type="file" name="upload" id="upload">
<button id="myBtn">Go!</button>

Any advice? Thanks!

edit: this is what i’m inputting

 another line

73 thoughts on “how to call a function using the output of a prior function in javascript”

Leave a Comment