动态任期的推荐书

时间:2013-10-07 16:45:36

标签: javascript html arrays random

我正在尝试将两个数组合并为一个(不是concat())并且我正在寻找有关最佳实践和建议的建议。目标是创建一个脚本(仅限javascript,而不是jQuery),从第一个数组中存储的证词列表中编写随机员工证词,然后通过查看第二个数组,动态地写出他们与公司在一起的年数。数组,找到他们的雇用日期(仅年份),并从当前年份减去。

这是我的第一个阵列:

<script>
  var r_text = new Array ();
  r_text[0] = "John Smith - Customer service - Year 5! 'The thing I love the most about company x is the people, the way the company rallies around an employee in need is truly amazing and something I have never seen before.  Company x truly has a family feel and the approachability of both management and supervisors makes for a great place to work'";
  r_text[1] = "Jane Smith - Back office - Year 9! 'The pay is above average and we have benefits, like a great health plan, paid sick days, and paid holidays.  The people are friendly and we help each other.  Supervisors are there to help you, one on one, so you can do your job efficiently.' ";
  r_text[2] = "Jane Doe - Trainer - Year 7! 'I feel lucky because I can actually say I love coming to work everyday. The atmosphere here is professional - yet personal at the same time. We are a business, yes, but we are also a family.  Our dedicated, approachable leadership team is always right behind you to help you achieve your goals.  We  come together in times of need whether it be for a local cause or to help an employee in need and it really makes me proud to say I work here and have for so many years.'";
    var i = Math.floor(3*Math.random())
    document.write(r_text[i]);
</script>

正如您所看到的,名称和年份已经在第一个脚本中。这就是我想要摆脱的。我希望这个脚本能够自给自足,不需要任何帮助,所以我希望在没有我或其他任何人进入并修改字符串的情况下更新年份。

好的,现在第二个数组是我遇到第一个问题的地方

我有这个简单的功能,今年可以从存储在变量中的设定年份减去它以获得终身职位:

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Get tenure</button>

<script>
function myFunction()
{
var d = new Date();
var h = 2003 //hire date
var x = document.getElementById("demo");
document.write(d.getFullYear()-h + " years");
}
</script>

</body>
</html>

好吧所以我知道我需要重新编写这个简单的函数来存储每个人在数组中的任期。我试过了:

<script>
  var hireYear = new Array ();
  hireYear[0] = 2008; //John Smith
  hireYear[1] = 2004; //Jane Smith
  hireYear[2] = 2006; //Jane Doe
   var d = new Date();
   var h = hireYear[i];
     for (var i=0;i<hireYear.length;i++)
     { 
     document.write(d.getFullYear() - hireYear[i] + " Years. <br />");
     }                      
</script>

打印出来:

5 Years. 
9 Years. 
7 Years. 

但我的困境是如何让这两个数组现在互相交流?我想,因为第一个数组使用Math.random,第二个数组将是子数组,所以我应该将r_text[0] = "John Smith - Customer service - Year 5!更改为r_text[0] = "John Smith - Customer service - hireYear[0],但除此之外,我已经丢失了。我已经尝试过搜索两个阵列一起工作的类似示例,但我能找到的只有concat()个例子,这似乎并不适用于我的需求。

注意:我不能在jQuery中执行此操作,必须在JavaScript中完成。我知道我不应该使用document.write所以如果有人有更好的建议,欢迎他们。

非常感谢您提供的任何提示,建议或批评!

1 个答案:

答案 0 :(得分:1)

您可以将年份与数组元素本身联系起来,而不是保留2个单独的数组。您可以将一个对象存储在一个数组元素中,其中一个属性text持有推荐,但实际年份为YYYY占位符,第二个属性为yearHired,当该人被雇用时为:< / p>

var r_text = [];
r_text[0] = {text: "John Smith - Customer service - Year YYYY! 'The thing I love the most about company x is the people, the way the company rallies around an employee in need is truly amazing and something I have never seen before.  Company x truly has a family feel and the approachability of both management and supervisors makes for a great place to work'",
             yearHired: 2008};

r_text[1] = {text: "Jane Smith - Back office - Year YYYY! 'The pay is above average and we have benefits, like a great health plan, paid sick days, and paid holidays.  The people are friendly and we help each other.  Supervisors are there to help you, one on one, so you can do your job efficiently.' ",
             yearHired: 2004}

r_text[2] = {text: "Jane Doe - Trainer - Year YYYY! 'I feel lucky because I can actually say I love coming to work everyday. The atmosphere here is professional - yet personal at the same time. We are a business, yes, but we are also a family.  Our dedicated, approachable leadership team is always right behind you to help you achieve your goals.  We  come together in times of need whether it be for a local cause or to help an employee in need and it really makes me proud to say I work here and have for so many years.'",
             yearHired: 2006}

这样你只需从数组中选择一个元素,计算服务年份并用结果替换YYYY占位符:

var i = Math.floor(r_text.length * Math.random());
var d = new Date();
var s = r_text[i].text.replace('YYYY', d.getFullYear() - r_text[i].yearHired);
document.write(s);

以下是演示:http://jsfiddle.net/hrz4g/2/

而且你是正确的,document.write不是显示数据的最佳选择。更好的方法是使用占位符,如DIV,并使用innerHTML属性在其中显示数据。

这是一个更新的演示,展示了这种方法:http://jsfiddle.net/hrz4g/3/