G
G
genrixxx2021-10-24 10:45:49
JavaScript
genrixxx, 2021-10-24 10:45:49

How to output an empty string?

If you type node file.js <blank string> into the terminal (i.e., don't type anything), you will get an error:

for(let l=str_min.length; l>0; l--){
                      ^
TypeError: Cannot read property 'length' of undefined

How can I make it so that if nothing is entered, it will simply return an empty string?

The program itself:

function search_largest_substr(){
    let str_min = arguments[0];
    const list = [];
    for(let n=1; n<arguments.length; n++){
        if(str_min.length<arguments[n].length){
            list.push(arguments[n]);
            continue;
        }
        list.push(str_min);
        str_min = arguments[n];
    }
    for(let l=str_min.length; l>0; l--){
        for(let p=0; p<=str_min.length-l; p++){
            const substr = str_min.slice(p, p+l);
            let isFound = true;
            for(let i=0; i<list.length; i++){
                if(	list[i].indexOf(substr) >= 0)
                    continue;
                isFound=false;
                break;
            }
            if( isFound )
                return substr;
        }
    }
    return "";
}
console.log(search_largest_substr.apply(this,process.argv.slice(2)));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-10-24
@sergiks

let str_min = arguments[0] || ""; // по умолчанию пустая строка

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question