如何从字母分配的数字中获取两位数字

时间:2017-07-12 07:15:27

标签: javascript node.js

朋友们,我的代码来自其他开发者。

但是我想制作一个这样的函数,但是我得到一个Y未定义的错误

function nameNum(){

    A = I = J = Q = Y = 1;
    B = K = R = 2;
    C = G = L = S = 3;
    D = M = T = 4;
    E = H = N = X = 5;
    U = V = W = 6;
    O = Z = 7;
    F = P = 8;

    var text = "BANISETTINIRANJAN";
    text = text.replace(/[^a-z]/gi, '');
    text = text.toUpperCase();
    var letters = text.split("")

    var num = (letters.reduce(function(prev, curr) {
        return prev + window[curr];
    }, 0));
    return nameNum();

但我得到的错误就像Y没有定义

2 个答案:

答案 0 :(得分:0)

回复电话错误且功能未正常关闭

  1. 请勿使用return
  2. 在函数内调用相同的函数
  3. 函数Y内部定义了一个
  4. <强>更新

    使用函数发送名称

    function nameNum(name) {
    
     A = I = J = Q = Y = 1;
      B = K = R = 2;
      C = G = L = S = 3;
      D = M = T = 4;
      E = H = N = X = 5;
      U = V = W = 6;
      O = Z = 7;
      F = P = 8;
    console.log('y is '+Y)
      var text = name;
      text = text.replace(/[^a-z]/gi, '');
      text = text.toUpperCase();
      var letters = text.split("")
    
      var num = (letters.reduce(function(prev, curr) {
        return prev + window[curr];
      }, 0));
      
      return num;
     
    }
    
    console.log(nameNum('bani'))

    使用githup链接解决方案

    进行了更新

    只需删除var

    即可

    var mennt = 'bani'; 
    
    function nameNum(name) {
     A = I = J = Q = Y = 1; //remove the var in this line 
      B = K = R = 2;
      C = G = L = S = 3;
      D = M = T = 4;
      E = H = N = X = 5;
      U = V = W = 6;
      O = Z = 7;
      F = P = 8;
    
      var text = name;
      text = text.replace(/[^a-z]/gi, '');
      text = text.toUpperCase();
      var letters = text.split("")
    
      var num = (letters.reduce(function(prev, curr) {
        return prev + window[curr];
      }, 0));
    
      return num;
    
    }
    
    var naNumber = nameNum(mennt);
    console.log(naNumber);

答案 1 :(得分:0)

function nameNum() {
                var x = {A:1, I:1, J:1, Q:1, Y:1, B:2, K:2, R:2, C:3, G:3, 
                L:3, S:3, D:4, M:4, T:4, E:5, H:5, N:5, X:5, U:6, V:6, W:6, 
                O:7, Z:7, F:8, P:8};

                var name = "Bani Naveen";
                var nameScore = 0;
                var name = name.replace(/[^a-z]/gi, '');
                    name = name.toUpperCase();
                    var letters = name.split("")

                for( var i = 0; i < letters.length; i++ )
                {
                    var curChar = name.charAt( i );
                    var curValue = x[ curChar ];
                    nameScore = nameScore + curValue;
                }//for()

                console.log("your name number will be" + nameScore);
                return nameScore;
             }