1

I'm very new to JS, I have been working with it just for fun and because I have free time. I wrote the code below just after reading manuals online for few minutes, so please don't blame me.

var doctype = document.type;
var n = doctype.indexOf("}") + 1;
var doctype2 = doctype.substring(n);
switch (doctype2) {
    case "doc_func_adm":
        var newtype = "documento-admissao-funcionario";
        switch (document.properties["ext1:tipo_doc_func_adm"]) {
            case "Contrato Experiência":
                var newtype2 = "contrato-experiencia";
                break;
            case "Prorrogação de Contrato":
                var newtype2 = "prorrogacao-contrato";
                break;
            case "Contrato: Prazo Indeterminado":
                var newtype2 = "contrato-prazo-indeterminado";
                break;
            case "Termo Bolsa Família":
                var newtype2 = "termo-bolsa-familia";
                break;
            case "Vale Transporte":
                var newtype2 = "vale-transporte";
                break;
            case "Ficha Registro":
                var newtype2 = "ficha-registro";
                break;
        } 
   break;
 }
 print(newtype + "-" + newtype2);
 print(doctype2); //check string result being tested

Console results:

 undefined-undefined
 doc_func_adm

Please notice that 'doctype2' has the same string as the switch test. As you can see, it returns only undefined values and I believe I'm missing something very basic that I can't see though.

Thanks for the assistance. Cheers.

augustus182l
  • 375
  • 1
  • 2
  • 12
  • Can you console.log(doctype); ? also n and doctype 2 – FLX Oct 05 '15 at 13:07
  • Use a debugger and just step through it... – Bartek Banachewicz Oct 05 '15 at 13:08
  • 2
    `newtype` and `newtype2` are declared inside the scope of the switch statement and don't exist outside of that scope. – 001 Oct 05 '15 at 13:10
  • good point @JohnnyMopp and if I were you I would put it as the answer as that IS the reason. – Thomas Oct 05 '15 at 13:11
  • @Thomas Actually after reading [this](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript), it appears there is no block scope in JS, so my comment is incorrect. – 001 Oct 05 '15 at 14:25
  • then the main question is if there are any unseen symbols in doctype.substring(n); like a \n. @augustus182l can you put one testline before the switch? if (doctype2.indexOf("doc_func_adm")) { print("this is it"); } if the print is executed then the problem is that you have unseen symbols in doctype2 which causes the switch to fail. – Thomas Oct 05 '15 at 17:31
  • I'm using this script inside a javascript embedded console in Alfresco. Johnny, I did defined those vars before outside of the scope with (var newtype=""; and var newtype2=""; I'm not sure if that's what you mentioned. @Thomas var doctype = document.type;var n = doctype.indexOf("}") + 1; var doctype2 = doctype.substring(n);if doctype2.indexOf("doc_func_adm")) { print("this is it") } else { print("else") }; Console result:else That's the output result of all the vars: Console results: doctype2: doc_func_adm n: 35 | doctype: {http://www.bcpg.fr/model/ext1/1.0}doc_func_adm Thanks guys. – augustus182l Oct 05 '15 at 18:47
  • Btw one thing you should not do: If you have variables defined already you shouldn't use "var" on their next use. It could lead to unexpected results (at least in all other programming languages than JS it is so). – Thomas Oct 06 '15 at 06:07

1 Answers1

1

I originally thought your problem would be the scope, but after reading the comments on the original post from @JohnnyMopp, and checking this link on the scope of javascript variables - I would say the problem is here:

 var doctype2 = doctype.substring(n);

  switch (doctype2) {
     case "doc_func_adm": 
      var newtype = "documento-admissao-funcionario";

I don't think doctype2 is ever encountering a case where it is "doc_func_adm";

If this case happened at least once, the variable newtype would contain "documento-admissao-funcionario" and would not be undefined.

Community
  • 1
  • 1
  • 1
    I found out what was the matter for the first switch, I just don't understand exactly the reason it was happening. For some reason that I don't know, the declared doctype2 which is the the term being tested on the switch function wasn't being seen as string. I solved it by using: switch (String(doctype2)) { Source: Switch case as string My issue now is the second switch function as even with the string transformation it's not properly working. It leads me to ask the gurus if it's possible to use switch function inside another switch before the former breaks. – augustus182l Oct 06 '15 at 20:08
  • 1
    @augustus182l - http://jsfiddle.net/y89qjb2h/ though nested switch statements work, I'd suggest a different approach to solving this problem. Perhaps splitting it into functions, 'getNewType(input)' & 'getNewType2(input)'; – Fabian Valle Oct 07 '15 at 23:09