Get your own website Result Size: 625 x 565
x
 
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The every() Method</h2>
<p>Check if every answers in the array is the same:</p>
<p id="demo"></p>
<script>
// Create an Array
const survey = [
  { name: "Steve",   answer: "Yes"},
  { name: "Jessica", answer: "Yes"},
  { name: "Peter",   answer: "Yes"},
  { name: "Elaine",  answer: "No"}
];
// Function to Run for every Element
function isSameAnswer(el, index, arr) {
  // Return true for the first element
  if (index === 0){
    return true;
  }
  else {
  // Compare the value of the previous element
    return (el.answer === arr[index - 1].answer);
  }
}
document.getElementById("demo").innerHTML = survey.every(isSameAnswer);
</script>
</body>
</html>