vendredi 21 octobre 2016

Why string variable is not equal to an element of an Array

function enCode (input) {
    let specialKey = input.shift().toLowerCase();

    let newInput = [];
    for(let i of input){
        newInput.push(i.split(' '));
    }

    let wordsArr = [];
    for(let x=0; x<newInput.length; x++){
        for(let k=0; k<newInput[x].length; k++){
            wordsArr.push(newInput[x][k]);
        }
    }

    let result = [];
    for(let i=0; i<wordsArr.length; i++){
        /*PROBLEM*/if(wordsArr[i].toLowerCase() === specialKey){/*PROBLEM*/
            wordsArr[i+1] = wordsArr[i+1].replace(/[a-zA-Z!%#$]/g, function(value){
                if(value === value.toUpperCase()){
                    value = value.toLowerCase();
                }
                var storage = {
                    '!' : '1',
                    '%' : '2',
                    '$' : '4',
                    '#' : '3'
                }
                return storage[value] || value;
            });
            result.push.apply(result, [wordsArr[i], wordsArr[i+1]]);
        }else{
            result.push(wordsArr[i]);
        }
    }
    return result.join(' ');

}

Why specialKey is not equal with the 4th(5th) element of the Array ? They are both strings right ? Checked typeof of both of them. Itsstring`.Where is the problem can you guys help me? Regards!

Aucun commentaire:

Enregistrer un commentaire