「すべて同じ」「2つ以上異なる」値をeveryで判定したサンプル

3つの値を変更してみてください。

<input type="number" id="n1" value="1">
<input type="number" id="n2" value="2">
<input type="number" id="n3" value="3">
<p id="r">3つの値を変更してみてください。</p>
const r = document.getElementById('r')
const n1 = document.getElementById('n1')
const n2 = document.getElementById('n2')
const n3 = document.getElementById('n3')
const n = [n1, n2, n3]

n.forEach(el => {
  el.addEventListener('input', () => {
    const arr = [n1.value, n2.value, n3.value]
    if (arr.every(v => v === n[0].value)) {
      r.textContent = '✅ すべて同じ値です。'
    } else if (arr.every((v, i, self) => self.indexOf(v) === i)) {
      r.textContent = '❌ すべて異なる値です。'
    } else {
      r.textContent = '⚠️ 重複する値が2つ以上あります。'
    }
  })
})

元記事を表示する