【问题标题】:how to call a function using the output of a prior function in javascript如何使用javascript中先前函数的输出调用函数
【发布时间】:2021-01-27 15:01:12
【问题描述】:

停留在 JavaScript 中的这个小部分。我目前有一个程序,当按下按钮时,它会获取一个输入文件并逐行读取并确定哪些行是回文,然后将它们输出到控制台。我的目标是获取这些回文并在它们上运行下一个函数(称为频率)并将函数的结果输出到控制台。但是,我不知道该怎么做。

这是我的代码:

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>

有什么建议吗?谢谢!

编辑:这是我输入的内容

 mom
 Racecar!
 another line

【问题讨论】:

  • 在与console.log() 相同的位置致电frequency() 以获取句子和回文列表。您可以在您选择的循环中执行此操作。
  • 回文是一个数组——你需要一个循环。

标签: javascript arrays function palindrome


【解决方案1】:

你需要在回文上运行它

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

替换以const result 开头的行,直到结尾为

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]);
  });
});

【讨论】:

猜你喜欢
  • 2021-11-01
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多