Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. removing duplicates from array in order to use regex to find match. returning null

removing duplicates from array in order to use regex to find match. returning null

Scheduled Pinned Locked Moved JavaScript
regexhtmldatabasecomdata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12962889
    wrote on last edited by
    #1

    Im using this function to go through my values that are in an array to find if they equal a small straight ("1234" or "2345" or "3456"); I decided the way id get this to work is by using regular expression , which worked if I got for example "12345". However if i got duplicates of a value , example "12234" it would not work. I used the filter function to get rid of the duplicate values, which worked however when it comes to my regular expressions it keeps returning null even when my x value returns for example "1234". Im thinking there has something to do with the filter function since without it i get values returned but I need it in order to get rid of duplicate values. console[^]

    function smstraight(){
    var score=0;
    var x=dice_value.sort();
    // removes duplicates
    x=x.filter(function(item,index,inputArray){
    return x=inputArray.indexOf(item) ==index;

        }).join("");
      var r1= /\\B(1234)/g;
          var a= x.match(r1);
      var r2=/\\B(2345)/g;
          var b= x.match(r2);
      var r3=/\\B(3456)/g;
         var c= x.match(r3);
    
        console.log(x);
        if(b== "2345" || a== "1234"|| c=="3456" || x== "12345"||x=="23456"){
        	console.log("hey it works");
        		score+= 30;
    	$('.sm').html(score);
        }
    

    }

    N J 2 Replies Last reply
    0
    • U User 12962889

      Im using this function to go through my values that are in an array to find if they equal a small straight ("1234" or "2345" or "3456"); I decided the way id get this to work is by using regular expression , which worked if I got for example "12345". However if i got duplicates of a value , example "12234" it would not work. I used the filter function to get rid of the duplicate values, which worked however when it comes to my regular expressions it keeps returning null even when my x value returns for example "1234". Im thinking there has something to do with the filter function since without it i get values returned but I need it in order to get rid of duplicate values. console[^]

      function smstraight(){
      var score=0;
      var x=dice_value.sort();
      // removes duplicates
      x=x.filter(function(item,index,inputArray){
      return x=inputArray.indexOf(item) ==index;

          }).join("");
        var r1= /\\B(1234)/g;
            var a= x.match(r1);
        var r2=/\\B(2345)/g;
            var b= x.match(r2);
        var r3=/\\B(3456)/g;
           var c= x.match(r3);
      
          console.log(x);
          if(b== "2345" || a== "1234"|| c=="3456" || x== "12345"||x=="23456"){
          	console.log("hey it works");
          		score+= 30;
      	$('.sm').html(score);
          }
      

      }

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      I don't think RegEx is the right tool for this. You'd be better served by iterating the array and evaluating for unique values and building a result in the same step:

      function smstraight(){
      var score=$('.sm').html(score); //maintain score

      var source=dice_value.sort();

      var result = [source[0]]; //We're going to put the result of our processing in here

      for(var i=1; i < source.length, i++){
      if(source[i] == source[i-1] + 1) {
      result.push(source[i]); //only push result if it matches our increment condition
      }
      }

      if(result.length == 4) // We've got a small straight!
      {
      console.log("hey it works");
      $('.sm').html(score+30);
      }
      }

      Please not I just wrote this in the post, so it's not exactly tested and should be treated as conceptual.

      "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

      1 Reply Last reply
      0
      • U User 12962889

        Im using this function to go through my values that are in an array to find if they equal a small straight ("1234" or "2345" or "3456"); I decided the way id get this to work is by using regular expression , which worked if I got for example "12345". However if i got duplicates of a value , example "12234" it would not work. I used the filter function to get rid of the duplicate values, which worked however when it comes to my regular expressions it keeps returning null even when my x value returns for example "1234". Im thinking there has something to do with the filter function since without it i get values returned but I need it in order to get rid of duplicate values. console[^]

        function smstraight(){
        var score=0;
        var x=dice_value.sort();
        // removes duplicates
        x=x.filter(function(item,index,inputArray){
        return x=inputArray.indexOf(item) ==index;

            }).join("");
          var r1= /\\B(1234)/g;
              var a= x.match(r1);
          var r2=/\\B(2345)/g;
              var b= x.match(r2);
          var r3=/\\B(3456)/g;
             var c= x.match(r3);
        
            console.log(x);
            if(b== "2345" || a== "1234"|| c=="3456" || x== "12345"||x=="23456"){
            	console.log("hey it works");
            		score+= 30;
        	$('.sm').html(score);
            }
        

        }

        J Offline
        J Offline
        John C Rayan
        wrote on last edited by
        #3

        you can use a nice lamda for it rather than RegEx

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups