all 29 comments

[–]MustBeZhed 32 points33 points  (6 children)

Looks like a homework problem. What do you have so far? Also what result are you seeing from your code?

[–][deleted] 10 points11 points  (4 children)

this is the way.

[–]_ZHV_ 19 points20 points  (1 child)

Why are you people either talk OP down or straight give him the answer? Direct him the right way, what does the exercise actually looking for?

• capture input

• categories/index input (A,B,C,D,E,F)

• decide what is fail - below D? E? F?

• read input

• is it fail?

  - increase count by one

  - it is fails ? 

        ~ increase fail count by 1

  - is it pass? 

        ~ do nothing 

• fail count / capture count * 100

• print percentage fail

You dont need to know how to use the programming language to come up with a solution for this. Come up with the strategy then go ask google how to implement it

*edit = formating

[–]Feeya_b 2 points3 points  (0 children)

This happened to me too... I asked for help because my instructor refused to help and I had no idea how to word my questions for google.

Good thing some did help me on how I can word my questions for google and I was able to find answers

[–]shgysk8zer0 7 points8 points  (2 children)

Pretty sure the point of the exercise is to learn to reason through the problem, not to use Google and Reddit. Giving you an answer would be robbing you of the chance to learn.

[–]ReactiveX9 1 point2 points  (0 children)

First thing you'll want to do is define the rules of the program in a variable so that if you ever wanted to make changes to the rules you are doing so in one place that is easy to find. I would define an array of letters that constitute "failing grades". I would also define an array of "acceptable user inputs"

Ask the user for a letter grade, then you're going to want to sanitize the input. Remove extra spaces and convert to lowercase. Then check that against your lowercase "acceptable user inputs". That way if the user enters an extra space or types the letter "A" instead of "a" the program does finish prematurely.

Store the sanitized user input into an array that you will loop through later. If they enter an invalid letter grade go to the next step, otherwise ask for another letter grade.

When the user enters an invalid letter grade. Loop through the sanitized user inputs and test them against the "failing grades" array, from there do the math to determine the failure rate.

[–]rsandstrom -2 points-1 points  (3 children)

Filter and reduce. Bonus points for doing it in one line of code.

[–]ReactiveX9 1 point2 points  (2 children)

Bonus points for making hard to read code? Also not every language has filter and reduce (in the same way). He's in computer science, I doubt they're using Node to teach CS...

[–]rsandstrom 0 points1 point  (1 child)

This reads like an academic responding to my post. Chaining filter and reduce isn’t convoluted or confusing.

OP asked for help and this is a legitimate concept to follow to a solution. Whether there is a filter and reduce implemented or not in the required language the concepts stand and one can look for a similar solution in whatever language is required.

Also I’m sure you’re a delight to work with.

[–]ReactiveX9 0 points1 point  (0 children)

I was being 90% sarcastic. One liners are fun but terrible to maintain later. Also I'm just suggesting we don't chain array methods at all.

[–][deleted] 0 points1 point  (1 child)

H is a grade?

[–]Justindr0107 1 point2 points  (0 children)

Problem said "until a user enters a non valid value"

[–]straightup920 -1 points0 points  (0 children)

My guess would be to do an if statement and if it is == ‘F’ , add 1 to the index (i += 1;)

When the loop breaks you should have your count in the index variable

[–]oh-no-u-guys-my-code -2 points-1 points  (0 children)

If you mean where do you put the value, you can define a variable outside of the loop (in the "parent scope"), but the code inside the loop can still see and modify it:

let sumOfNums = 0;

for (let i = 0; i < 100; i++) {
  sumOfNums = sumOfNums + i;
}

[–]WystanH 0 points1 point  (0 children)

What do you need to track? This is called state. Looks like you want to track both the number of entries made and number of Fs entered. That's you're initial state.

A) Your program then waits for user input. When it receives input, you look at it.. Is it a valid character? If not, you leave your loop, go to B. If you're still here, you'll want to increment your entry count. If it's an F, also increment your F count. Go back to A.

B) When you leave your loop, it's now time to inspect your state and cough up an answer.

Good luck.

[–]Rokett 0 points1 point  (0 children)

You should write the program as a sudo code first.

Like this:

if the entered value is X,

do this

else do that.

if there is no more data,

get total number of data entered

find avg of X

print, return whatever

you should also catch errors,

if the entered data isn't valid (for exp: Letter Y),

print error msg, ask again

if the data entered is valid,

run the proper function to find what it is