/    Sign up×
Community /Pin to ProfileBookmark

[Help!] Javascript Switch Statements!

This is my first post I know! haha I need some help for an assignment that I have at school. I know, i’m a super rookie! If someone can point me in the right direction that would be amazing!l:
Write a program check the math of a student. The program must allow the student to enter two numbers and an arithmetic operator (+, -, *, or /) and the student’s answer to the problem. The program should compute the correct answer and compare it to the student’s answer and output “Correct! Good job.” or “Incorrect. Try again!”.

<!DOCTYPE HTML>
<html lang=”en-us”>
<head>
<meta charset=”utf-8″>
<title>Check Your Work</title>
<script type=”text/javascript”>

function math () {

var x = parseFloat(document.getElementById(“value1”).value);
var s = parseFloat(document.getElementById(“symbol”).value);
var y = parseFloat(document.getElementById(“value2”).value);
var z = parseFloat(document.getElementById(“useranswer”).value);

switch (correctanswer) {

case ‘+’:
var correctanswer = x + y
if z != correctanswer
then display “Incorrect. Try Again!”
if z == correctanswer
then display “Correct! Good Job.”
break;

case ‘*‘:
var correctanswer = x *
y
if z != correctanswer
then display “Incorrect. Try Again!”
if z == correctanswer
then display “Correct! Good Job.”
break;

case ‘/’:
var correctanswer = x / y
if z != correctanswer
then display “Incorrect. Try Again!”
if z == correctanswer
then display “Correct! Good Job.”
break;

}

}
</script>
</head>

<body>
<h1>Test Your Math Skills!</h1>
<input type=”text” id=”value1″>
<input type=”text” id=”symbol”>
<input type=”text” id=”value2″<br>
= <input type=”text” id=”useranswer”>
<br><button type=”button” onclick=”math()”>Check</button>
<div id=”output”></div>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@jalarieNov 03.2014 — 
var s = parseFloat(document.getElementById("symbol").value);
[/QUOTE]

The variable "s" is a CHARACTER; get rid of the "parseFloat" stuff.


switch (correctanswer) {
[/QUOTE]

You are using "case" to check the symbol, but the switch is on "correctanswer" which, at this point, doesn't exist.
Copy linkTweet thisAlerts:
@TcobbNov 03.2014 — 
  • 1. Listen to Jalerie


  • 2.get rid of the "then"s

  • 2. What is "display?" Did you mean "alert?"


  • 4. Learn to use parentheses, semicolons and wavy brackets, as in:


  • [CODE]case '+':
    var correctanswer = x + y;
    if (z != correctanswer){
    alert( "Incorrect. Try Again!");
    }
    if (z == correctanswer){
    alert("Correct! Good Job.");
    }
    break;[/CODE]
    ×

    Success!

    Help @Empanada22 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 6.17,
    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: @nearjob,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...