2012-03-21 37 views
0

我不確定它叫什麼,但畢竟數學完成後它會返回一個介於0和11之間的值,我需要將該數字匹配到與它們的位置對應的zodAnimal和zodAnimalD(所以0會返回鼠標,和第一個描述等)。將整數與數組匹配以獲取值?

如何做到這一點?:

var birthYear = parseInt(prompt ('Enter your birth year:')); 
var birthMonth = prompt ('Enter the name of the month of birth:'); 
var birthDay = parseInt(prompt ('Enter your day of birth as an integer:')); 
var milliDay = 1000*60*60*24; //Milliseconds in a day 
monthAbb = 'janfebmaraprmayjunjulaugsepoctnovdec'; 
chineseZod = 12; 
zodCycle = 1924; //Chinese Zodiac Cycle 
var zodAnimal = new Array('Rat','Ox','Tiger','Rabbit','Dragon','Snake','Horse','Goat','Monkey','Rooster','Dog','Pig'); 
var zodAnimalD = new Array('Forthright, tenacious, intense, meticulous, charismatic, sensitive, intellectual, industrious, charming, eloquent, sociable, artistic, and shrewd. Can be manipulative, vindictive, self-destructive, envious, mendacious, venal, obstinate, critical, over-ambitious, ruthless, intolerant, and scheming.','Dependable, ambitious, calm, methodical, born leader, patient, hardworking, conventional, steady, modest, logical, resolute, and tenacious. Can be stubborn, dogmatic, hot-tempered, narrow-minded, materialistic, rigid, and demanding.','Unpredictable, rebellious, colorful, powerful, passionate, daring, impulsive, vigorous, stimulating, sincere, affectionate, humanitarian, and generous. Can be restless, reckless, impatient, quick-tempered, obstinate, selfish, aggressive, and moody.','Gracious, good friend, kind, sensitive, soft-spoken, amiable, elegant, reserved, cautious, artistic, thorough, tender, self-assured, shy, astute, compassionate, lucky, and flexible. Can be moody, detached, superficial, self-indulgent, opportunistic, and stubborn.','Magnanimous, stately, vigorous, strong, self-assured, proud, noble, direct, dignified, eccentric, intellectual, fiery, passionate, decisive, pioneering, artistic, generous, and loyal. Can be tactless, arrogant, imperious, tyrannical, demanding, intolerant, dogmatic, violent, impetuous, and brash.','Deep thinker, wise, mystic, graceful, soft-spoken, sensual, creative, prudent, shrewd, elegant, cautious, responsible, calm, strong, constant, and purposeful. Can be a loner, bad communicator, possessive, hedonistic, self-doubting, distrustful, mendacious, suffocating, and cold.','Cheerful, popular, quick-witted, changeable, earthy, perceptive, talkative, agile, magnetic, intelligent, astute, flexible, and open-minded. Can be fickle, arrogant, childish, anxious, rude, gullible, and stubborn.','Righteous, sincere, sympathetic, mild-mannered, observant, artistic, intellectual, ingenious, innovative, creative, mothering, peaceful, and generous. Can be indecisive, over-passive, worrier, pessimistic, sensitive, shy, and weak-willed.','Inventor, motivator, improviser, quick-witted, inquisitive, flexible, innovative, problem solver, self-assured, sociable, artistic, polite, dignified, competitive, objective, and factual. Can be egotistical, vain, arrogant, selfish, reckless, snobbish, deceptive, manipulative, cunning, jealous, suspicious, and stubborn.',' Acute, neat, meticulous, organized, self-assured, decisive, conservative, critical, perfectionist, alert, zealous, practical, scientific, and responsible. Can be over zealous and critical, puritanical, egotistical, abrasive, proud, opinionated, and gives into empty bravado.','Honest, intelligent, straightforward, loyal, sense of justice and fair play, attractive, amicable, unpretentious, sociable, open-minded, idealistic, moralistic, practical, affectionate, sensitive, and easy going. Can be cynical, lazy, cold, judgmental, pessimistic, worrier, stubborn, and quarrelsome.','Honest, gallant, sturdy, sociable, peace-loving, patient, loyal, hard-working, trusting, sincere, calm, understanding, thoughtful, scrupulous, passionate, and intelligent. Can be naive, over-reliant, self-indulgent, gullible, fatalistic, and materialistic.'); 

var monthArr = new Array(11); 

monthArr [0] = "jan"; 
monthArr [1] = "feb"; 
monthArr [2] = "mar"; 
monthArr [3] = "apr"; 
monthArr [4] = "may"; 
monthArr [5] = "jun"; 
monthArr [6] = "jul"; 
monthArr [7] = "aug"; 
monthArr [8] = "sep"; 
monthArr [9] = "oct"; 
monthArr [10] = "nov"; 
monthArr [11] = "dec"; 

var monthNum = monthAbb.indexOf(birthMonth.slice(0, 3).toLowerCase())/3; 
alert(monthNum); 
var d = new Date (birthYear, monthNum, birthDay); 
alert(d); 
var dCurrent = new Date(); 
dCurrent = dCurrent.getTime(); //Grabs the time of the current date in milliseconds. 
d = d.getTime(); //Grabs the time of the user-entered date in milliseconds. 
var dTotal = dCurrent - d; 
alert(dTotal); 
dTotal = dTotal/milliDay; 
dTotal = Math.floor(dTotal); //7193 
alert(dTotal + ' is after division'); 
dTotal = dTotal/365.25; 
dTotal = Math.floor(dTotal); 
alert(dTotal + ' is how old you are!'); 
dTotal = birthYear - zodCycle; 
dTotal = dTotal % chineseZod; 
alert(dTotal); 
+0

zodAnimal [0]將返回「老鼠」..這是你在找什麼? – 2012-03-21 07:00:08

+0

我想寫入頁面相應的條目,以將它們放入提示中。 – Chara 2012-03-21 07:02:58

回答

0

它只是zodAnimal[dTotal]zodAnimalD[dTotal]

alert(zodAnimal[dTotal]); 
0

你的意思是:


console.log(zodAnimal[dTotal]); 
console.log(zodAnimalD[dTotal]); 
+0

我需要使用document.write,但我認爲你寫的可能是我正在尋找的東西,我只是不能使用console.log。 – Chara 2012-03-21 07:03:46

0

如果你想將它寫入頁,有兩種方式:

  1. 你可以做。但是,這是一種非常粗糙的方式,它會擦除​​文檔中的所有內容並寫入您想要的內容。

  2. 的另一種方法是創建一個HTML元素:

現在,你只需要填寫<div>與數據zodAnimal,像這樣:

var placeholder = document.getElementById("zodText"); 
placeholder.innerHTML = zodAnimal[dTotal]; 
0

以下是我可能這樣做的一種方式。我會避免像1000 * 60 * 60 * 24這樣的開始,因爲這是一個常數(一天中的秒數不會改變,所以不要計算它)。

<html> 
<body> 

<label for='year'>Year:</label><input type='text' id='year' maxlength='4' size='4' /> 
<label for='month'>Month:</label><input type='text' id='month' maxlength='2' size='2'/> 
<label for='day'>Day:</label><input type='text' id='day' maxlength='2' size='2' /> 
<input type='button' onclick='calc()' value='Calculate' /> 
<div id='result'></div> 

<script type="text/javascript"> 
function val(el) { 
    return document.getElementById(el).value; 
} 

function calc() { 
     var data = [ 
      ['Rat','Forthright, tenacious, intense, meticulous, charismatic, sensitive, intellectual, industrious, charming, eloquent, sociable, artistic, and shrewd. Can be manipulative, vindictive, self-destructive, envious, mendacious, venal, obstinate, critical, over-ambitious, ruthless, intolerant, and scheming.'], 
      ['Ox','Dependable, ambitious, calm, methodical, born leader, patient, hardworking, conventional, steady, modest, logical, resolute, and tenacious. Can be stubborn, dogmatic, hot-tempered, narrow-minded, materialistic, rigid, and demanding.'], 
      ['Tiger','Unpredictable, rebellious, colorful, powerful, passionate, daring, impulsive, vigorous, stimulating, sincere, affectionate, humanitarian, and generous. Can be restless, reckless, impatient, quick-tempered, obstinate, selfish, aggressive, and moody.'], 
      ['Rabbit','Gracious, good friend, kind, sensitive, soft-spoken, amiable, elegant, reserved, cautious, artistic, thorough, tender, self-assured, shy, astute, compassionate, lucky, and flexible. Can be moody, detached, superficial, self-indulgent, opportunistic, and stubborn.'], 
      ['Dragon','Magnanimous, stately, vigorous, strong, self-assured, proud, noble, direct, dignified, eccentric, intellectual, fiery, passionate, decisive, pioneering, artistic, generous, and loyal. Can be tactless, arrogant, imperious, tyrannical, demanding, intolerant, dogmatic, violent, impetuous, and brash.'], 
      ['Snake','Deep thinker, wise, mystic, graceful, soft-spoken, sensual, creative, prudent, shrewd, elegant, cautious, responsible, calm, strong, constant, and purposeful. Can be a loner, bad communicator, possessive, hedonistic, self-doubting, distrustful, mendacious, suffocating, and cold.'], 
      ['Horse','Cheerful, popular, quick-witted, changeable, earthy, perceptive, talkative, agile, magnetic, intelligent, astute, flexible, and open-minded. Can be fickle, arrogant, childish, anxious, rude, gullible, and stubborn.'], 
      ['Goat','Righteous, sincere, sympathetic, mild-mannered, observant, artistic, intellectual, ingenious, innovative, creative, mothering, peaceful, and generous. Can be indecisive, over-passive, worrier, pessimistic, sensitive, shy, and weak-willed.'], 
      ['Monkey','Inventor, motivator, improviser, quick-witted, inquisitive, flexible, innovative, problem solver, self-assured, sociable, artistic, polite, dignified, competitive, objective, and factual. Can be egotistical, vain, arrogant, selfish, reckless, snobbish, deceptive, manipulative, cunning, jealous, suspicious, and stubborn.'], 
      ['Rooster','Acute, neat, meticulous, organized, self-assured, decisive, conservative, critical, perfectionist, alert, zealous, practical, scientific, and responsible. Can be over zealous and critical, puritanical, egotistical, abrasive, proud, opinionated, and gives into empty bravado.'], 
      ['Dog','Honest, intelligent, straightforward, loyal, sense of justice and fair play, attractive, amicable, unpretentious, sociable, open-minded, idealistic, moralistic, practical, affectionate, sensitive, and easy going. Can be cynical, lazy, cold, judgmental, pessimistic, worrier, stubborn, and quarrelsome.'], 
      ['Pig','Honest, gallant, sturdy, sociable, peace-loving, patient, loyal, hard-working, trusting, sincere, calm, understanding, thoughtful, scrupulous, passionate, and intelligent. Can be naive, over-reliant, self-indulgent, gullible, fatalistic, and materialistic.'] 
     ]; 
     var y = parseInt(val("year"), 10), 
      m = parseInt(val("month"), 10), 
      d = parseInt(val("day"), 10), 
      zod_offset = (y - 1924) % 12, 
      n = new Date().getTime(); 
      t = new Date(y,m,d).getTime(); 
      age_in_ms = Math.floor(Math.floor((n-t)/86400000)/365.25); 
     document.getElementById("result").innerHTML = "As a " + age_in_ms + " year old " + data[zod_offset][0] + ", you are " + data[zod_offset][1] + "</p>"; 

} 
</script> 
</body></html>