2017-02-01 43 views
-3

我是编程新手,这个问题在编码训练营的入学考试中。我很好奇答案:测试一个字符串是否以另一个字符串开头,在JavaScript中

编写一个方法not_string,它接受一个字符串并返回该字符串,其前缀为“not”,除非原始字符串已经以全字“not”开头。

例如:

not_string("Hi, this is a string") 
#=> "not Hi, this is a string" 

not_string("not a string here") 
#=> "not a string here" 

not_string("nothing strange about this one") 
#=> "not nothing strange about this one" 

回答

0

这应该是它:

var not_string = function(input) { 
    if(typeof input !== 'string') { 
    return 'please enter a string'; 
    } 
    if(input.indexOf('not') === 0) { 
    return input; 
    } 
    return 'not ' + input; 
} 
相关问题