Cattle brush off the net 25 questions (two questions)

48. E-mail string judge
topic Link
https://www.nowcoder.com/practice/c72b2b5472704d4a98597cb74b0257a7?tpId=2&&tqId=10859&rp=1&ru=/activity/oj&qru=/ta/front-end/question-ranking
subject description
to determine whether the input correct mailbox format
input described
mailbox string
output descriptor
true that a correct format
key technology
regex
Title analysis
mailbox regular expression: ^ \ w + ([-. w +) * \ \ w

function isAvailableEmail(sEmail) {
    var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    return reg.test(sEmail);   
}

49. The character string statistical
topic Link
https://www.nowcoder.com/practice/777d0cd160de485cae0b1fd1dd973b44?tpId=2&&tqId=10862&rp=2&ru=/activity/oj&qru=/ta/front-end/question-ranking
Title Description

  1. Statistical frequency of occurrence of each character string, returns an Object, key statistics for the character, value for the frequency of occurrence
  2. Without limiting the order key
  3. Parameter input string is not empty
  4. Ignore whitespace characters

Example 1
Input
'hello world'
output
{h: 1, e: 1 , l: 3, o: 2, w: 1, r: 1, d: 1}

The key technology
string

Topic analysis

  1. Res defined objects.
  2. Determining whether the character is a space;
  3. If it were not spaces, to determine whether there is a character in the res object;
  4. If not, add the attribute res target, while a value of 1;
  5. If present, this object is res 1 attribute values.
function count(str) {
    var res = {};
    for(var i=0;i<str.length;i++){
        if(str[i] != ' '){
            if(!(str[i] in res)){
                res[str[i]] = 1;
            }else{
                res[str[i]]++;
            }
        }
    }
    return res; 
}

  

Guess you like

Origin www.cnblogs.com/liu-xin1995/p/12363357.html
Recommended