# Variables in JavaScript: Tax and Discount program

This is a post about one of those lessons that crushes your notion of yourself as an effective teacher, and how you rebuild from that! I’ve been working with my 7th and 8th graders on the concept of variables for about a week, and they’ve been learning about percents and decimals for a couple of *years*. It’s harsh when you have that realization that they understood very little of either topic.

I was out of the classroom on Friday, so I had to leave a lesson with my Computer Science students that they could complete independently.

We’ve been learning about variables in JavaScript programming. I decided to leave them with this task:

Create a program that has variables for the cost of an item, the percent discount, and the percent tax rate. Use the program to calculate the new cost after the discount and tax. Display the new cost to the screen.

I put tons of resources on my Google Classroom page. They had a .pdf file with detail and some hints in it:

I also left them a video screencast of me going through the assignment, and giving them some hints.

Screencast part 1

Screencast part 2

I had really high hopes! My sub e-mailed me on Friday: “Kids are finishing 20 minutes into class! What do I have them do?” I thought: how amazing! They were so productive! I can’t wait to see the work they produced.

I wish I could have waited longer!

I logged onto my Google Classroom the next day and saw the awful truth: two students had actually completed the task correctly. TWO! About half of the remaining students had turned in a program at all. Of those, they were full of logical and mathematical errors. As I browsed through them, I decided the biggest issue was that the kids had little understanding how to calculate discount or tax. I used to teach 7th grade math. We spend a whole quarter on discount or tax. The knowledge (if it took at all) didn’t transfer. Another issue I had to tackle involved misunderstanding variables. It was harder to isolate, but this student example shows a little bit of both problems.

The student converted the tax of 5% into the decimal 0.5, which is incorrect. Tons of misunderstandings about discount and tax. Kids sometimes added the percents as dollar amounts… divided by 10 instead of 100 to convert to a decimal… divided by the percent instead of multiplying by the decimal… I saw just about every misconception with percents possible.

This student’s example shows some common misperceptions about variables too. It would have been more efficient to substitute “d / 100” instead of “0.10” in the code for the discount rate. The variable “k” was set by using a calculator to take the discount, instead of using a formula in the program. Another common problem was using assignment statements out of order – for example:

`total_cost = cost_w_discount + cost_w_discount * tax/100;`

`cost_w_discount = cost - cost * discount / 100;`

The cost_w_discount variable needs to be assigned first. In a less forgiving computer language or environment, the total_cost will be calculated from a cost_w_discount that hasn’t yet been calculated – so it could be garbage data.

This problems get to the heart of why text-based programming is so difficult for kids. There are an awful lot of ideas they need to have correct in order to program well.

It really did baffle me that students turned in their work looking like this. They should have been able to look at their output and tell the programs just weren’t doing the right math.

I decided to tackle the percent/tax calculation issue and then work with them on the programming concepts. I started by having them all go to Khan Academy to do the practice problems on discount, tax, and tip, using a calculator. I wanted them to pay careful attention to what math needed to be done to solve the problems.

After they did a few practice problems, we had a quick discussion to summarize their strategies. Most students came to a place where they turned the percent to a decimal, multiplied the original cost by that decimal to get the discount or tax, then added to or subtracted from the original cost. Some students understood where they went wrong in their program at this point, but I gave them another challenge.

I grouped the students and gave them a half-sheet with this prompt: The output is correct, if you use the values for the variables that I listed. Figure out what math I did. Be ready to explain it.

It turned out to be a good group activity, and the students discussed and used their calculators to do trial-and-error. It took them some time to remember that, for example, 3.5% is 0.035 – and that after you calculate the discounted amount, that’s what you tax – you don’t tax the original cost once you’ve subtracted the discount.

When we discussed the activity, the discussion went something like this.

Student: So you do 230 times 0.4 to calculate the discount.

Me: Why 0.4?

Student: Because you move the decimal over 2 places.

Me: So let’s say I’m a computer and I’m not smart enough to understand how to just move a decimal. Could I multiply or divide?

Student: Divide by…. um…

Student 2: 10.

Student 3: No, 100.

Student: Oh, yeah. 100. Divide by 100.

Me: Ok, so I calculate the discount this way. I’ll write “var dc = cost * discount / 100;”. That way it uses my variables. After you multipled 230 by 0.4, what did you do next?

Student: You take it away from the 230.

Me: So now I could say “cost = cost – dc;” That would subtract the discount and store the value back in that variable called “cost”. Now “cost” has changed. Did you deal with the tax next?

We went from there. Before I released the students to finish up their programs, I met with the two students who had completed the task correctly the first time. I gave them heaps of praise and asked if they would be TA’s for the rest of the class and help other students finish the program too. They were really excited to help, and as other students finished the task, they asked if they could TA as well.

Not everyone got done, but many more did, and I felt validated to hear students say “I didn’t really get this before. I do now.” This is really difficult work for some students to program in text for the first time. We still have issues with students using variables before they’ve been assigned and other misconceptions about coding. But it felt like progress and I was proud of how hard they worked to understand tax and discount. It’s not glamorous work, but it’s important. I plan to come back to it when we start on if/then statements, which I think we really need to start on and continue spiraling back to variables.

My spirit was crushed at first, but things finished OK. I had to remember, and still have to remember, that when a task completely falls apart, there’s more than one reason why. But we can make progress if we can isolate those reasons.

### 2 responses to “Variables in JavaScript: Tax and Discount program”

### Trackbacks / Pingbacks

- March 11, 2015 -

My high schools students do this too. For some reason, variables throw them for a loop, and as we get to more complex variable assignment, they get confused, even after they’ve figured out the basics. Just today, I got a “It’s telling me x hasn’t been assigned.” To which I answer, it’s not. Where did you set x equal to something. They recognize the issue immediately when I point it out, but they’re not getting it themselves consistently.

I love these explanations, and I love how you’re teasing out what in the world is going on in their heads.