2015-01-28 2 views
0

무작위 모욕 발생기를 작성했습니다. 그것은 좋고, 내가 필요한 것에 잘 맞습니다. 문제는 모든 vars를 다시 복사하지 않으면 프로그램이 두 번 이상 실행될 때마다 모욕이 항상 동일하다는 것입니다. 내가 할 노력하고있어랜덤 모욕 발생기 (랜덤 화 결과)

var bodyPart = ["face", "foot", "nose", "hand", "head"]; 
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"]; 
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"]; 
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"]; 

var bodyPart = bodyPart[Math.floor(Math.random() * 5)]; 
var adjective = adjective[Math.floor(Math.random() * 4)]; 
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)]; 
var animal = animal[Math.floor(Math.random() * 5)]; 

var randomInsult = "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + "."; 

randomInsult; 
"Your nose is more insultingly stupid than a warthog's nose." 
randomInsult; 
"Your nose is more insultingly stupid than a warthog's nose." 

내가 다시 randomInsult;를 실행할 때, 나는 다른 결과를 원하는 것입니다 : 여기 내 코드입니다.

+0

:

이 방법을 호출합니다. 모든 일은 변수를 한 번 선택하여 할당하는 것입니다. 당신의 기능은 임의의 모욕을 다시 고를 것입니다. 게다가 어레이의 단일 결과를 할당 할 때 실제로 배열 변수를 덮어 쓰고 있습니다. – TommyBs

답변

0

사용하는 기능 : 내 의견으로 당

function generateRandomInsult() { 
    // ... all of your existing code ... 

    return randomInsult; 
} 

generateRandomInsult();  // this is what you repeat each time 
0

위, 당신은 함수를 사용해야합니다.

var bodyPart = ["face", "foot", "nose", "hand", "head"]; 
 
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"]; 
 
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"]; 
 
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"]; 
 

 
var randomInsult = (function() { 
 
    var bp, a, a2, an; 
 
    var bp = bodyPart[Math.floor(Math.random() * 5)]; 
 
    var a = adjective[Math.floor(Math.random() * 4)]; 
 
    var a2 = adjectiveTwo[Math.floor(Math.random() * 5)]; 
 
    var an = animal[Math.floor(Math.random() * 5)]; 
 
    var insult = "Your" + " " + bp + " " + "is more" + " " + a + " " + a2 + " " + "than a" + " " + an + "'s" + " " + bp + "."; 
 

 
    alert(insult); 
 
}); 
 

 
document.getElementById('test').addEventListener('click', randomInsult, false);
<button id="test">Click me</button>

0

각 호출에 임의의 본문, 형용사, adjectiveTwo과 동물을 선택하려면이 같은 뭔가를해야 할 것이다.

function randomInsult() { 
    var bodyPart = ["face", "foot", "nose", "hand", "head"]; 
    var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"]; 
    var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"]; 
    var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"]; 
    var bodyPart = bodyPart[Math.floor(Math.random() * 5)]; 
    var adjective = adjective[Math.floor(Math.random() * 4)]; 
    var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)]; 
    var animal = animal[Math.floor(Math.random() * 5)]; 
    return "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + "."; 
} 

코드에서 "randomInsult"문자열을 한 번만 생성합니다. 새로운 임의 값으로 각 호출에서 생성되어야합니다. 코드에 함수를 간단히 포함 시키십시오. 당신은 함수를 사용할 필요가

randomInsult();