我遇到了JavaScript语法问题

时间:2016-03-17 01:51:24

标签: javascript syntax

我在为codecademy做这个任务时遇到了麻烦,并且想知道你们是否可以帮我找出代码中的这个bug。

confirm("I am ready to play!")
var age = prompt("What's your age");
if(age is > 13)
{
    console.log( "We arent responsible if under age restriction")
}
else 
{
    console.log("Enjoy!")

4 个答案:

答案 0 :(得分:2)

(age is > 13 )替换为(age > 13)

答案 1 :(得分:1)

还需要关闭else语句:

package instantMessanger;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;



 public class ClientRunner {
    public static void main(String[] args){
        Client client = new Client("127.0.0.1"); //using local host
        client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        client.startRunning();
    }
}

public class Client extends JFrame{

    /**
     * An Instant Messaging Client
     */
    private static final long serialVersionUID = 1L;
    private JTextField userText;
    private JTextPane chatWindow;
    private JScrollPane sPane;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private String message = "";
    private String serverIP; 
    private Socket connection;
    private String ClientUserName; 

    //constructor
    public Client(String host){
        super("Client Instant Messanger");
        serverIP = host;
        userText = new JTextField();
        userText.setEditable(false);
        userText.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        sendMessage(event.getActionCommand()); //takes text from text area and sends it
                        userText.setText("");
                    }
                }
        );
        add(userText, BorderLayout.NORTH);
        //chatWindow = new JTextArea();
        chatWindow = new JTextPane();
        sPane = new JScrollPane(chatWindow);
        add(sPane, BorderLayout.CENTER);
        setSize(600, 300);
        setVisible(true); 
    }

    //start
    public void startRunning(){
        try{
            connectToServer();
            setupStreams();
            whileChatting();
        }catch(EOFException e){
            showMessage("\n Client terminated the connection ");
        }catch(IOException i){
            i.printStackTrace();
        }finally{
            cleanUp();
        }
    }

    //connect to server
    private void connectToServer() throws IOException{
        showMessage("Attempting connection... \n");
        connection = new Socket(InetAddress.getByName(serverIP), 6879);
        showMessage("Connected to: " + connection.getInetAddress().getHostName());
    }

    //generate username with random (unique) number
    private void generateUserName(){
        ClientUserName = "CLIENT"+ (int)Math.floor(Math.random() * ( 10 ));
    }

    //set up streams to send and receive messages
    private void setupStreams() throws IOException{
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();
        input = new ObjectInputStream(connection.getInputStream());
    }

    //while chatting with server
    private void whileChatting() throws IOException{
        generateUserName();
        ableToType(true);
        do{
            try{
                message = (String) input.readObject();
                showMessage("\n" + message);
            }catch(ClassNotFoundException c){
                showMessage("\n user input not accepted");
            }
        }while(!message.equals("SERVER - END"));
    }

    //clean up
    private void cleanUp(){
        showMessage("\n Closing the chat!");
        ableToType(false);
        try{
            output.close();
            input.close();
            connection.close();
        }catch(IOException i){
            i.printStackTrace();
        }   
    }

    //send message to server
    private void sendMessage(String message){
        try{
            output.writeObject(ClientUserName + " — " + message);
            output.flush();
            showMessage("\n" + ClientUserName + " — " + message);
        }catch(IOException ioE){
            appendToPane(chatWindow, "\n Error sending message", Color.RED);
        }
    }

    //change + update chatWindow
    private void showMessage(final String message){
        System.out.println("the message is: " + message + " ends here");
        SwingUtilities.invokeLater(
                //create the thread
                new Runnable(){
                    public void run(){
                        if(message.contains("CLIENT")){
                            appendToPane(chatWindow, message, Color.blue); //append the message to the history
                        }else if(message.contains("SERVER")){
                            appendToPane(chatWindow, message, Color.MAGENTA); 
                        }else{
                            appendToPane(chatWindow, message, Color.green); 
                        }
                    }
                }
        );
    }

    //allow user to type into text box
    private void ableToType(final boolean tof){
        SwingUtilities.invokeLater(
                new Runnable(){
                    public void run(){
                        userText.setEditable(tof);
                    }
                }
        );
    }

    //append to pane
    private void appendToPane(JTextPane tp, String msg, Color c)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }



}

答案 2 :(得分:1)

如果要调试代码,请打开JavaScript控制台(大多数Windows浏览器上为F12,大多数Mac浏览器上为Cmd + Shft + I),然后单击“控制台”选项卡,然后刷新页面。

当您的代码遇到错误时,会在其中记录错误说明和行号。

正如其他人提到的那样,从is < 13中删除“是”。 “&lt;”符号字面上被解释为“小于”,因此不使用“是”字。

此外,您需要在每个语句的末尾添加分号,并记得在打开条件代码时关闭它们。

在将大纲扩展为实际代码时,您需要密切关注语法。错误的变量/函数或丢失的标点符号在代码中反复发生时可能非常令人沮丧。必须在语法中正确地形成每一段代码而不会出错,否则您的代码将无法正常工作。

答案 3 :(得分:0)

一个完整正确的答案:

$scope.selPcode = 123