0

I have a problem in slideAC(data) function.

If condition seem to have some problem in the picture condition (data[0] == "picture")

I have already try to alert the data for testing the input value by alert(data[0]);

and the result is "picture" as well I have no idea what is the problem??

since other test condition work correctly.


HERE IS THE INPUT DATA IN THE extractData(data <-- array) FUCNCTION

(It's Already Split from other function by use split("\n"))

slide
,- width 400
,- height 300
,- into #slide1
,- picture
,+[pic/001.png]
,+[pic/002.jpg]
,+[pic/003.jpg]
,+[pic/004.jpg]
,

The purpose of the code is extract the words from the above text and generate some code.

  function extractData(data){

    alert(data);
    var n = 0;
    var step1 = "";
    var step2 = "";
    var step3 = "";
    var step4 = "";
    var picture =[];
        //check '@' command by call the first line data
        if(data[0].indexOf("slide") !== -1){

            for(var i=1; i<data.length; i++){
//                alert(n);

                switch (n){
                    case 0:///////////////////////////////////////
//                    alert("case1");
                    //extract from '-'
                    if(data[i].indexOf('-') !== -1){
                        step1 = data[i].replace('-','');
                        step2 = step1.split(' ');
                        step3 = step2.slice(1,step2.length);
                        //slide Attribute Compiler
                        n = slideAC(step3);

                    }else{
                        alert("out");
                    }//end of if condition
                    break;

                    case 1:///////////////////////////////////////
//                    alert("case2");
                    //extract from '+'
                    if(data[i].indexOf('+') !== -1){
                        step1 = data[i].replace('+','');
                        step2 = step1.replace("[","");
                        step3 = step2.replace("]","");
                        picture.push(step3);
                        alert(step3);

                    }else if(data[i].indexOf('-') !== -1){
                        step1 = data[i].replace('-','');
                        step2 = step1.split(' ');
                        step3 = step2.slice(1,step2.length);
                        //slide Attribute Compiler
                        n = slideAC(step3);
                    }else{
                        //Error
//                        alert("wrong pic syntax");
//                        javascript_abort();
                    }//end of if condition
                    break;

                }//end of iswitch case

            }//end of item for loop

        }else if(data[0].indexOf("menu") !== -1){
            //Provision
        }else if(data[0].indexOf("form") !== -1){
            //Provision
        }else{
            javascript_abort();
        }//end of if condition


    }//end of syntaxCompiler


    //slide Attribute Compiler
    function slideAC(data){
//    alert(data[0]);

    var a = 0

            if(data[0] == "width"){
                var propWidth = data[1];
//                alert(data[0] + " : " + propWidth);
//                alert(typeof data);
                a = 0;
            }else if(data[0] == "height"){
                 var propHeight = data[1];
//                alert(data[0] + " : " + propHeight);
//                alert(typeof data);
                a = 0;
            }else if(data[0] == "into"){
                var propInto = data[1];
//                alert(data[0] + " : " + propInto);
//                alert(typeof data);
                a = 0;
            }else if(data[0] == "picture"){
                a = 1;
            }else{
                alert("why");
//                javascript_abort();
            }//end of if condition

        return a;

    }//end of slide attribute compiler

Do you have some advise ?? Please help

I just want the if condition work correctly

Thanks in advance

PS. Sorry If my wording make you confuse.

1
  • on which line you are facing issue? Commented Apr 25, 2013 at 17:08

2 Answers 2

2

I'm guessing there may be space characters around the words. Hard to tell because you haven't shown a good picture of the result of the split, or shown the original input, and how you're splitting it.

If this is the case, you could trim it. I used a switch statement instead of your if/else if/else.

    // trim the string-------v
            switch(data[0].trim()) {
            case "width":
                var propWidth = data[1];
//                alert(data[0] + " : " + propWidth);
//                alert(typeof data);
                a = 0;
                break;
            case "height":
                 var propHeight = data[1];
//                alert(data[0] + " : " + propHeight);
//                alert(typeof data);
                a = 0;
                break;
            case "into":
                var propInto = data[1];
//                alert(data[0] + " : " + propInto);
//                alert(typeof data);
                a = 0;
                break;
            case "picture":
                a = 1;
                break;
            default:
                alert("why");
//                javascript_abort();
            }

You'll need a patch for the .trim() method if you support old browsers.

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

2 Comments

THANK GOD !! I have wondering about the space since i have try to test many think to see there are a space or not I can't solve it any way. This methods and your solution save my life Thank a lot ^__^ @user2320734
@JongzPuangput the problem here is not an extra space, it's a newline char. trim() gets rid of those too, so it's really useful in this case, but you should review your code where you call split(',')
0

If js says it's false, then it is! You should eval your vars in a debugger to see what is happening. If using Chrome, just place a call to debugger to break and inspect vars values.

http://msdn.microsoft.com/en-us/library/ie/0bwt76sk(v=vs.94).aspx

Comments

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.