Search code examples
javascriptarraysfunctionpalindrome

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);
    });
    reader.readAsText(document.querySelector('input').files[0]);
  });
}, 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

 mom
 Racecar!
 another line

Solution

  • You need to run it over the palindromes

    const re = /[\W_]/g;
    const clean = word => word.replace(re, '');
    const palindrome = str => {
      if (str === '') return false;
      var lowRegStr = clean(str).toLowerCase(); // compare clean
      var reverseStr = lowRegStr.split('').reverse().join('');
      console.log(lowRegStr,reverseStr)
      return reverseStr === lowRegStr;
    };
    
    const frequency = s => [...s].reduce((a, c) => (a[c] = a[c] + 1 || 1) && a, {}); // https://stackoverflow.com/a/58600923/295783
    
    const process = file => {
      const sentences = file.split(/\r?\n/);
      const palindromes = sentences.map(word => clean(word).toLowerCase()).filter(palindrome); // filter after clean
      const freq = palindromes.map(palindrome => ({
        palindrome,
        freq: frequency(palindrome)
      }));
    
      const freqAll = frequency(palindromes.join(""))
    
      console.log('all sentences:', sentences);
      console.log('only palindromes:', palindromes);
      console.log(freq);
      console.log("All",freqAll);
    }
    
    // replace the test result below with your file getting
    const result = `mom
    Racecar!
    another line`
    process(result); // goes into the success of the file getting

    replace the lines starting with const result until end with

    window.addEventListener('load', function() {
      document.getElementById("myBtn").addEventListener("click", function() {
        const reader = new FileReader();
        reader.addEventListener('load', function() { process(this.result) });
        reader.readAsText(document.querySelector('input').files[0]);
      });
    });