-1

I have for loop which goes through the array with arguments. When next argument is "?", "&" or "||", it shouldn't add comma, however, it always adds. I couldn't understand why, here is the code:

var args = ["arg1","arg2","?","arg3"];
var query = "";
for (var i = 0; i < args.length; i++) {

		switch (args[i]) {

			case "?":
				query += " where ";
				break;

			case "&":
				query += " and ";
				break;

			case "||":
				query += " or ";
				break;

			default:
				if (args[i+1] != "?"); 
				{
					query += args[i] + ", ";
					break;
				}
				query += args[i] + " ";
				break;

		}

	}
document.write(query);

When I type this (this is splitted by " " and sent to array args):

arg1 arg2 ? arg3

It prints it like this:

arg1, arg2, where arg3, // while it should be arg1, arg2 where arg3,

Thanks for helping people, problem was caused by an extern script. And yes, I removed semicolon ;)

12
  • where is it different arg1, arg2, where arg3, // while it should be arg1, arg2 where arg3,? Commented May 26, 2016 at 18:43
  • 1
    @NinaScholz that comma after arg2 before where is not wanted apparently. Commented May 26, 2016 at 18:43
  • 1
    Prints where? You're probably "printing" an array, and its toString method adds the comma. Commented May 26, 2016 at 18:44
  • @Teemu When I print query variable. Query variable is string and is defined before this loop. Commented May 26, 2016 at 18:44
  • 1
    if (args[i+1] != "?"); oops! Commented May 26, 2016 at 18:46

4 Answers 4

5

Your if statement is broken:

        if (args[i+1] != "?"); // <---- remove that semicolon
        {
            query += args[i] + ", ";
            break;
        }

You've got a stray semicolon. It is not a syntax error, but it means the if doesn't do anything. The code that adds the comma always runs, and exits the switch before the code that doesn't add the comma.

Sign up to request clarification or add additional context in comments.

10 Comments

@CayceK I have, and I've also run the code. It works without the semicolon.
Thanks for trying to help out but I solved this. This problem was caused by another array from extern script, now everything works. Thanks for answers anyway.
@Nikola trust me, you want to get rid of that semicolon.
@Nikola The problem is ppl vote for the wrong answer without knowing the whole story (for comments here and there). This could misguide other persons to find out the right answer, which is lies in a comment.
@SyedEkramUddinEmon Except in this case it's not the wrong answer. That semicolon dramatically changes the meaning of the code.
|
1

You have a semicolon between your if and your block:

if (args[i+1] != "?"); 

Should be

if (args[i+1] != "?")

Comments

1

There may be completely different ways to solve this problem which would make your code easier to extend without deepening trees of if or switch

A quick example,

// define some dictionaries
let logicDict = Object.assign(Object.create(null), {
    '?': 'where',
    '&': 'and',
    '||': 'or'
});

// define some flags
let noComma = false;

// reduce your array
['arg1', 'arg2', '?', 'arg3'].reduceRight((str, e) => {
    if (e in logicDict) {
        noComma = true;
        return logicDict[e] + ' ' + str;
    }
    if (!noComma) e += ',';
    noComma = false;
    return e + ' ' + str;
}, '').slice(0, -1);
// "arg1, arg2 where arg3,"

1 Comment

This is very interesting. Thanks!
0

Thanks for noticing semicolon guys, problem was caused by an extern script.

3 Comments

Your script, modified only to declare query and to hook args up to process.argv as a Node.js program, did not work with the semicolon but it did work correctly once the semicolon was removed.
Indeed. Leave that semicolon in and it'll still be wrong.
LOL, I said thanks for noticing semicolon guys, and you're still complaining about ..king semicolon?! Yes, that was an error, I know! No matter how it sounds stupid, but it actually worked even with that semicolon (and I'm really not sure how, but I removed it). I won't repeat this guys. Please, there's nothing new to say about this. Thanks for noticing, again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.