Days of the week are represented as three-letter strings ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"). Write a javaScript function solution that, given a string S representing the day of the week and an integer K (between 0 and 500), returns the day of the week that is K days later. For example, given S = "Wed" and K = 2, the function should return "Fri". Given S = "Sat" and K = 23, the function should return "Mon".

Respuesta :

Answer:

function getDay(s, k){

var weekDays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

var index=weekDays.findIndex(function(weekDay){return weekDay==s;});

return weekDays[(index+k)%7];

}

Explanation:

//this functions receive the next parameters:

//s which is the day of the week

//k the number of days after s week day

function getDay(s, k){

var weekDays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

//variable index is the index in list weekDays of s

var index=weekDays.findIndex(function(weekDay){return weekDay==s;});

//use module operator % to get the remainder of the division (index+k)/7

//return the string value of the day

return weekDays[(index+k)%7];

}

The program is an illustration of functions.

Functions are used to group blocks of codes in order to perform as one

The JavaScript function, where comments are used to explain each line is as follows:

//This defines the function

function printDay(s, k){  

//This initializes an array that holds the week days

var weekDays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

//This gets the index of day s from the weekDays array

var Sindex = weekDays.indexOf(s);

//This returns the day of the week k days after day s

return weekDays[(Sindex+k)%7];

}

Read more about similar programs at:

https://brainly.com/question/14101896