随机化而不重复多次

时间:2016-10-12 12:04:48

标签: javascript string random

我有以下任务要执行。我得到一个字符串,我必须反向写,并随机化这些字,以形成5个不同的字符串。我已经开始反向编写字符串并随机化这些单词,但我无法做到5次。这是代码。

var x = "Lorem ipsum dolor sit amet";
var wordsArray = x.split(" ");


function wordsReverse(allWords){
    var otherArray = [];
    for ( var i = allWords.length-1; i >= 0; i--) {
        otherArray.push(allWords[i]);
    }
    return otherArray;
}

function createRandomText(text){
    var randomArray = [];
    var randomText = null;
    for ( var j = 0; j < 5; j++) {
        for ( var k = 0; k < text.length; k++) {
            randomText = text[Math.floor(Math.random()*text.length)];
            if ( text[randomText] === undefined ) {
                randomArray.push(randomText);
                text[randomText] = true;
            }

        }   
    }

    return randomArray;
}

console.log(wordsReverse(wordsArray).toString().replace(/\,/g, ' '))
console.log(createRandomText(wordsArray).toString().replace(/\,/g, ' '))

感谢帮助。

1 个答案:

答案 0 :(得分:0)

循环直到你得到所有5个随机字符串。如果string不在数组循环中。

<Button Style="{StaticResource ButtonStyle}">
      <Button.Content>
            <StackPanel>
                  <TextBlock Text="Title" />
                  <TextBlock Text="{Binding SomeValue, Mode=OneWay}" />
            </StackPanel>
      </Button.Content>
</Button>

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
      <Setter Property="Template">
          <Setter.Value>
               <ControlTemplate TargetType="{x:Type Button}">
                     <Border Name="bd">
                          <ContentPresenter />
                     </Border>
                     <ControlTemplate.Triggers>
                         //HOW TO HERE...
                         <Trigger Property="Text" Value="SomeText 1">
                              <Setter TargetName="bd" Property="Background" Value="#b5e61d"/>  
                         </Trigger>
                         <Trigger Property="Text" Value="SomeText 2">
                              <Setter TargetName="bd" Property="Background" Value="#99d9ea"/>  
                         </Trigger>
                     </ControlTemplate.Triggers>
               </ControlTemplate>  
          </Setter.Value>
      </Setter>
 </Style>