【问题标题】:Not returning user array inputs不返回用户数组输入
【发布时间】:2019-08-06 23:12:56
【问题描述】:

我正在尝试在输入日期后返回什么生肖。 我有点卡在我的代码的哪一部分没有正确返回值。

我尝试了多个 alert 和 console.log 命令来返回值以及更改不同的方法和代码布局。这是代码。

var start = 1901

var chineseZodiac {

  var birthYears {

    //Creates Inputs for the date.
    var birthDate = parseInt(prompt('Enter the date of birth as an integer, ranging from 1 to 31', '31'));
    var birthMonth = parseInt(prompt('Enter the month of birth as an integer, ranging from 1 to 12', '12'));
    var birthYear = parseInt(prompt('Enter the year of birth as a 4 digit integer', '2016'));
    var birthingTime = new Date(birthDate, birthMonth, birthYear);

    //Collecting data on the current time and stores the user inputs
    var currentTime = new Date();
    currentYear = currentTime.getFullYear();
    currentMonth = currentTime.getMonth();
    currentDay = currentTime.getDate();

    //Declares Array of chineseZodiak
    var zodiacSigns = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"];
  }
  resultTest(alert(resultSaving));

  //reporting function

}


function resultTest() {
  var resultSaving = math.floor(zodiacSigns[(birthYear - start) % 12]);
  document.getElementById("myText").innerHTML = resultSaving;
}

console.log(math.floor(zodiacSigns([birthYear - start] % 12)));

【问题讨论】:

  • 这看起来不像是有效的 JavaScript。您应该在控制台中看到语法错误(实际上有几个)。如果您一直在寻找 console.log 命令的结果,您没有发现吗?

标签: javascript arrays syntax


【解决方案1】:

您的帖子不是有效的 JavaScript,有几处需要更正。

这是一个如何获得预期结果的示例。

/**
 * Put the main logic into a function.
 */
function getZodiacSign () {

  const start = 1901,
    zodiacSigns = ['Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig'];

  let birthYear = getUserBirthYear();
  const index = (birthYear - start) % 12;
  
  document.getElementById('result').innerHTML = 
    'Congratulations, you are a <strong>' + zodiacSigns[index] + '</strong> on the Chinese Zodiac';

}

/**
 * Use a prompt to get user birth year.
 *
 * @returns {number}
 */
function getUserBirthYear() {
  return parseInt(prompt('Enter the year of birth as a 4 digit integer', '2016'));
}

// Call the function
getZodiacSign();
&lt;div id="result"&gt;&lt;/div&gt;

您尝试定义变量的方式无效:

var chineseZodiac {
  ...
}

你在提示用户3次输入你以后都不用的信息,使用表单输入收集信息会好得多,而且绝对不要让用户输入你不去的信息以后用。

// Get rid of these lines
var birthDate = parseInt(prompt('Enter the date of birth as an integer, ranging from 1 to 31', '31'));
var birthMonth = parseInt(prompt('Enter the month of birth as an integer, ranging from 1 to 12', '12'));
var birthingTime = new Date(birthDate, birthMonth, birthYear);

您似乎没有在任何地方使用当前日期,您也可以摆脱这些行。

//Collecting data on the current time and stores the user inputs
var currentTime = new Date();
currentYear = currentTime.getFullYear();
currentMonth = currentTime.getMonth();
currentDay = currentTime.getDate();

你在一个字符串上调用math.floor()

math.floor(zodiacSigns[(birthYear - start) % 12]);

改为使用index计算得到对应的数组元素。

考虑用表单输入代替提示,用页面中显示的结果代替警报。

【讨论】:

  • 感谢老兄,我回顾了一遍,发现其中有一些问题。谢谢你的投入
  • @JoeyMercer 的答案是否解决了您的问题?如果是,请将其标记为accepted
【解决方案2】:

您的代码有一些错误。

你可以试试这样的

var start = 1901

var chineseZodiac = function() {
  var birthYears 
  {

    //Creates Inputs for the date.
    var birthDate = parseInt(prompt('Enter the date of birth as an integer, ranging from 1 to 31', '31'));
    var birthMonth = parseInt(prompt('Enter the month of birth as an integer, ranging from 1 to 12', '12'));
    var birthYear = parseInt(prompt('Enter the year of birth as a 4 digit integer', '2016'));
    var birthingTime = new Date(birthDate, birthMonth, birthYear);

    //Collecting data on the current time and stores the user inputs
    var currentTime = new Date();
    currentYear = currentTime.getFullYear();
    currentMonth = currentTime.getMonth();
    currentDay = currentTime.getDate();

    //Declares Array of chineseZodiak
    var zodiacSigns = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"];
  }

resultTest(birthYear)

function resultTest(birthYear)
{
  var resultSaving = zodiacSigns[Math.floor((birthYear-start) % 12)];
  console.log(resultSaving)
}

}

console.log(start)

chineseZodiac()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2019-09-26
    • 2018-05-23
    • 2019-03-12
    • 2021-10-05
    • 2020-06-15
    • 2021-03-24
    • 2022-10-12
    相关资源
    最近更新 更多