gpt4 book ai didi

javascript - 嵌套函数可以使用在父函数内的其他函数中设置值的变量吗? (Javascript)

转载 作者:行者123 更新时间:2023-12-01 01:23:58 25 4
gpt4 key购买 nike

我是一个完全的初学者,试图编写代码来滚动一系列分支 d100 表(第一个表的结果决定下一个表滚动,依此类推)。

我已经通过将其全部编写为一个函数来使其工作,但是因为我想多次循环特定的表,同时仍然引用前一个表的结果,所以我想将其作为一个一系列嵌套函数。

我遇到的问题是,如果我将作为一个函数工作的相同代码嵌套到父函数中的函数中,编辑:yearResults 现在被写入HTML,但 monthResults 返回为未定义

有什么办法可以让我完成这项工作吗?这是试图实现这一目标的愚蠢方法吗?感谢您的帮助。

function fullRoll() {

var yearRoll
var yearResults

var monthRoll
var monthResults

// Rolls 1d100 on Yearly Events Table
function annualTable() {
var yearRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (yearRoll >= 1 && yearRoll <= 10) {
yearResults = "Ambassador";
} else if (yearRoll >= 11 && yearRoll <= 15) {
yearResults = "Assassination";
} else if (yearRoll >=16 && yearRoll <= 30) {
yearResults = "Birth";
} else if (yearRoll == 31) {
yearResults = "Comet";
} else if (yearRoll >=32 && yearRoll <=40) {
yearResults = "Death";
} else if (yearRoll >=41 && yearRoll <= 43) {
yearResults = "Earthquake";
} else if (yearRoll >=44 && yearRoll <= 47) {
yearResults = "Famine";
} else if (yearRoll >=48 && yearRoll <= 52) {
yearResults = "Fire";
} else if (yearRoll >=53 && yearRoll <= 57) {
yearResults = "Flood";
} else if (yearRoll == 58) {
yearResults = "Incursion";
} else if (yearRoll >= 59 && yearRoll <= 68) {
yearResults = "Marriage";
} else if (yearRoll == 69) {
yearResults = "Religion";
} else if (yearRoll >= 70 && yearRoll <= 74) {
yearResults = "Plague";
} else if (yearRoll >= 75 && yearRoll <= 78) {
yearResults = "Plot";
} else if (yearRoll >= 79 && yearRoll <= 87) {
yearResults = "Rebellion";
} else if (yearRoll == 88) {
yearResults = "Visitation";
} else if (yearRoll >= 89 && yearRoll <= 100) {
yearResults ="War";
}
}

// Determines the Results of a Roll on the Monthly Event Table
function monthlyTable() {
//Rolls on Monthly Table for Manmade Disasterss
if (yearResults == "Assassination" || yearResults == "Incursion" || yearResults == "Plot" || yearResults == "Rebellion" || yearResults == "War") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
if (monthRoll >= 1 && monthRoll <= 5) {
var monthResults = "Accident1";
} else if (monthRoll >= 6 && monthRoll <= 10) {
monthResults = "Bandits1";
} else if (monthRoll >=11 && monthRoll <= 15) {
monthResults = "Birth1";
} else if (monthRoll >= 16 && monthRoll <=20) {
monthResults = "Death1";
} else if (monthRoll >= 21 && monthRoll <=25) {
monthResults = "Taxes1";
} else if (monthRoll >=26 && monthRoll <=27) {
monthResults = "Fame1";
} else if (monthRoll == 28) {
monthResults = "Fire, Minor1";
} else if (monthRoll >= 29 && monthRoll <= 30) {
monthResults = "Haunting1";
} else if (monthRoll >=31 && monthRoll <=33) {
monthResults = "Monster1";
} else if (monthRoll >= 34 && monthRoll <=41) {
monthResults = "Incursion, Major1";
} else if (monthRoll >= 42 && monthRoll <= 51) {
monthResults = "Incursion, Minor1";
} else if (monthRoll >=52 && monthRoll <= 54) {
monthResults = "Injustice1";
} else if (monthRoll >= 55 && monthRoll <= 60) {
monthResults = "Battle, Major1";
} else if (monthRoll == 61) {
monthResults = "Criminal1";
} else if (monthRoll >=62 && monthRoll <= 66) {
monthResults = "Recruiting1";
} else if (monthRoll >=67 && monthRoll <=71) {
monthResults = "Troop Movements1";
} else if (monthRoll >=72 && monthRoll <= 78) {
monthResults = "Uprising1";
} else if (monthRoll >=79 && monthRoll <= 80) {
monthResults = "Vengeful Stranger1";
} else if (monthRoll >=83 && monthRoll <=100) {
monthResults = "Nothing1";
}

//Rolls on Table for Natural Disasters
} else if (yearResults == "Comet" || yearResults == "Earthquake" || yearResults == "Famine" || yearResults == "Fire" || yearResults == "Flood" || yearResults == "Plague") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (monthRoll >= 1 && monthRoll <= 5) {
monthResults = "Accident2";
} else if (monthRoll >= 6 && monthRoll <= 15) {
monthResults = "Bad Harvest2";
} else if (monthRoll >= 16 && monthRoll <= 22) {
monthResults = "Bad Weather2";
} else if (monthRoll >= 23 && monthRoll <= 32) {
monthResults = "Bandit Activity2";
} else if (monthRoll >= 33 && monthRoll <= 37) {
monthResults = "Birth2";
} else if (monthRoll >= 38 && monthRoll <= 44) {
monthResults = "Death2";
} else if (monthRoll >= 45 && monthRoll <= 49 ) {
monthResults = "Earthquake, Minor2";
} else if (monthRoll >= 50 && monthRoll <= 54) {
monthResults = "Taxes2";
} else if (monthRoll >= 55 && monthRoll <= 60) {
monthResults = "Fire, Minor2";
} else if (monthRoll >= 61 && monthRoll <= 66) {
monthResults = "Flooding2";
} else if (monthRoll >= 67 && monthRoll <= 69) {
monthResults = "Haunting2";
} else if (monthRoll >= 70 && monthRoll <= 73) {
monthResults = "Monster2";
} else if (monthRoll >= 74 && monthRoll <= 78) {
monthResults = "Injustice2";
} else if (monthRoll >= 79 && monthRoll <= 82) {
monthResults = "Landslide2";
} else if (monthRoll >= 83 && monthRoll <= 87) {
monthResults = "Maneater2";
} else if (monthRoll >= 88 && monthRoll <= 92) {
monthResults = "Plague2";
} else if (monthRoll == 93) {
monthResults = "Uprising2";
} else if (monthRoll == 94) {
monthResults = "VIP2";
} else if (monthRoll >= 95 && monthRoll <=100) {
monthResults = "Nothing2";
}

//Rolls on Table for Other Disasters
} else if (yearResults == "Ambassador" || yearResults == "Birth" | yearResults == "Death" || yearResults == "Marriage" || yearResults == "Visitation") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (monthRoll >= 1 && monthRoll <= 5) {
var monthResults = "Accident3";
} else if (monthRoll >= 6 && monthRoll <= 7) {
monthResults = "Bad Harvest3";
} else if (monthRoll >= 8 && monthRoll <= 10) {
monthResults = "Bad Weather3";
} else if (monthRoll >= 11 && monthRoll <= 15) {
monthResults = "Bandit Activity3";
} else if (monthRoll >= 16 && monthRoll <= 25) {
monthResults = "Birth3";
} else if (monthRoll >= 26 && monthRoll <= 30) {
monthResults = "Death3";
} else if (monthRoll >= 31 && monthRoll <= 35) {
monthResults = "Famous Person3";
} else if (monthRoll >= 36 && monthRoll <= 40) {
monthResults = "Fire, Minor3";
} else if (monthRoll >= 41 && monthRoll <= 43) {
monthResults = "Haunting3";
} else if (monthRoll >= 44 && monthRoll <= 50) {
monthResults = "Injustice3";
} else if (monthRoll >= 51 && monthRoll <= 55) {
monthResults = "Work of Art3";
} else if (monthRoll >= 56 && monthRoll <= 60) {
monthResults = "Maneater3";
} else if (monthRoll >= 61 && monthRoll <= 68) {
monthResults = "Marriage3";
} else if (monthRoll >= 69 && monthRoll <= 78) {
monthResults = "Criminal3";
} else if (monthRoll >= 79 && monthRoll <= 83) {
monthResults = "Vengeful Stranger3";
} else if (monthRoll >= 84 && monthRoll <= 92) {
monthResults = "VIP Visit3";
} else if (monthRoll >= 93 && monthRoll <= 100) {
monthResults = "Nothing3";
}

}
}

annualTable();
monthlyTable();
document.getElementById('annual').innerHTML = yearResults;
document.getElementById('month1').innerHTML = monthResults;

}

最佳答案

您的条件错误:

if (yearRoll >= 1 && <= 50) {

应该是:

if (yearRoll >= 1 && yearRoll <= 50) {

请记住对代码中的所有范围检查执行此操作。

<小时/>

此外,考虑到代码中的注释是正确的 ( random number between 1 and 100 ),您可以继续删除 >=1 。和<=100全部检查。

关于javascript - 嵌套函数可以使用在父函数内的其他函数中设置值的变量吗? (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com