Hey. I have an array of words in the file, the user specifies the required number, and the specified number of words from the file are displayed in a random order:

<?php $src = file("file.txt"); $n = htmlspecialchars($_GET["words"]); $words = array_rand($src, $n); $result = array_map(function($key) use ($src) { return $src[$key]; }, $words); echo "<div class='word'>" . implode("<hr></div><div class='word'>", $result) . "</div>" ?> 

Displayed as:

 <div class="results center"> <div class="word">витраж<hr></div> <div class="word">лапоть<hr></div> <div class="word">водопад<hr></div> <div class="word">пробка<hr></div> </div> 

There was a need to check the words that the user remembered. When you click on a button, input'y are added instead of words, where the user enters values ​​(I’m supposed to understand how to do this). The question is how then to compare the array that was displayed to the user, with the values ​​that he entered and ideally output the result with errors.

  • To calculate the convergence of arrays, there is array_intersect to calculate the difference of array_diff. Your main task is to transfer both arrays to the server side. - fens
  • And how does the user 'remember' a collection of words? - ddeadlink
  • @ddeadlink this is supposed to be an exercise for training the memory :) and now I am trying to understand how to implement the ability to test - Alex Bub
  • Do you need to compare outgoing data with the data that the user enters after entering the input? - ddeadlink
  • @ddeadlink Yes, while the source data is randomly output from the file each time. Not enough experience to figure out how to do it correctly - Alex Bub

1 answer 1

You can try to save the original data when loading the page in localstorage or session (if you need to save data on other pages / after reloads), at your choice, or as an array / object into a variable in case of exclusion of the above mentioned mechanisms.

  var initial = document.querySelectorAll('.word'), toCompare = []; initial.forEach(function(node){ toCompare.push(node.innerHTML.split('<')[0]); }) 

Next, you can make an event handler on the change in the newly created inputs and by the index of this very input you can compare with what you previously received

  • Thanks for the tip, I will try! - Alex Bub