gpt4 book ai didi

javascript - 将 Javascript 数组中的数字与数学变量相匹配

转载 作者:行者123 更新时间:2023-12-03 12:38:33 24 4
gpt4 key购买 nike

请理解,我根本不是 Javascript 程序员,所以我陷入了我正在尝试构建的简单表单计算器的最后一步。我能够加载表单数组值,找到每个选项的最高值,但我希望它显示文本而不是值。

 //Set up an associative array 
//The keys represent the cards type
//We use this this array when the user selects a cards from the form
var cards_values = new Array();
cards_values["None"] = 0;
cards_values["Lemon"] = 5;
cards_values["Custard"] = 5;
cards_values["Fudge"] = 7;
cards_values["Mocha"] = 8;
cards_values["Raspberry"] = 10;
cards_values["Pineapple"] = 5;
cards_values["Dobash"] = 9;
cards_values["Mint"] = 5;
cards_values["Cherry"] = 5;
cards_values["Apricot"] = 8;
cards_values["Buttercream"] = 7;
cards_values["Chocolate Mousse"] = 12;



//This function finds the cards price based on the
//drop down selection
function getCardValue() {
var cardValue = 0;
//Get a reference to the form id="cardform"
var theForm = document.forms["cardform"];
//Get a reference to the select id="cards"
var selectedCard = theForm.elements["cards"];

//set Card Price equal to value user chose
//For example cards_values["Lemon".value] would be equal to 5
cardValue = cards_values[selectedCard.value];

//finally we return cardValue
return cardValue;
}

//This function finds the cards price based on the
//drop down selection
function getCardValue1() {
var cardValue1 = 0;
//Get a reference to the form id="cardform"
var theForm = document.forms["cardform"];
//Get a reference to the select id="cards"
var selectedCard = theForm.elements["cards1"];

//set Card Price equal to value user chose
//For example cards_values["Lemon".value] would be equal to 5
cardValue1 = cards_values[selectedCard.value];

//finally we return cardValue
return cardValue1;
}

//This function finds the cards price based on the
//drop down selection
function getCardValue2() {
var cardValue2 = 0;
//Get a reference to the form id="cardform"
var theForm = document.forms["cardform"];
//Get a reference to the select id="cards"
var selectedCard = theForm.elements["cards2"];

//set Card Price equal to value user chose
//For example cards_values["Lemon".value] would be equal to 5
cardValue2 = cards_values[selectedCard.value];

//finally we return cardValue
return cardValue2;
}

function calculateTotal() {
//Here we get the total price by calling our function
//Each function returns a number so by calling them we add the values they return together
var highcardvalue = Math.max(getCardValue(), getCardValue1(), getCardValue2());


//display the result
var divobj = document.getElementById('highestcard');
divobj.style.display = 'block';
divobj.innerHTML = "The highest card is " + highcardvalue;

}

function hideTotal() {
var divobj = document.getElementById('highestcard');
divobj.style.display = 'none';
}

Fiddle Demo

我不知道是否应该使用 obj 重建或者其他解决方案是否会更好。

最佳答案

您正在使用数组作为对象 - 数组采用整数键并具有长度属性。对象采用 string 键。

虽然 JavaScript 对象通常被称为关联数组,但事实并非如此。在许多方面相似,但在一些重要方面有所不同。

(数组构建在对象之上的事实意味着您正在做的事情有效,但这是不对的。)

var cards_values= {
"None": 0,
"Lemon": 5,
"Custard": 7,
"Fudge": 7
"Mocha": 8,
"Raspberry": 10,
"Pineapple": 5,
"Dobash": 9,
"Mint": 5,
"Cherry": 5,
"Apricot": 8,
"Buttercream": 7,
"Chocolate Mousse": 12
};

关于javascript - 将 Javascript 数组中的数字与数学变量相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621174/

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