C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C#

I had stayed with this question for 10 days🥴 ............................................................................ //your code goes here int result; foreach(string count in coffee.Keys.ToArray()) { result=coffee[count]*discount/100; coffee[count]-=result; Console.WriteLine(count+": "+ coffee[count]); }

11th May 2021, 10:08 PM
Hussein Ashammeri
Hussein Ashammeri - avatar
1 Answer
+ 2
This should work: int result; foreach (string count in coffee.Keys) { result=coffee[count] * discount/100; result = coffee[count] - result; Console.WriteLine(count+": "+result); } The main problem I saw when running your code was that the coffee dictionary was being modified in your foreach which also iterated through coffee. This threw an exception. I replaced storing your calculated result in coffee[count] with storing it in the result variable. Contrary to what the question says, you actually don't need the .ToArray() so I removed that. It is generally good to follow what the problem says but it is also nice to keep code as clean as possible. Writing clean code is a broader goal outside of the sololearn course so I thought it was best to leave that unnecessary method call out.
12th May 2021, 6:02 AM
Josh Greig
Josh Greig - avatar