It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
One more stupid question, but why this isn't working?

function laske(){

var maara = document.getElementById("maara").value;

if (document.getElementById("luokka1").checked){
var hinta = 140,61;
}
if (document.getElementById("luokka2").checked){
hinta = 86,10;
}
else if (document.getElementById("alennus").checked){
hinta = hinta * 0,5;

if (document.getElementById("paluu").checked){
hinta = hinta*2;
}
document.getElementById("loppuhinta").innerHTML = hinta * maara;
}
Are you sure you wanted commas in numbers instead of dots?
avatar
KneeTheCap: One more stupid question, but why this isn't working?

function laske(){

var maara = document.getElementById("maara").value;

if (document.getElementById("luokka1").checked){
var hinta = 140,61;
}
if (document.getElementById("luokka2").checked){
hinta = 86,10;
}
else if (document.getElementById("alennus").checked){
hinta = hinta * 0,5;

if (document.getElementById("paluu").checked){
hinta = hinta*2;
}
document.getElementById("loppuhinta").innerHTML = hinta * maara;
}
Could be several things. The best way to debug your program is to make sure you look at the error messages your debugger gives you. In this case, since you are running this in a browser, hit F12 and take a look at the console window. It will most likely tell you what the problem is.

One potential version is, as tinysalamander pointed out, your use of comma as a decimal separator. Even if your OS is running a locale where this is standard, a programming language is most likely not affected by that locale and is expecting a period as opposed to a comma. See if changing this fixes it and if not post the error you are getting.

Another best programming practice is to use English for variable names. I work with a lot of developers from different countries and all agree that using their native languages in code creates chaos and bugs. One reason is that in doing so you are mixing languages and making the code unreadable to anyone who doesn't speak your language. T This is because function and variable names need to be meaningful in order to help make sense of and debug the code. Nothing is wrong with Finnish, I very much enjoy listening to Finnish songs, but in this case the international convention is to stick with a single language - the one which the commands of the programming language are in.
commas were the culprit here, changed them and now it works. This assignment is almost done, only one thing left. How do I make a checkbox that affects only one radio input?

I mean, there's two radio checkboxes and they have different values (ticket prices) assigned to them. Now I need to make a checkbox that acts as a discount and affects only to one price. How do I make it that the discount is added only when the two correct checkboxes are selected?

As for Finnish, I agree but the teacher requires us to use it. I have no idea why.
Post edited September 24, 2016 by KneeTheCap
avatar
KneeTheCap: commas were the culprit here, changed them and now it works. This assignment is almost done, only one thing left. How do I make a checkbox that affects only one radio input?

I mean, there's two radio checkboxes and they have different values (ticket prices) assigned to them. Now I need to make a checkbox that acts as a discount and affects only to one price. How do I make it that the discount is added only when the two correct checkboxes are selected?

As for Finnish, I agree but the teacher requires us to use it. I have no idea why.
You are going to need another event handler. When the discount checkbox is hit you would check if both the required checkboxes are selected and if so, change the value of the price checkbox.

var discount = document.getElementById("discount");
var price = document.getElementById("price");

discount.addEventListener("change", function (e) {

if (discount.checked && price.checked) {
price.value = price.value * discount.value;
}

});
avatar
KneeTheCap: commas were the culprit here, changed them and now it works. This assignment is almost done, only one thing left. How do I make a checkbox that affects only one radio input?

I mean, there's two radio checkboxes and they have different values (ticket prices) assigned to them. Now I need to make a checkbox that acts as a discount and affects only to one price. How do I make it that the discount is added only when the two correct checkboxes are selected?

As for Finnish, I agree but the teacher requires us to use it. I have no idea why.
If statement?

function CalculatePrice() {
if document.getElementById('radio_which_is_affected_by_checkbox').checked && document.getElementById('checkbox').checked {
#calculate discount price
}
else {
#calculate non-discount price, depending on which radio is checked
}
return price
}