Home > Software engineering >  how should I `extract Sunday` from the week and then enter it in console as the last day of the week
how should I `extract Sunday` from the week and then enter it in console as the last day of the week

Time:10-10

I want to extract sunday from the week and I want it in the console as the last day of the week only after i enter it in the prompt. If someone can please let me know and rectify how to do it.

function days(){
  var daysOfWeek = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
  let currentDay = prompt("Enter day of week:");
  let dayNum = daysOfWeek.indexOf(currentDay);
  while (dayNum < 0) {
    currentDay = prompt("Invalid day. Enter day of week:").toLowerCase();
    dayNum = daysOfWeek.indexOf(currentDay);
  }
  
  let daysLeft = daysOfWeek.splice(dayNum 1).concat(daysOfWeek.splice(0, dayNum))
  
  
  console.log(daysLeft);
  }
  days();

CodePudding user response:

I am not sure what you are asking you want to print in output but what I see in the code you are trying to print is how many days are remaining in the week. that you can do with

function days(){
  var daysOfWeek = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
  let currentDay = prompt("Enter day of week:");
  let dayNum = daysOfWeek.indexOf(currentDay);
  while (dayNum < 0) {
    currentDay = prompt("Invalid day. Enter day of week:").toLowerCase();
    dayNum = daysOfWeek.indexOf(currentDay);
  }
  let daysLeft = 6-daysOfWeek.indexOf(currentDay);
  console.log(daysLeft == 0 ? "last day of the week " : daysLeft);    
}
  days();

CodePudding user response:

function days(){
  var daysOfWeek = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
  let currentDay = prompt("Enter day of week:");
  let dayNum = daysOfWeek.indexOf(currentDay);
  while (dayNum < 0) {
    currentDay = prompt("Invalid day. Enter day of week:").toLowerCase();
    dayNum = daysOfWeek.indexOf(currentDay);
  }
  
  let daysLeft = daysOfWeek.splice(dayNum,1)
  daysOfWeek.push(daysLeft[0])
  
  console.log(daysOfWeek);
  }
days();

  • Related