How to add hyphen when days of week are concurrent [closed]












-2















Example1 :



daysofweek=['MON','TUE','WED','THU','FRI','SUN'] ;


output should be :



MON-FRI,SUN


Example2 :



daysofweek=['MON',TUE'];


output should be :



MON,TUE









share|improve this question















closed as off-topic by Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo Nov 24 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 5





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 24 '18 at 6:10











  • please try to solve the problem first then asking for the part which you stuck with your code

    – Hoàng Đăng
    Nov 24 '18 at 6:13











  • I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

    – Drew Reese
    Nov 24 '18 at 6:26











  • why should the output be MON-FRI,SUN ?

    – marvinIsSacul
    Nov 24 '18 at 6:36











  • ^ and also why wouldn't ['MON','TUE'] become MON-TUE

    – JasonB
    Nov 24 '18 at 6:37
















-2















Example1 :



daysofweek=['MON','TUE','WED','THU','FRI','SUN'] ;


output should be :



MON-FRI,SUN


Example2 :



daysofweek=['MON',TUE'];


output should be :



MON,TUE









share|improve this question















closed as off-topic by Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo Nov 24 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 5





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 24 '18 at 6:10











  • please try to solve the problem first then asking for the part which you stuck with your code

    – Hoàng Đăng
    Nov 24 '18 at 6:13











  • I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

    – Drew Reese
    Nov 24 '18 at 6:26











  • why should the output be MON-FRI,SUN ?

    – marvinIsSacul
    Nov 24 '18 at 6:36











  • ^ and also why wouldn't ['MON','TUE'] become MON-TUE

    – JasonB
    Nov 24 '18 at 6:37














-2












-2








-2








Example1 :



daysofweek=['MON','TUE','WED','THU','FRI','SUN'] ;


output should be :



MON-FRI,SUN


Example2 :



daysofweek=['MON',TUE'];


output should be :



MON,TUE









share|improve this question
















Example1 :



daysofweek=['MON','TUE','WED','THU','FRI','SUN'] ;


output should be :



MON-FRI,SUN


Example2 :



daysofweek=['MON',TUE'];


output should be :



MON,TUE






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 6:21









Foo

1




1










asked Nov 24 '18 at 6:09









harryharry

32




32




closed as off-topic by Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo Nov 24 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo Nov 24 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Bhojendra Rauniyar, lagom, stdob--, Hassan Imam, Foo

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 5





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 24 '18 at 6:10











  • please try to solve the problem first then asking for the part which you stuck with your code

    – Hoàng Đăng
    Nov 24 '18 at 6:13











  • I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

    – Drew Reese
    Nov 24 '18 at 6:26











  • why should the output be MON-FRI,SUN ?

    – marvinIsSacul
    Nov 24 '18 at 6:36











  • ^ and also why wouldn't ['MON','TUE'] become MON-TUE

    – JasonB
    Nov 24 '18 at 6:37














  • 5





    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

    – CertainPerformance
    Nov 24 '18 at 6:10











  • please try to solve the problem first then asking for the part which you stuck with your code

    – Hoàng Đăng
    Nov 24 '18 at 6:13











  • I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

    – Drew Reese
    Nov 24 '18 at 6:26











  • why should the output be MON-FRI,SUN ?

    – marvinIsSacul
    Nov 24 '18 at 6:36











  • ^ and also why wouldn't ['MON','TUE'] become MON-TUE

    – JasonB
    Nov 24 '18 at 6:37








5




5





The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

– CertainPerformance
Nov 24 '18 at 6:10





The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.

– CertainPerformance
Nov 24 '18 at 6:10













please try to solve the problem first then asking for the part which you stuck with your code

– Hoàng Đăng
Nov 24 '18 at 6:13





please try to solve the problem first then asking for the part which you stuck with your code

– Hoàng Đăng
Nov 24 '18 at 6:13













I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

– Drew Reese
Nov 24 '18 at 6:26





I think, by definition, days of the week can't be concurrent. Contiguous perhaps.

– Drew Reese
Nov 24 '18 at 6:26













why should the output be MON-FRI,SUN ?

– marvinIsSacul
Nov 24 '18 at 6:36





why should the output be MON-FRI,SUN ?

– marvinIsSacul
Nov 24 '18 at 6:36













^ and also why wouldn't ['MON','TUE'] become MON-TUE

– JasonB
Nov 24 '18 at 6:37





^ and also why wouldn't ['MON','TUE'] become MON-TUE

– JasonB
Nov 24 '18 at 6:37












2 Answers
2






active

oldest

votes


















0














var oneDArray = function(weekArray){

var week = ;
for(var i=0; i< weekArray.length;i++){
var conDays = weekArray[i];
if((sorter[conDays[1]] - sorter[conDays[0]]) === 0 ){
week[i] = conDays[0];
}
else if((sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){
week[i] = conDays;
}else{
week[i] = conDays.join("-");
}
}
return week.join();
};

var convertToWeekRanges = function (week) {
var weekRanges =
weekRanges[0] = [week[0], week[0]];
var lastIndex = 0;

for (var i = 1; i < week.length; i++) {

if ((sorter[week[i]] - sorter[weekRanges[lastIndex][1]]) === 1) {
weekRanges[lastIndex][1] = week[i];
} else {
weekRanges[++lastIndex] = [week[i], week[i]];
}
}
return weekRanges;
};

console.log(oneDArray(convertToWeekRanges(['MON','TUE'] )));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','SAT','SUN'])));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','FRI','SUN'])));





share|improve this answer


























  • my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

    – harry
    Nov 24 '18 at 7:29













  • var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

    – harry
    Nov 24 '18 at 11:50













  • As you see MON-TUE is consecutive. thats why you are getting this output.

    – saurav omar
    Nov 24 '18 at 12:20











  • consuective should be consider more than 2 length

    – harry
    Nov 24 '18 at 12:34











  • Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

    – saurav omar
    Nov 24 '18 at 12:49



















0














You can take a reference of all days in week to compare which day is missing and use split and map on missing day like below






let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))








share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 25 '18 at 9:57


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














var oneDArray = function(weekArray){

var week = ;
for(var i=0; i< weekArray.length;i++){
var conDays = weekArray[i];
if((sorter[conDays[1]] - sorter[conDays[0]]) === 0 ){
week[i] = conDays[0];
}
else if((sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){
week[i] = conDays;
}else{
week[i] = conDays.join("-");
}
}
return week.join();
};

var convertToWeekRanges = function (week) {
var weekRanges =
weekRanges[0] = [week[0], week[0]];
var lastIndex = 0;

for (var i = 1; i < week.length; i++) {

if ((sorter[week[i]] - sorter[weekRanges[lastIndex][1]]) === 1) {
weekRanges[lastIndex][1] = week[i];
} else {
weekRanges[++lastIndex] = [week[i], week[i]];
}
}
return weekRanges;
};

console.log(oneDArray(convertToWeekRanges(['MON','TUE'] )));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','SAT','SUN'])));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','FRI','SUN'])));





share|improve this answer


























  • my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

    – harry
    Nov 24 '18 at 7:29













  • var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

    – harry
    Nov 24 '18 at 11:50













  • As you see MON-TUE is consecutive. thats why you are getting this output.

    – saurav omar
    Nov 24 '18 at 12:20











  • consuective should be consider more than 2 length

    – harry
    Nov 24 '18 at 12:34











  • Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

    – saurav omar
    Nov 24 '18 at 12:49
















0














var oneDArray = function(weekArray){

var week = ;
for(var i=0; i< weekArray.length;i++){
var conDays = weekArray[i];
if((sorter[conDays[1]] - sorter[conDays[0]]) === 0 ){
week[i] = conDays[0];
}
else if((sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){
week[i] = conDays;
}else{
week[i] = conDays.join("-");
}
}
return week.join();
};

var convertToWeekRanges = function (week) {
var weekRanges =
weekRanges[0] = [week[0], week[0]];
var lastIndex = 0;

for (var i = 1; i < week.length; i++) {

if ((sorter[week[i]] - sorter[weekRanges[lastIndex][1]]) === 1) {
weekRanges[lastIndex][1] = week[i];
} else {
weekRanges[++lastIndex] = [week[i], week[i]];
}
}
return weekRanges;
};

console.log(oneDArray(convertToWeekRanges(['MON','TUE'] )));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','SAT','SUN'])));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','FRI','SUN'])));





share|improve this answer


























  • my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

    – harry
    Nov 24 '18 at 7:29













  • var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

    – harry
    Nov 24 '18 at 11:50













  • As you see MON-TUE is consecutive. thats why you are getting this output.

    – saurav omar
    Nov 24 '18 at 12:20











  • consuective should be consider more than 2 length

    – harry
    Nov 24 '18 at 12:34











  • Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

    – saurav omar
    Nov 24 '18 at 12:49














0












0








0







var oneDArray = function(weekArray){

var week = ;
for(var i=0; i< weekArray.length;i++){
var conDays = weekArray[i];
if((sorter[conDays[1]] - sorter[conDays[0]]) === 0 ){
week[i] = conDays[0];
}
else if((sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){
week[i] = conDays;
}else{
week[i] = conDays.join("-");
}
}
return week.join();
};

var convertToWeekRanges = function (week) {
var weekRanges =
weekRanges[0] = [week[0], week[0]];
var lastIndex = 0;

for (var i = 1; i < week.length; i++) {

if ((sorter[week[i]] - sorter[weekRanges[lastIndex][1]]) === 1) {
weekRanges[lastIndex][1] = week[i];
} else {
weekRanges[++lastIndex] = [week[i], week[i]];
}
}
return weekRanges;
};

console.log(oneDArray(convertToWeekRanges(['MON','TUE'] )));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','SAT','SUN'])));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','FRI','SUN'])));





share|improve this answer















var oneDArray = function(weekArray){

var week = ;
for(var i=0; i< weekArray.length;i++){
var conDays = weekArray[i];
if((sorter[conDays[1]] - sorter[conDays[0]]) === 0 ){
week[i] = conDays[0];
}
else if((sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){
week[i] = conDays;
}else{
week[i] = conDays.join("-");
}
}
return week.join();
};

var convertToWeekRanges = function (week) {
var weekRanges =
weekRanges[0] = [week[0], week[0]];
var lastIndex = 0;

for (var i = 1; i < week.length; i++) {

if ((sorter[week[i]] - sorter[weekRanges[lastIndex][1]]) === 1) {
weekRanges[lastIndex][1] = week[i];
} else {
weekRanges[++lastIndex] = [week[i], week[i]];
}
}
return weekRanges;
};

console.log(oneDArray(convertToWeekRanges(['MON','TUE'] )));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','SAT','SUN'])));

console.log(oneDArray(convertToWeekRanges(['MON','TUE','WED','THU','FRI','SUN'])));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 14:44

























answered Nov 24 '18 at 7:20









saurav omarsaurav omar

767




767













  • my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

    – harry
    Nov 24 '18 at 7:29













  • var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

    – harry
    Nov 24 '18 at 11:50













  • As you see MON-TUE is consecutive. thats why you are getting this output.

    – saurav omar
    Nov 24 '18 at 12:20











  • consuective should be consider more than 2 length

    – harry
    Nov 24 '18 at 12:34











  • Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

    – saurav omar
    Nov 24 '18 at 12:49



















  • my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

    – harry
    Nov 24 '18 at 7:29













  • var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

    – harry
    Nov 24 '18 at 11:50













  • As you see MON-TUE is consecutive. thats why you are getting this output.

    – saurav omar
    Nov 24 '18 at 12:20











  • consuective should be consider more than 2 length

    – harry
    Nov 24 '18 at 12:34











  • Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

    – saurav omar
    Nov 24 '18 at 12:49

















my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

– harry
Nov 24 '18 at 7:29







my question is how to add hyphen when iterate days of week are concurrent.MON-FRI,SUN. if there is no concurrent weeks and that will displayed as comma separated values.

– harry
Nov 24 '18 at 7:29















var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

– harry
Nov 24 '18 at 11:50







var daysofweek=['MON','TUE']; output is now MON-TUE : but expected is MON,TUE

– harry
Nov 24 '18 at 11:50















As you see MON-TUE is consecutive. thats why you are getting this output.

– saurav omar
Nov 24 '18 at 12:20





As you see MON-TUE is consecutive. thats why you are getting this output.

– saurav omar
Nov 24 '18 at 12:20













consuective should be consider more than 2 length

– harry
Nov 24 '18 at 12:34





consuective should be consider more than 2 length

– harry
Nov 24 '18 at 12:34













Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

– saurav omar
Nov 24 '18 at 12:49





Update function -> var oneDArray = function(weekArray){ var week = ; for(var i=0; i< weekArray.length;i++){ var conDays = weekArray[i]; console.log(sorter[conDays[1]] - sorter[conDays[0]]); if(conDays.length == 1 || (sorter[conDays[1]] - sorter[conDays[0]]) === 1 ){ week[i] = conDays; }else{ week[i] = conDays.join("-"); } } return week.join(); };

– saurav omar
Nov 24 '18 at 12:49













0














You can take a reference of all days in week to compare which day is missing and use split and map on missing day like below






let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))








share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 25 '18 at 9:57
















0














You can take a reference of all days in week to compare which day is missing and use split and map on missing day like below






let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))








share|improve this answer


























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 25 '18 at 9:57














0












0








0







You can take a reference of all days in week to compare which day is missing and use split and map on missing day like below






let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))








share|improve this answer















You can take a reference of all days in week to compare which day is missing and use split and map on missing day like below






let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))








let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))





let daysofweek1 = ['MON','TUE','WED', 'THU','FRI','SUN'] ;

let daysofweek2 = ['MON','WED','THU','FRI','SUN'] ;

let daysofweek3 = ['TUE','WED'];

let allWeeks = ['MON','TUE','WED','THU','FRI','SAT', 'SUN']

function getCombinedDays(arr) {
return allWeeks.map(d => arr.some(v => v == d) ? d : '-')
.join('')
.split('-')
.flatMap(d => d.length > 3 ? (d.length == 6 ? [d.slice(0,3), d.slice(-3)] : d.slice(0,3) + '-' + d.slice(-3)) : d)
.filter(d => d)
}

console.log(getCombinedDays(daysofweek1))

console.log(getCombinedDays(daysofweek2))

console.log(getCombinedDays(daysofweek3))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 12:06

























answered Nov 24 '18 at 9:11









Nitish NarangNitish Narang

2,9401815




2,9401815













  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 25 '18 at 9:57



















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 25 '18 at 9:57

















Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 25 '18 at 9:57





Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 25 '18 at 9:57



Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'