Skip to content Skip to sidebar Skip to footer

Js Function For Writing Out A Word, Binary Counter Style

How can I create a loop to output any given word like this: ...if the word is 'abcd' a--- -b-- ab-- --c- a-c- -bc- abc- ---d a--d -b-d ab-d --cd a-cd -bcd abcd in other words, let

Solution 1:

var i = 1;
console.log("abcd".replace(/./g, function(c,n) { 
   return (i & (1 << n)) ? c : '-';
}));

Just loop i from 1 to (2 ^ length) -1

Solution 2:

Looks like you actually want the reverse of binary counting; i.e. for binary it would be

000100100011

but you're flipping it. Which is fine. The following code counts from 0 to the necessary number (16 for a four-letter word), gets the binary representation for each number, reverses it, and outputs the letters of your word for those places where there's a one in the binary representation.

functionletterScroller(str) {
   var iterations = Math.pow(2,str.length);//iterate from 0 to 2**wordlengthvar result = "";
   for (i=0;i<iterations;i++) {
       //get the binary representation, pad it, and reverse itvar bin = reverse(pad(i.toString(2), str.length));

       //loop through binary, adding character of word where there's a 1for (j=0;j<str.length;j++) {
           if (bin.charAt(j)=="1") {
               result += str.charAt(j)
           } else {
               result += "-";
           }
       }
       result += "<br />"; //separate lines with HTML line break
   }
   return result;
}

functionpad(str, length) {
    while (str.length < length) {
       str="0" + str;
    }
    return str;
}

functionreverse (str) {
  return str.split("").reverse().join("");
}

Solution 3:

function doStuff(word) {
    var wordLength = word.length, num = 1 << wordLength;
    var i, bit, wordChars = word.split(""), chars;
    for (var i = 1; i < num; ++i) {
        chars = [];
        for (bit = 0; bit < wordLength; ++bit) {
            chars.push( i & 1 << bit ? wordChars[bit] : "-" );
        }
        console.log( chars.join("") );
    }
}

doStuff("abcd");

Solution 4:

with KennyTM's code it can be done quite easily

String.prototype.reverse = function(){
splitext = this.split("");
revertext = splitext.reverse();
reversed = revertext.join("");
return reversed;
}

str='abcd'; //your stringfor(var i=0;i<20;i++){ //a loop of numbers  var result='';

  var ii=i.toString(2).reverse();
  for(var q=0;q<Math.max(ii.length,str.length);q++){
    if(ii.charAt(q)=='1'){
      result+=str.charAt(q);
    }else{
      result+='-';
      }
  }      

 document.write(result+'<br>');
}

that was fun :) What do You need that for?

Solution 5:

Here's the short version:

varstring = 'abcd';
for (i = 1; i < Math.pow(string.length,2); i++) {
    str = "";
    for (j = 0; j < string.length; j++) {
        if (i & (1 << j)) 
            str += string[j];
        else
           str += "-";
    }
 console.log(str);
}

Enjoy!

Post a Comment for "Js Function For Writing Out A Word, Binary Counter Style"