Javascript Values for letters

百般思念 提交于 2020-01-02 00:33:11

问题


I am developing an app in Phonegap.. My app is based on numerology calculation, In my app there is a portion where I need to calculate the value of letters. For example my name 'Aron' .. I need to calculate the values, I am using switch case statement for that but it is not working.. can anyone pls help me to find out the error..I am giving my code below.

//curage is current age //fna is firstname

var bbbb;


 for(var i=0; i<curage; i++) 
  {
    bbbb+=lettervalue(fna.charAt(i));

  }

function lettervalue(var g)
{

var cccc;
switch(g)
        {
           case a: cccc="1";           
           break;           
           case b: cccc="2";
           break;
           case c: cccc="3";
           break;
           case d: cccc="4";
           break;
           case e: cccc="5"; 
            break;     
           case f: cccc="6"; 
            break;         
            //till z
        }

return cccc;
}




document.getElementById("hbt").value=cccc;

回答1:


@Jocheved you switch case statment should be like this.. inside argument dont use var

function lettervalue(g)
{
switch(g)
        {
           case "a": return 1;                    
           case "b": return 2;          
           case "c": return 3;           
           case "d": return 4;           
           case "e": return 5;                  
           case "f": return 6;                        
           default : return 0; 

        }

}

and change the last line like this..instead of 'cccc' change to 'bbbb'

document.getElementById("hbt").value=bbbb;



回答2:


At first in terms of function signature, you can't use var inside the arguments.

function lettervalue(var g)

Need to be

function lettervalue(g)

Then inside your switch case, you need to have String

  case "a": 
           cccc="1";           
           break;   



回答3:


As you are using the cases with strings, your letters must be between "

case "a": cccc="1";           
       break;   

In javascript you dont declare a variable as an argument function:

function name(argument){   <--- not (var argument)
     code
}


来源:https://stackoverflow.com/questions/29671625/javascript-values-for-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!