Skip to content

Instantly share code, notes, and snippets.

@et4891
Created May 29, 2016 01:54
Show Gist options
  • Save et4891/0a7d4ee21819db0043f1e411d1bba6aa to your computer and use it in GitHub Desktop.
Save et4891/0a7d4ee21819db0043f1e411d1bba6aa to your computer and use it in GitHub Desktop.
Input two lists (using comma to separate the list) and compare 1st and 2nd list. Outputs any duplicates / not duplicates of 1st from 2nd
$(function(){
/*
Inputs need a comma in between each value don't put quotations (line break is allowed)
Example inputs
A,
B,
C
*/
//When submit
$(".submit-values").on('click', function(){
//get the value in the textarea
var todayValues = $(".today-input textarea").val();
var yesterdayValues = $(".yesterday-input textarea").val();
//remove linebreaks from the inputs
todayValues = todayValues.replace(/(\r\n|\n|\r)/gm,"");
yesterdayValues = yesterdayValues.replace(/(\r\n|\n|\r)/gm,"");
//change the string into array
var today = todayValues.split(",");
var yesterday = yesterdayValues.split(",");
//find the duplicates
var outputTrue = dupOutput(today, yesterday);
//find ones not duplicated
var outputFalse = today.filter(function(val) {
return outputTrue.indexOf(val) == -1;
});
//join the arrays by adding <br> then use html output
outputTrue = outputTrue.join("<br>");
outputFalse = outputFalse.join("<br>");
//display the outputs
$(".dup .display").html(outputTrue);
$(".not-dup .display").html(outputFalse);
//returns duplicates in an array
function dupOutput(today, yesterday){
var output = [];
//loop through today
for(var i = 0; i < today.length; i++){
//loop through yesterday if today matches yesterday, push into the output array
for(var j = 0; j < yesterday.length; j++){
if(today[i] == yesterday[j]){
output.push(today[i]);
}
}
}
return output;
}
});
});
<!DOCTYPE html>
<html>
<head>
<title>Duplicates</title>
<style type="text/css">
div.inputs div{
display: inline-block;
}
</style>
</head>
<body>
<!--
Example:
input1 = a,b,c,d,e
input2 = d,c,f,e,g
dupOutput =
c
d
e
notDupOutput =
a
b
-->
<!-- textarea inputs -->
<div class="inputs">
<div class="today-input">
<h2>Today</h2>
<textarea rows="20"></textarea>
</div>
<div class="yesterday-input">
<h2>Yesterday</h2>
<textarea rows="20"></textarea>
</div>
</div>
<button class="submit-values">
Submit
</button>
<!-- end textarea inputs -->
<!-- display duplicates -->
<div class="dup">
<h3>Duplicates</h3>
<div class="display"></div>
</div>
<hr>
<!-- display not duplicates -->
<div class="not-dup">
<h3>Not Duplicates</h3>
<div class="display"></div>
</div>
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/dup.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment