// 1. 定义变量和函数
const customName = document.getElementById('customname');
const randomize = document.querySelector('.randomize');
const story = document.querySelector('.story');
function randomValueFromArray(array) {
return array[Math.floor(Math.random() * array.length)];
}
// 2. 纯文本字符串
var insertx=[
'Willy the Goblin',
'Big Daddy',
'Father Christmas'
];
var inserty=[
'the soup kitchen',
'Disneyland',
'the White House'
];
var insertz=[
'spontaneously combusted',
'melted into a puddle on the sidewalk',
'turned into a slug and crawled away'
];
// 3. 事件监听器和未完成的函数定义
randomize.addEventListener('click', result);
function result() {
var name= randomValueFromArray(insertx);
var place = randomValueFromArray(inserty);
var status = randomValueFromArray(insertz);
var inputName ='Bob';
var weight=600;
var temperature = 50;
var temMeter='。C';
if (customName.value !== '') {
inputName = customName.value;
}
if (document.getElementById("uk").checked) {
weight = Math.round(300);
temperature = Math.round(94);
}
if(document.getElementById('us').checked){
temMeter = '。F'
}
var storyText = ' It was '+temperature+' ' + temMeter +' outside, so '+name+' went for a walk. When they got to
'+place+', they stared in horror for a few moments, then '+status+'. '+inputName+' saw the whole thing, but was not
surprised — '+name+' weighs 300 pounds, and it was a hot day.';
story.textContent =storyText;
story.style.visibility = 'visible';
}
document.getElementById('cn').onclick = () => {
document.title = '笑话生成器';
document.getElementById('lbl-customname').textContent = '请输入自定义的名字:';
document.getElementById('lbl-cn').textContent = '中国';
document.getElementById('lbl-us').textContent = '美国';
document.getElementById('lbl-uk').textContent = '英国';
document.getElementById('customname').placeholder = '李雷';
document.querySelector('.randomize').textContent = '随机生成笑话';
console.log("customerName->"+customName);
};
document.getElementById('us').onclick =
document.getElementById('uk').onclick = () => {
document.title = 'Silly story generator';
document.getElementById('lbl-customname').textContent = 'Enter custom name:';
document.getElementById('lbl-cn').textContent = 'CN';
document.getElementById('lbl-us').textContent = 'US';
document.getElementById('lbl-uk').textContent = 'UK';
document.getElementById('customname').placeholder = 'Bob';
document.querySelector('.randomize').textContent = 'Generate random story';
};