I’m trying out some new found coding skills and still have a lot of room to go so I know this function maybe a tad crude, but I’m having trouble with getting a correct ‘total’. The function takes any of four inputs values and multiplies them according to different values of ‘select/options’ and calculates a ‘total’. No matter the input value or the value of the ‘select’ option, my total comes up to ‘0’. Anyone tell me what I did wrong?
function submitAll() {
switch (fOne) {
case 'weekly':
cOne *= 4;
break;
case 'biweekly':
cOne *= 2;
}
switch (fTwo) {
case 'weekly':
cTwo *= 4;
break;
case 'biweekly':
cTwo *= 2;
}
switch (fThree) {
case 'weekly':
cThree *= 4;
break;
case 'biweekly':
cThree *= 2;
}
switch (fFour) {
case 'weekly':
cFour *= 4;
break;
case 'biweekly':
cFour *= 2;
}
total = cOne + cTwo + cThree + cFour;
document.getElementById("display").innerHTML = 'Your total income is; ' + total;
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
const fOne = 'weekly'
let cOne = 1
const fTwo = 'monthly'
let cTwo = 2
const fThree = 'biweekly'
let cThree = 3
const fFour = 'weekly'
let cFour = 4
function submitAll() {
switch (fOne) {
case "weekly":
cOne *= 4;
break;
case "biweekly":
cOne *= 2;
}
switch (fTwo) {
case "weekly":
cTwo *= 4;
break;
case "biweekly":
cTwo *= 2;
}
switch (fThree) {
case "weekly":
cThree *= 4;
break;
case "biweekly":
cThree *= 2;
}
switch (fFour) {
case "weekly":
cFour *= 4;
break;
case "biweekly":
cFour *= 2;
}
total = cOne + cTwo + cThree + cFour;
console.log('total', total)
}
submitAll()
// total 28
It seems to work for me. Again, I think this isn’t the best approach - you shouldn’t depend on all those out of scope variables in the function and really that function has too much repetition. I think you’re making this too difficult, as we discussed before.
Ok, now I think I’m beginning to understand what your getting at, I think. What I believe your doing is calling the subTotal function inside the total function. I know its hard to teach an old dog new tricks so please bare with me.
Right, there is a principle in coding - DRY - Don’t Repeat Yourself. You are basically doing the same things 4 times in a row. You can get away with doing the same things twice maybe, but not 4 times. It’s easier to read and you are only doing it in one place so there it is easier to debug.
the reason why you get 0 is probably due to the first assignment of cOne, cTwo..., if you initialize them with 0, then you will get 0, because cOne *= 4; is like cOne = cOne*4 and if cOne === 0 then it will become like cOne = 0.
I just assumed, i don’t know, can you put the whole code?
I used html input values to try different values. There was something wrong in my getElementById().value. I misspelled document then copied and pasted the same mistake four times. Put S*&% in, get S*&^ out right?