Advertisement
Guest User

Untitled

a guest
May 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. $(function(){
  2. /*
  3. Inputs need a comma in between each value don't put quotations (line break is allowed)
  4. Example inputs
  5. A,
  6. B,
  7. C
  8. */
  9.  
  10. //When submit
  11. $(".submit-values").on('click', function(){
  12. //get the value in the textarea
  13. var todayValues = $(".today-input textarea").val();
  14. var yesterdayValues = $(".yesterday-input textarea").val();
  15.  
  16. //remove linebreaks from the inputs
  17. todayValues = todayValues.replace(/(\r\n|\n|\r)/gm,"");
  18. yesterdayValues = yesterdayValues.replace(/(\r\n|\n|\r)/gm,"");
  19.  
  20. //change the string into array
  21. var today = todayValues.split(",");
  22. var yesterday = yesterdayValues.split(",");
  23.  
  24. //find the duplicates
  25. var outputTrue = dupOutput(today, yesterday);
  26. //find ones not duplicated
  27. var outputFalse = today.filter(function(val) {
  28. return outputTrue.indexOf(val) == -1;
  29. });
  30.  
  31. //join the arrays by adding <br> then use html output
  32. outputTrue = outputTrue.join("<br>");
  33. outputFalse = outputFalse.join("<br>");
  34.  
  35. //display the outputs
  36. $(".dup .display").html(outputTrue);
  37. $(".not-dup .display").html(outputFalse);
  38.  
  39. //returns duplicates in an array
  40. function dupOutput(today, yesterday){
  41. var output = [];
  42. //loop through today
  43. for(var i = 0; i < today.length; i++){
  44.  
  45. //loop through yesterday if today matches yesterday, push into the output array
  46. for(var j = 0; j < yesterday.length; j++){
  47. if(today[i] == yesterday[j]){
  48. output.push(today[i]);
  49. }
  50. }
  51. }
  52. return output;
  53. }
  54. });
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement