运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,暗/亮
前往 Spaces
<!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>