/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Working with a Function – cannot get two parts working.

Hi, below is the assignment followed by my code.

The two parts I cannot get working are:
[COLOR=”#FF0000″]1)[/COLOR] If I delete the default number from the input box the message I want “Invalid input. Exiting program” does not come up.
[COLOR=”#FF0000″]2)[/COLOR] I also want the same message when they Cancel button is pushed.
If I delete the nubmer and press OK it returns “Your Grade is: E” and when I click the Cancel button it also returns “Your Grade is: E”. Thank you for your help.

[COLOR=”#0000CD”][/COLOR][B]Assignment:[/B]
• Create a JS script code containing a function that inputs a student’s average and returns:
o A if the student’s average is 90-100
o B if the average is 80-89
o C if the average is 70-79
o D if the average is 60-69
o E if the average is 0-59.
o If the average is NOT between 0 and 100, display “invalid input”.

Your code should ask the user to input the student’s average, validate the average (=value should be between 0 – 100), and then:

o If valid, call a function to determine and return A, B, C, D, or E depending on the average.
[COLOR=”#B22222″]o If the average is not valid or if the user pressed Cancel button, display an alert message and exit the program. – not working[/COLOR]

[CODE]<script type=”text/javascript”>

function stuAverage(param1, param2, param3, param4, param5, param6)
{
var stuAverage = prompt(“To determine your grade please enter your average”, 60);

if ((isNaN(stuAverage)) || ((stuAverage <0) || (stuAverage >100)))
{
alert(“Invalid input. Exiting program”);
}

else if (stuAverage >= 90 && stuAverage <= 100)
{
return alert(“Your Grade is: ” + param1 + “”)
}

else if (stuAverage >= 80 && stuAverage <= 89)
{
return alert(“Your Grade is: ” + param2 + “”)
}

else if (stuAverage >= 70 && stuAverage <= 79)
{
return alert(“Your Grade is: ” + param3 + “”)
}

else if (stuAverage >= 60 && stuAverage <= 69)
{
return alert(“Your Grade is: ” + param4 + “”)
}

else if (stuAverage >= 0 && stuAverage <= 59)
{
return alert(“Your Grade is: ” + param5 + “”)
}

else if (stuAverage == ” “)
{
return alert(param6 + “”)
}
}

stuAverage (“A”, “B”, “C”, “D”, “E”, “Invalid input. Exiting program”);

</script> [/CODE]

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Nov 07.2014 — &lt;script type="text/javascript"&gt;

<i> </i>function stuAverage(param1, param2, param3, param4, param5, param6)
<i> </i>{
<i> </i>var stuAverage = prompt("To determine your grade please enter your numeric average",60);
<i> </i> if (stuAverage != null)
<i> </i> {
<i> </i> if (stuAverage &gt;= 90 &amp;&amp; stuAverage &lt;= 100)
<i> </i> {
<i> </i> return alert("Your Grade is: " + param1 + "");
<i> </i> }

<i> </i> else if (stuAverage &gt;= 80 &amp;&amp; stuAverage &lt;= 89)
<i> </i> {
<i> </i> return alert("Your Grade is: " + param2 + "");
<i> </i> }

<i> </i> else if (stuAverage &gt;= 70 &amp;&amp; stuAverage &lt;= 79)
<i> </i> {
<i> </i> return alert("Your Grade is: " + param3 + "");
<i> </i> }

<i> </i> else if (stuAverage &gt;= 60 &amp;&amp; stuAverage &lt;= 69)
<i> </i> {
<i> </i> return alert("Your Grade is: " + param4 + "");
<i> </i> }

<i> </i> else if (stuAverage &gt;= 0 &amp;&amp; stuAverage &lt;= 59)
<i> </i> {
<i> </i> return alert("Your Grade is: " + param5 + "");
<i> </i> }

<i> </i> else
<i> </i> {
<i> </i> alert(param6);
<i> </i> }
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> alert(param6);
<i> </i> }
<i> </i>}
<i> </i>stuAverage ("A", "B", "C", "D", "E", "Invalid input. Exiting program");


&lt;/script&gt;
Copy linkTweet thisAlerts:
@computersadeNov 07.2014 — For information, it is considered bad practice to use the same name for a function and a variable.

Try this:
[CODE]<script type="text/javascript">
function stuAverage() {
var sa_Input = prompt("To determine your grade please enter your average", 60);

var msg = "";
var grade = "Invalid input. Exiting program";

if ( sa_Input != null && sa_Input != "" && (sa_Input >= 0 && sa_Input <= 100)){
msg = "Your Grade is: ";
if (sa_Input >= 90 && sa_Input <= 100) {
grade = "A"
}

else if (sa_Input >= 80 && sa_Input <= 89) {
grade = "B"
}

else if (sa_Input >= 70 && sa_Input <= 79) {
grade = "C"
}

else if (sa_Input >= 60 && sa_Input <= 69) {
grade = "D"
}

else if (sa_Input >= 0 && sa_Input <= 59) {
grade = "E"
}
}

return alert(msg + grade)
}

stuAverage ();

</script>[/CODE]
Copy linkTweet thisAlerts:
@vexingNov 08.2014 — Hi BlondieC

I think were in the same online course?

This forum is helping me out as well. Thxs?

Are you taking Dynamic Web sites with AMP (Apache, MySQL and PHP)?

The Mid-term assignment is stressing me out!

:eek:
Copy linkTweet thisAlerts:
@BlondieCauthorNov 09.2014 — Thank you and thank you for the tip re using the same name for a function and a variable. Completely forgot about using null.
Copy linkTweet thisAlerts:
@BlondieCauthorNov 09.2014 — Thank you - I forgot about null. I've read about it but haven't used it till now.
Copy linkTweet thisAlerts:
@BlondieCauthorNov 09.2014 — Hi BlondieC

I think were in the same online course?

This forum is helping me out as well. Thxs?

Are you taking Dynamic Web sites with AMP (Apache, MySQL and PHP)?

The Mid-term assignment is stressing me out!

:eek:[/QUOTE]


Hi Vexing, my online course is JavaScript but I will eventually be taking the one you are taking. My mid-term was a couple of weeks ago now and I didn't do very well on the coding part. Even though it was 90 minutes it wasn't enough time for me as I started to panic and then couldn't think things through. I have found this forum very helpful with my questions, making sure things are correct and getting lots of additional info and tips I've been writing down.
Copy linkTweet thisAlerts:
@vexingNov 10.2014 — I didn't do well on the mid-term task program assignment either, 20% ?

It takes me forever to figure this out. I find that the examples are simple and assignments are complicated. i wish they would give some structure to follow. I dropped Dynamic Websites with AMP=PHP its not any better. Im going to retake it next semester. Im going to read up on anything I can find on it during the holidays its a lot like JavaScript script wise.
Copy linkTweet thisAlerts:
@BlondieCauthorNov 13.2014 — This is the first course that I've actually struggled with and it's the assignments. I can't always find the connection between the assignment and the material covered. It's like read step 1 now complete step 67 and somewhere in between is the how to part but it's not identified or clear.

I've been very frustrated and can't wait for it to be over. After I complete the other courses I will come back to JavaScript and try to find a place or course where I can learn it and have it make sense.
Copy linkTweet thisAlerts:
@iBeZiNov 13.2014 — It sounds to me like the assignment is only asking you to use a function when working out the grade from the average given by the user, rather than wrapping everything in a function. So something like this

[code=php]
function calculateGrade(stuAverage) {
if (stuAverage >= 90 && stuAverage <= 100) {
return "A";
} else if (stuAverage >= 80 && stuAverage <= 89) {
return "B";
} else if (stuAverage >= 70 && stuAverage <= 79) {
return "C";
} else if (stuAverage >= 60 && stuAverage <= 69) {
return "D";
} else if (stuAverage >= 0 && stuAverage <= 59) {
return "E";
}
}

//get average and trim off any whitespace
var average = prompt('To determine your grade please enter your average',60).trim();

//check average isn't empty, null, not a number and is between 0 and 100
if(average == '' || average == null || isNaN(average) || average > 100 || average < 0) {
alert('Invalid Input');
} else {
//use function to return letter grade
alert('Your grade is: '+calculateGrade(average));
}

[/code]
Copy linkTweet thisAlerts:
@vexingNov 17.2014 — It's not you, the course could be better presented it's confusing. I'm sure there is a better way of learning JavaScript. PHP follows the same rules as JavaScript, they are both c# based languages. Maybe it would of been best to learn C# first? My PHP teacher told me I'll get better with practice. Your forum is very helpful!)
Copy linkTweet thisAlerts:
@BlondieCauthorNov 18.2014 — I found the course instructor confusing along with his instructions - very confusing. There are total disconnects between the chapters we are to read and the assignments. It's very frustrating trying to learn and understand this.
Copy linkTweet thisAlerts:
@vexingDec 02.2014 — Hey Blondie!

Find me in the class participants, my last name is Fortin.

E-mail me.

If your taking PHP next semester we'll be in the same online class again?

There is only one teacher for this course. First assignment is midterm assignment, 5% online quiz weekly.
Copy linkTweet thisAlerts:
@AMYLAURAFeb 28.2015 — Hey Blondie,

I am taking the same javascript class as you did and have been struggling since Assignment 2 because of the instructor's teaching method. I'm about to do the midterm exam and wondered if you could pass over any helpful info on the coding section. I'm a working mom of 2 and I have to tell you that I don't know what I would have done without your posts here. They have saved my life and I have related to everything that you have written here and I'm positive that I'm not the only one that thinks that!! It's a very frustrating situation to be in. I'd appreciate any guidance.

Amy
Copy linkTweet thisAlerts:
@BlondieCauthorMar 16.2015 — Hey Blondie!

Find me in the class participants, my last name is Fortin.

E-mail me.

If your taking PHP next semester we'll be in the same online class again?

There is only one teacher for this course. First assignment is midterm assignment, 5% online quiz weekly.[/QUOTE]


Hi Vexing, Sorry for the very late reply - I had no idea there was a message from you. I took the January semester off - I needed a break after taking 4 courses back-to-back. I still have the PHP course to but for the Spring/Summer session I think I'll take the Photoshop1 course. Not sure what to expect with the PHP - I hope it goes more smoothly than the JavaScript.
Copy linkTweet thisAlerts:
@BlondieCauthorMar 16.2015 — Hey Blondie,

I am taking the same javascript class as you did and have been struggling since Assignment 2 because of the instructor's teaching method. I'm about to do the midterm exam and wondered if you could pass over any helpful info on the coding section. I'm a working mom of 2 and I have to tell you that I don't know what I would have done without your posts here. They have saved my life and I have related to everything that you have written here and I'm positive that I'm not the only one that thinks that!! It's a very frustrating situation to be in. I'd appreciate any guidance.

Amy[/QUOTE]


Hi Amy Laura, the JavaScript course was not an easy one for me to get through but I did thanks to people on here who were willing to share their knowledge. You've probably written the mid-term already and I can't even remember how many parts there were for the coding - I think two with the first one broken down into sub-parts. Hopefully you got through it ok.
×

Success!

Help @BlondieC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...