Very dumb question about JavaScript

Pages: 12
Thank you very much! It's runing good!

... i even didnt have the compiler to compile this JavaScript, I just pick up an online instant compiler to run the program!

Thank you very much for your guys help! I really appreciate!

You can just save it as a .html file and then open it in a web browser by double clicking on it.
Last edited on
I know how to run it, just don't know how to get error code if the program has problem. I think i actually need a compiler thing.
Last edited on
Actually I wanna know use what compiler can use for test javaScript and get error code if there is any?
Can anyone help with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>

  <head>
    
    <title>
    
    
    </title>
    
    
    <script>
    
    
      function getInputAsText(_id){return document.getElementById(_id).value};
      function getInputAsNumber(_id){return parseFloat(document.getElementById(_id).value)};
      function setOutput(_id, _value){document.getElementById(_id).value = _value;}//Is it NULL? If it is, why and how to fix?
      function calculate()
        {
      
        // declare a variable 
          var score;
          var myAnswer1;
          var myAnswer2;
          var myAnswer3;
          var myResult1;
          var myResult2;
          var myResult3;
          
        // get variable's value
           score = 0;
          
          
        // perform this
        
        if (myAnswer1 == "8")
        
        {
        
         score = score + 1 ;// got this one right
         
         myResult1 = "Correct";
         
        }
        
        else 
        
        {
        
        myResult1 = "WRONG! It's 8";
        
        }
         
        if (myAnswer2 == "Apple")
        
        {         
          score = score + 1 ;// got this one right
          myResult2 = "Correct";
        }    
        else
        {        
        myResult2 = "WRONG! It's Apple"        
        }
          
        if (myAnswer3== "Microsoft")
        
        {
        
         score = score + 1 ;// got this one right
         
         myResult3 = "Correct";
         
        }
        
        else 
        
        {
        myResult3 = "WRONG! It's Microsoft"
        }  
        // write Output value
          setOutput("myResult1", myResult1);
          setOutput("myResult2", myResult2);
          setOutput("myResult3", myResult3);
          setOutput("score", "Your score is " + score + " out of 3 ");
    }    
      
    </script>
    
    
  </head>
    
    
  <body>
  
  
      Instruction:<br>
      
      Answer the three questions and press go.<br>
      
      Your score will appear.<br>
      
      
      <br>
  
      
      Input Values:<br>
      
      
      1. A byte is how many bits?      <input id="myAnswer1"><br>
      
      
      2. Steve Jobs heads what company?  <input id="myAnswer2"><br>  
      
      
      3. Bill Gates heads what company?  <input id="myAnswer3"><br>  
      
      
     <button onclick="calculate()">Go</button><br>
      

      <br>
      
      
      Output value:<br>
      
      Result #1: <input type="text" id = myResult1" size="70"><br>
      
      
      Result #2: <input id="myResult2" size="70"><br>
      
      
      Result #3: <input id="myResult3" size="70"><br>
      
      
      Your Score:   <input type="text" id = score" size="70"><br>
      
  
  </body>
  
  
</html> 
Find a forum where this is topical already. Better yet, have your friend find a forum where it's topical.
You forgot a double quote before myResult1 here:

Result #1: <input type="text" id = myResult1" size="70"><br>

Same with score here:

Your Score: <input type="text" id = score" size="70"><br>

Also, you'll have to add a couple of lines in your calculate function:

1
2
3
4
5
6
7
8
9
10
11
    /* ... */

    // perform this
 
    myAnswer1 = getInputAsText("myAnswer1");
    myAnswer2 = getInputAsText("myAnswer2");
    myAnswer3 = getInputAsText("myAnswer3");

    if (myAnswer1 == "8") 

    /* ... */

This could help a lot -> http://www.w3schools.com/js/default.asp

Find a forum where this is topical already. Better yet, have your friend find a forum where it's topical.

This is a very good suggestion too.
Last edited on
Topic archived. No new replies allowed.
Pages: 12