I am making a function that returns greatest mmon prefix. When input is: ["flower","flow","flight"],
output should be: "fl"
.
But there is an exception, when input is an empty array the output should be an empty string. This is where my function is not working.
var longestCommonPrefix = function(strs) {
let prefix = '';
let word = '';
for (let i = 0; i < strs.length; i++){
for(let j = i + 1; j < strs.length; j++) {
for (let k = 0; strs[i][k] === strs[j][k]; k++) {
prefix += strs[i][k];
//console.log(prefix);
}
prefix += ',';
//console.log(prefix);
}
}
prefix = prefix.split(',').sort().slice(1);
console.log(prefix);
// if(prefix[0].length === 0) {
// return "";
// }
return prefix[0];
};
console.log(longestCommonPrefix(["flower","flow","flight"]));
I am making a function that returns greatest mmon prefix. When input is: ["flower","flow","flight"],
output should be: "fl"
.
But there is an exception, when input is an empty array the output should be an empty string. This is where my function is not working.
var longestCommonPrefix = function(strs) {
let prefix = '';
let word = '';
for (let i = 0; i < strs.length; i++){
for(let j = i + 1; j < strs.length; j++) {
for (let k = 0; strs[i][k] === strs[j][k]; k++) {
prefix += strs[i][k];
//console.log(prefix);
}
prefix += ',';
//console.log(prefix);
}
}
prefix = prefix.split(',').sort().slice(1);
console.log(prefix);
// if(prefix[0].length === 0) {
// return "";
// }
return prefix[0];
};
console.log(longestCommonPrefix(["flower","flow","flight"]));
Share
Improve this question
edited Jun 5, 2023 at 12:38
Reporter
3,9365 gold badges35 silver badges49 bronze badges
asked May 7, 2020 at 17:13
user10321333user10321333
3
- 5 show full code please – brk Commented May 7, 2020 at 17:14
- 1 my guess is that you have a callback function and you return a value inside of it, but its just a guess. show us your code – Ilijanovic Commented May 7, 2020 at 17:18
- Posted the code @brk – user10321333 Commented May 7, 2020 at 21:13
6 Answers
Reset to default 3Following should work:
function returnEmptyIfNull(arr) {
if(!arr?.length)
return "";
return "not empty";
}
Check the array for null
and a size of 0
. If either of these conditions are true
, return an empty string.
function joinItems(items) {
return items == null || items.length === 0 ? '' : items.join(', ');
}
console.log(joinItems(['Hello', 'World'])); // "Hello, World"
console.log(joinItems([])); // ""
You can simplify this the the following expression:
const joinItems = (items = []) => items?.join(', ') ?? '';
console.log(joinItems(['Hello', 'World'])); // "Hello, World"
console.log(joinItems()); // ""
return ""
will return an empty string. If the function is returning undefined, there's something else wrong with your function.
function fn () {
return "";
}
console.log(typeof fn());
// 'string'
function check(param){
if (param.length == 0){
return "";
}
}
console.log(check([]));
Works fine for me, check if the array is empty by checking if the length is 0. You are getting undefined because your ' return "" ' line is not being run.
You need to check that your input is an array, then check that its length is 0.
function checkArrayIsEmpty(arr) {
if(Array.isArray(arr) && arr.length === 0) {
return "";
} else {
return "not an empty array";
}
}
if (strs.length==1)
return strs[0]
This worked for me.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744874749a4598480.html
评论列表(0条)