www.webdeveloper.com
Results 1 to 6 of 6

Thread: if else doesn't work

  1. #1
    Join Date
    Sep 2012
    Posts
    5

    if else doesn't work

    this does not work
    what is wrong with it
    making a program that conjugates Spanish verbs

    var pronoun = prompt("What is your pronoun(put yo, tu, el, etc.)");

    var verb = prompt("What is your verb");

    var last = verb.substring(verb.length-2)

    var first = verb.substring(0,verb.length-2)

    var type = verb.substring(verb.length-2,verb.length-1);

    if (pronoun === yo && type === a) {
    alert(first + "o");
    };

    else if (pronoun === yo && type === e) {
    alert(first + "e");
    };

    else (pronoun === yo && type === i) {
    alert(first + "o");
    };

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    you are still making the same mistake here, leaving out the if:

    Code:
    else (pronoun === yo && type === i) {
    alert(first + "o");
    };
    aside from that pronoun is a variable and yo is a string, so it should be
    Code:
    pronoun === "yo"
    i is a string too, so that should be type === "i" as well

    oh - and you're still putting the semi colons after the ending brace of the if statement. What's the point in asking questions if you're not going to listen to the answers?
    Last edited by xelawho; 09-21-2012 at 12:47 AM.

  3. #3
    Join Date
    Nov 2010
    Posts
    1,084
    ... but you are going to end up with an insanely long list of if/elses. Here's a shortcut that you might be interested in:
    Code:
    <script type="text/javascript">
    var pronoun = prompt("What is your pronoun(put yo, tu, el, etc.)");
    
    var verb = prompt("What is your verb");
    
    var last = verb.substring(verb.length-2)
    
    var first = verb.substring(0,verb.length-2)
    
    var type = verb.substring(verb.length-2,verb.length-1);
    
    var verbs={yo:{a:"o",e:"e",i:"o"},
    			tu:{a:"as",e:"as",i:"is"}
    		}
    
    alert(first+verbs[pronoun][type])
        </script>
    now all you have to do is figure out how to code the irregulars

  4. #4
    Join Date
    Sep 2012
    Posts
    3
    you have a 2 simple errors in your code
    the first one as xelawho said all variables in your code are string, so they should be marked like this
    pronoun === 'yo' or pronoun === "yo"
    and so for
    type === 'a' or type === "a"

    second error at last part of code
    else (pronoun === 'yo' && type === 'i')
    because last eles desn't need a condition
    else {........}
    or make it else if
    eles if(pronoun === 'yo' && type === 'i') {........}
    so your code will be like this



    var pronoun = prompt("What is your pronoun(put yo, tu, el, etc.)");

    var verb = prompt("What is your verb");

    var last = verb.substring(verb.length-2);

    var first = verb.substring(0,verb.length-2);

    var type = verb.substring(verb.length-2,verb.length-1);

    if (pronoun === 'yo' && type === 'a') {
    alert(first + "o");
    };

    else if (pronoun === 'yo' && type === 'e') {
    alert(first + "e");
    };

    else if (pronoun === 'yo' && type === 'i') {
    alert(first + "o");
    };

  5. #5
    Join Date
    Sep 2012
    Posts
    5

    thanks

    i made the changes and it worked
    this is the code

    var verb = prompt("What is your verb");

    var last = verb.substring(verb.length-2)

    var first = verb.substring(0,verb.length-2)

    var type = verb.substring(verb.length-2,verb.length-1);

    var verbs={yo:{a:"o",e:"e",i:"o"},
    tu:{a:"as",e:"as",i:"is"},
    el:{a:"a",e:"e",i:"e"},
    ella:{a:"a",e:"e",i:"e"},
    usted:{a:"a",e:"e",i:"e"},
    nosotros:{a:"amos",e:"emos",i:"imos"},
    nosotras:{a:"amos",e:"emos",i:"imos"},
    vosotros:{a:"ais",e:"eis",i:"is"},
    vosotras:{a:"ais",e:"eis",i:"is"},
    ellos:{a:"an",e:"en",i:"en"}

    }

    alert(first+verbs[pronoun][type])


    just asking,
    how does alert(first+verbs[pronoun][type]) work

    i don't understand why the [pronoun][type] has to be there

    please don't criticize me, i want to understand why it has to be there
    i've only been doing javascript for 4 days
    i'm 12 years old

  6. #6
    Join Date
    Nov 2010
    Posts
    1,084
    I'm not criticizing you and I think it's great that you're getting into coding so early. You will be a total ninja by the time you are 20 if you keep going. But if you are going to ask questions it's polite to read the answers and try not to repeat the same mistake. Doesn't matter how old you are, and anyway you will learn faster that way.

    In the code I showed you verbs is an object (pretty much everything in javascript is an object, so you should get used to working with them).

    Objects have keys and keys have values. Yo, tu, el, etc are keys, but here each value is another object. Within the yo object a, e and i are keys and "o", "e" and "o" are their respective values.

    You can access an object's values in two ways: dot notation -
    Code:
    alert(verbs.yo.a) // returns "o"
    or array notation -
    Code:
    alert(verbs["yo"]["a"]) // returns "o"
    They return the same thing, but if you are trying to access object values with variables (as you do in your code) you can't use dot notation, but you can use array notation. In plain English, this:
    Code:
    verbs[pronoun][type]
    says return the value corresponding to the key that matches the "type" variable from the object that matches the "pronoun" variable from within the verbs object.

    which I guess is not so plain, but that's as best I can explain it.

    hope that helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles