augustus182l
augustus182l

Reputation: 385

Any particular reason why this switch returns undefined?

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.

Upvotes: 1

Views: 1265

Answers (1)

Fabian Valle
Fabian Valle

Reputation: 81

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.

Upvotes: 1

Related Questions