代码之家  ›  专栏  ›  技术社区  ›  user944513

如何在javascript中返回字符串而不是布尔值

  •  0
  • user944513  · 技术社区  · 6 年前

    给定一个字符串和一个单词词典,找出输入字符串是否可以分解为一个或多个字典单词的空格分隔序列。

    dictionary = ["I" , "have", "Jain", "Sumit", "am", "this", "dog"]
    
    String = "IamSumit"
    
    Output: "I am Sumit"
    
    String ="thisisadog"
    
    Output : String can't be broken   
    

    我的职能是给我 boolean 输出而不是字符串 result 我试过这样做 return result; . 仍然返回布尔值

    const find = (S,dic ,result) =>{
            if (S.length === 0) {
                console.log(result,'kkkk')
                return result;
            }else {
                let index = 0,
                    word = "";
    
                while (index < S.length){
                    word += S.charAt(index);
                    if (dic.indexOf(word) !==-1) {
                        if (find(S.substring(index + 1), dic, result + word + " ")) {
                            return true;
                        } else {
                            //System.out.println(word + "  backtrack");
                            index++;
                        }
                    }else{
                        index++;
                    }
                }
            }
    
            return false
        }
    
        console.log(find("thisisadog",["I" , "have", "Jain", "Sumit", "am", "this", "dog"],''))
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Ahmed Hammad    6 年前

    我理解您需要做什么,但我不太理解您的代码是如何工作的,所以我实现了我的版本。

    const splitStringFromDict = (str, dict) => {
        let index = 0, word = "", result = [];
        while (index < str.length) {
            word += str.charAt(index);
            if (dict.indexOf(word) !== -1) {
                result.push(word);
                word = ""
            }
            index++;
        }
        if (word !== "") result.push(word);
    
        for (word of result) {
            if (dict.indexOf(word) === -1){
                return "String can not be broken";
            }
        }
        return result.join(' ');
    }
    

    它不是一个递归函数,我认为它更容易理解。

    现在,我可能应该强调的是,用中现有函数的相同名称命名函数 Array.prototype ,根本不是一个好的做法。 here

        2
  •  0
  •   Abdul Moeez    6 年前

    有些错误类似于使用(返回true/false)而不是字符串值返回。除了在

     if (S.length === 0) {
      console.log(result,'kkkk')
      return result;
            } 
    

    const find = (S,dic ,result,broken) =>{
        if (S.length === 0) {
            console.log(result)
            return result;
        }
    
        else {
            let index = 0,
                word = "";
    
            while (index < S.length){
                word += S.charAt(index);
                if (dic.indexOf(word) !==-1) {
                    if (find(S.substring(index + 1), dic, result + word + " ")) {
                        return result;
    
                    } else {
                        //System.out.println(word + "  backtrack");
                        index++;
                    }
                }else{
                    index++;
                }
            }
        }
        return broken;
    }
    
    console.log(find("IamSumit",["I" , "have", "Jain", "Sumit", "am", "this", "dog"],'',`String can't be broken`))
    

    输出:

    String = "IamSumit"
    
    Output: "I am Sumit"
    
    String ="thisisadog"
    
    Output : 'String can`t be broken'