Five Consecutive days excluding weekends - javascript

I have written a code that raises a flag when number of leaves taken by a person exceeds 5 business days. However, it doesn't raise the flag when we have weekend in middle. Say, someone takes leave on 23,24 and then on 27,28 and 29. As 25 and 26 are weekends, tool doesn't count it. Can someone help me on how i must check the weekend dates here and push the value as "Yes" that includes weekend dates ?
function PrepareReport(reportData) {
var vacationData = [];
var dayValuesStr = '';
var dayValuesArray = [];
if ($("#ddlfromYear").val() == $("#ddltoYear").val()) {
count = parseInt($("#ddltoMonth").val()) - parseInt($("#ddlfromMonth").val());
}
else {
count = 12 - parseInt($("#ddlfromMonth").val()) + parseInt($("#ddltoMonth").val());
}
//val.ResourceID FullName EnterpriseID WorkOrder Domain Totalhours
vacationData.push(reportData.FullName);
vacationData.push(reportData.EnterpriseID);
vacationData.push(reportData.WorkOrder);
vacationData.push(reportData.Domain);
if (reportData.IsOffshore == 1) {
vacationData.push('Offshore');
}
else {
vacationData.push('Onshore');
}
vacationData.push(reportData.TOTALHOURS);
var counter = 0;
FlagCounterLastVal = 0;
vacationData.push("No");
for (var monthNum = 0; monthNum <= count; monthNum++) {
dayValuesStr = reportData['MONTH' + monthNum];
if (dayValuesStr) {
dayValuesArray = dayValuesStr.split(',');
var countArray = dayValuesArray.length - 1;
$.each(dayValuesArray, function (key, val) {
if (val.endsWith('.00'))//round off values
{
if (parseInt(val) == 0) {
vacationData.push('-');
counter = 0; // resetting counter to 0 for non-consecutive days
}
else {
if (FlagCounterLastVal > 0) {
counter = FlagCounterLastVal;
}
counter++;
vacationData.push(parseInt(val));
****if (counter >= 5 && FlagCounterLastVal == 0) {
var index = vacationData.indexOf("No");
vacationData[index] = "Yes";
}****
if (key == (countArray) && count > 0) { // vacation taken at the month ends
FlagCounterLastVal = counter;
}
}
}
else {
vacationData.push(val);
}
});
}
}
return vacationData;
}

You can use getDay for that.
var day = yourDateObject.getDay();
var isWeekend = (day === 6) || (day === 0);
6 = Saturday, 0 = Sunday

I won't be sharing code here as it's Data Structure and would like you to think a little bit, and try yourself first. If still you won't be able to code it then I will help you out in your code.
What I would do is have 2 arrays, both will be sorted this will help in search.
1. Having all weekends dates. // weekendArr
2. Leave dates taken by employee. //leaveArr
Steps
1. delete all the weekendArr dates from leaveArr.
2. Check if leaveArr have continues dates more then 5 or what ever you want it to be.
3. If it is greater then 5 then raise a flag for that employee.
Now you need to decide what data structure you will use to maintain employee leaves, employeeID, and leave flag.
Hope this will be enough to figure out code now.

Related

snail in the well javascript challenge

This is my first question on stack overflow although this question had been answered before I didn't get enough details to understand why the code was written that way and I didn't just want to copy and paste the solution without understanding it.
The snail climbs 7 feet each day and slips back 2 feet each night, How many days will it take the snail to get out of a well with the given depth?
Sample Input
31
Sample Output
6
this is was what i wrote but it didn't work
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
let climb = 0, days = 0;
for(climb + 7; climb < depth; days++){
climb += 2;
console.log(days);
try this:
var depth = parseInt(readline(), 10);
var day = 0;
for(let climb = 0; climb <= depth;) {
day +=1;
climb += 7;
if(climb >= depth) {
break;
}
climb -= 2;
}
console.log(day);
Just take input and write this
var day= Math.ceil((depth-2)/5);
and output that!
/* day --> 7++
night --> 2-- */
var day = 0;
var total = 0;
var input = 41;
while (input>total){
day++;
total+=7;
if (total>=input){
console.log(day);
break;
}
total = total -2
}
As mentioned in the comments, no need to loop. Just work out the math of the problem an use that.
function snailCalc (depth, dailyGain, nightlyLoss) {
var days = 1;
var netGain = dailyGain-nightlyLoss;
if(depth > dailyGain ) {
//Basic calc based on net gain taking the first day into account
days = (depth-dailyGain)/netGain;
//Add the fist day
days += 1;
//We want whole days so use Mathc.ceil
days = Math.ceil(days)
//Or we could do all that in one line
//days = Math.ceil(((depth-dailyGain)/netGain) + 1);
}
return days;
}
const gain = 7;
const loss = 2
for(var d = 1; d < 31; d++)
{
console.log(`Depth: ${d}, Days: ${snailCalc(d, gain, loss)}` )
}
Bob
function main() {
var depth = parseInt(readLine(), 10);
console.log(Math.round(depth / 5))
}
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var days=1;
var level =0;
for(level =0;level<depth;days++){
level+=7
if(level<depth){
level-=2;
} else{
break ;
}
}console.log(days)
}
Try this, I had the same trouble a couple days ago and we find the error is that using js you need to reset the variable where you sum the result before evaluate again if the distance the snail climbed that day is greater than the depth to end the clycle.
depth = 31;
let days = 0;
let climb = 0;
while(climb < depth){
let result = climb + 7;
if(result >= depth){
days++;
break;
}
climb = result - 2;
days++;
//console.log("climb ",climb);
}
console.log(days);
You can change the function input and test the snipet:
for more you can run code and check result ↗
Eg : main(128) // 26
function main(input) {
let depth = input
let climbsUp = 7
let slipsBack = 2
let distance = climbsUp - slipsBack
let days = 0;
let rDepth = Math.round(depth / distance)
for (let i = 0; i < rDepth; i++) {
distance = distance + 5
days = ++days
if (days === 6) {
console.log('days will it take the snail to get out of a well : ' + rDepth)
} else {
console.log('days will it take the snail to get out of a well : ' + rDepth)
break;
}
}
}
main(42);
main(128);

Set condition based on inner text of node?

(function() {
var i=0;
var f=function() {
let l=document.querySelector("*[data-control-name=withdraw_single]");
if (!!l) {
setTimeout(function() {
l.click();
}, 100);
setTimeout(function() {
document.querySelector(".artdeco-modal .artdeco-button--primary").click();
},1500);
setTimeout(function() {
f();
},2500);
}
};
f();
})()
The above javascript bookmarklet works well for automating the invitation withdraw process on LinkedIn. However, there is a value in this element that I am hoping to factor in:
<time class="time-badge time-ago">
1 day ago
</time>
Any idea on how to add a condition to only move forward with the process ONLY if this 'time-ago' value is greater than [X] days ago, otherwise STOP the process just provide an alert like 'All invites older than [X] days ago withdrawn.'
Ciao, you could try to do this:
...
let val = parseInt(document.querySelector(".time-badge").innerText.split(' ')[0]);
if (val > [X]){
// go forward
}
else {
// stop
alert('All invites older than [X] days ago withdrawn.');
}
var str = "<time class=\"time-badge time-ago\"> 1 day ago </time>";
var patt = /\> (.*?) day ago \<\/time>/i;
var result = str.match(patt);
if(result !== undefined && result !== null){
var day = parseInt(result[1]);
if(day <= 10){
// include results upto 10 days
} else {
// exclude results
}
}

Counter with different settings for different days of the week

I need a counter which will count up numbers with different increase per tick depending on day of the week.
I found this counter: https://stackoverflow.com/a/3346311 which is counting the way I need but now I don't know how to make it to change INCREMENT value depending on day of the week.
Sorry for my poor english, hope question is understandable.
Regards
Thanks a lot Guys for all answers, I'm very impressed.
Date.getDay() returns the current day of the week starting at 0 (sunday)
var INCREMENT;
var dayOfWeek = new Date().getDay();
switch(dayOfWeek){
case 0: //Sunday
INCREMENT = 2; //Add your number
break;
case 1: //Monday
INCREMENT = 3; //Add your number
break;
//...
case 6: //Saturday
INCREMENT = 5; //Add your number
break;
}
Or you could do it like this
var daysToIncrementValues = {0: 5, 1:4, 2:3, 3:2, 4:1, 5:9, 6:7} // the values assigned are random here, you can assign whatever value
var todaysIncrementValue = dayToIncrement(new Date().getDay())
More concise, and no switch.
You should use setInterval() for this. And inside your function block, declare some conditionals, where you increment the count a different value depending on the day of the week.
To find the day of the week, use the new Date().getDay() method, which will return a number from 0 through 6, depending on the day.
var date = document.getElementById("date");
var count = 0;
setInterval(function() {
if (new Date().getDay() === 0) { // On Sunday's, increment by 1
count += 1;
}
else if (new Date().getDay() === 1) { // On Monday's, increment by 2
count += 2;
}
else if (new Date().getDay() === 2) { // On Tuesday's, increment by 3
count += 3;
}
else if (new Date().getDay() === 3) { // On Wednesday's, increment by 4
count += 4;
}
else if (new Date().getDay() === 4) {
count += 5;
}
else if (new Date().getDay() === 5) {
count += 6;
}
else {
count += 7;
}
date.innerHTML = count;
},1000);
Let me explain the above code.
I have an empty paragraph with an id equal to date and I'm referencing it through document.getElementById. I'm initializing a count variable to 0 upon page load.
At the end of my setInterval() loop, I'm adding the result of count to my div. The loop is being run once a second, which is represented by 1,000, which is the number of milliseconds the loop should be run. You can change this to whatever you like.
I just created a fiddle
http://jsfiddle.net/3nawhmdf/
If you want more DRY code, you could try something like this.
var date = document.getElementById("date");
var count = 0;
setInterval(function() {
count += (new Date().getDay() + 1);
date.innerHTML = count;
},1000);
Credit goes to bonesbrigade for this solution

Small Straight (Yahtzee) Algorithm

I have created a working javascript function to check an array of 5 numbers for a small straight, in a Yahtzee game I'm making. I've tested it to no end and I'm confident it works 100% of the time, but it is also probably the worst algorithm of all time in terms of being efficient. Here is what it looks like:
function calcSmstraight() {
var sum = 0;
var r = new Array();
var r2 = new Array();
var counter = 0;
var temp;
var bool = false;
var bool2 = false;
r[0] = document.getElementById('setKeep1').value;
r[1] = document.getElementById('setKeep2').value;
r[2] = document.getElementById('setKeep3').value;
r[3] = document.getElementById('setKeep4').value;
r[4] = document.getElementById('setKeep5').value;
// Move non-duplicates to new array
r2[0] = r[0];
for(var i=0; i<r.length; i++) {
for(var j=0; j<r2.length; j++) {
if(r[i] == r2[j]) {
bool2 = true; // Already in new list
}
}
// Add to new list if not already in it
if(!bool2) {
r2.push(r[i]);
}
bool2 = false;
}
// Make sure list has at least 4 different numbers
if(r2.length >= 4) {
// Sort dice from least to greatest
while(counter < r2.length) {
if(r2[counter] > r2[counter+1]) {
temp = r2[counter];
r2[counter] = r2[counter+1];
r2[counter+1] = temp;
counter = 0;
} else {
counter++;
}
}
// Check if the dice are in order
if(((r2[0] == (r2[1]-1)) && (r2[1] == (r2[2]-1)) && (r2[2] == (r2[3]-1)))
|| ((r2[1] == (r2[2]-1)) && (r2[2] == (r2[3]-1)) && (r2[3] == (r2[4]-1)))) {
bool = true;
}
}
if(bool) {
// If small straight give 30 points
sum = 30;
}
return sum;
}
My strategy is to:
1) Remove duplicates by adding numbers to a new array as they occur
2) Make sure the new array is at least 4 in length (4 different numbers)
3) Sort the array from least to greatest
4) Check if the first 4 OR last 4 (if 5 in length) numbers are in order
My question:
Does anyone know a way that I can improve this method? It seems ridiculously terrible to me but I can't think of a better way to do this and it at least works.
Given that you're implementing a Yahtzee game you presumably need to test for other patterns beyond just small straights, so it would be better to create the array of values before calling the function so that you can use them in all tests, rather than getting the values from the DOM elements inside the small straight test.
Anyway, here's the first way that came to my mind to test for a small straight within an array representing the values of five six-sided dice:
// assume r is an array with the values from the dice
r.sort();
if (/1234|2345|3456/.test(r.join("").replace(/(.)\1/,"$1") {
// is a small straight
}
Note that you can sort an array of numbers using this code:
r2.sort(function(a,b){return a-b;});
...but in your case the values in the array are strings because they came from the .value attribute of DOM elements, so a default string sort will work with r2.sort(). Either way you don't need your own sort routine, because JavaScript provides one.
EDIT: If you assume that you can just put the five values as a string as above you can implement tests for all possible combinations as a big if/else like this:
r.sort();
r = r.join("");
if (/(.)\1{4}/.test(r)) {
alert("Five of a Kind");
} else if (/(.)\1{3}/.test(r)) {
alert("Four of a Kind");
} else if (/(.)\1{2}(.)\2|(.)\3(.)\4{2}/.test(r)) {
alert("Full House");
} else if (/(.)\1{2}/.test(r)) {
alert("Three of a Kind");
} else if (/1234|2345|3456/.test( r.replace(/(.)\1/,"$1") ) {
alert("Small Straight");
} // etc.
Demo: http://jsfiddle.net/4Qzfw/
Why don't you just have a six-element array of booleans indicating whether a number is present, then check 1-4, 2-5, and 3-6 for being all true? In pseudocode:
numFlags = array(6);
foreach(dice)
numFlags[die.value-1] = true;
if(numFlags[0] && numFlags[1] && numFlags[2] && numFlags[3]) return true
//Repeat for 1-4 and 2-5
return false
This wouldn't be a useful algorithm if you were using million-sided dice, but for six-siders there are only three possible small straights to check for, so it's simple and straightforward.
I do not play Yahtzee, but I do play cards, and it would appear the algorithm might be similar. This routine, written in ActionScript (my JavaScript is a bit rusty) has been compiled but not tested. It should accept 5 cards for input, and return a message for either straights greater than 3 cards or pairs or higher.
private function checkCards(card1:int,card2:int,card3:int,card4:int,card5:int):String
{
// Assumes that the 5 cards have a value between 0-12 (Ace-King)
//The key to the routine is using the card values as pointers into an array of possible card values.
var aryCardValues:Array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
aryCardValues[card1] += 1;
aryCardValues[card1] += 1;
aryCardValues[card1] += 1;
aryCardValues[card1] += 1;
aryCardValues[card1] += 1;
var aryCardNames:Array = new Array("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King");
var strOutMessage:String;
var intCardCount:int = 0;
var strSeperator:String;
var strHighCard:String;
for (var i:int = 0;i < aryCardValues.length;i++)
{
//Check for runs of three of a kind or greater.
if (aryCardValues[i] >= 2)
{
strOutMessage = strOutMessage + strSeperator + i + "-" + aryCardNames[i] + "s";
strSeperator = " & ";
}
//Check for values in a straight.
if (aryCardValues[i] > 0)
{
intCardCount++;
if (intCardCount > 3)strHighCard = aryCardNames[i];
}
else
{
if (intCardCount < 3)intCardCount = 0;
}
}
if (intCardCount > 3) strOutMessage = intCardCount + " run " + strHighCard + " High."
return strOutMessage;
}
It may not be as concise as the regular expressions used above, but it might be more readable and easily modified. One change that could be made is to pass in an array of cards rather than discrete variables for each card.

Time validations (Javascript)

I have a form (Using JavaScript) in which users are supposed to enter information like their work start time, break start time, break end time and work end time. I have 4 textboxes for this purpose. Time entered into the textbox is in hhmm format (24-hour only).
Requirements:
1. The break times (start and end) must be within the work start and end time.
2. Break start must be before break end time.
3. Users can take up work shifts during the day or even overnight.
4. Work time(excluding breaks) should be less than 8 hours and greater than 4 hours.
So some typical valid entries would be:
User1-
Start time: 0900
Break start time:1300
Break end time:1400
End time:1600
User2-
Start time:2200
Break start time:2300
Break end time:2330
End time:0400
Some invalid entries would be:
Case1 - Break start before break end
Start time:2200
Break start time: 2330
Break end time: 2300
End time:0400
Case 2 -Breaks outside work time
Start time:2200
Break start time:1830
Break end time: 1900
End time:0400
I'm having trouble writing the validation code (JavaScript) for requirements 1,2 & 3. Please help me out.
Here's what I've got so far. (Please note: I cannot use DATE objects.)
var wrkSt = getElement('TB1').value;
var wrkSt_hr = parseFloat(wrkSt.substr(0,2));
var wrkSt_mn= parseFloat(wrkEd.substr(2,2));
var brkSt = getElement('TB2').value;
var brkSt_hr = parseFloat(brkSt.substr(0,2));
var brkSt_mn= parseFloat(brkEd.substr(2,2));
var brkEd = getElement('TB3').value;
var brkEd_hr = parseFloat(brkSt.substr(0,2));
var brkEd_mn= parseFloat(brkEd.substr(2,2));
var wrkEd = getElement('TB4').value;
var wrkEd_hr = parseFloat(wrkEd.substr(0,2));
var wrkEd_mn= parseFloat(wrkEd.substr(2,2));
var msg='';
if ((wrkSt_hr > wrkEd_hr) || ((wrkSt_hr == wrkEd_hr) && (wrkSt_mn >= wrkEd_mn)) )
{
msg+='shift overnight selected';
}
if (wrkEd_hr>12){wrkEd_hr-=12;}
if (wrkSt_hr >12){wrkSt_hr -=12;}
if (brkSt_hr>12){brkSt_hr-=12;}
if (brkEd_hr>12){brkEd_hr-=12;}
var Breakdiff = ((brkEd_hr - brkSt_hr)*60) + (brkEd_mn - brkSt_mn);
var Workdiff_tot = ((wrkEd_hr- wrkSt_hr)*60) + (wrkEd_mn -wrkSt_mn);
var Shiftdiff = Workdiff_tot - Breakdiff;
if (Shiftdiff > 480) //8hours = 8*60 = 480 min
{
msg+='Time greater than 8 hrs';
}
if (Shiftdiff < 240) //4 hours = 4*60 = 240 min
{
msg+='Time less than 4 hrs';
}
Please help me with the logic for checking breaks and work time. Thx for any help in advance!
function validate(start, breakStart, breakEnd, end) {
var minutes = [];
for (var i = 0; i < 4; ++i) {
var time = arguments[i];
if (!/^(?:[01]\d|2[0-3])[0-5]\d$/.test(time)) {
throw new Error("Invalid time " + time);
}
var minute = minutes[i] = time.substring(0, 2) * 60 + +time.substring(2);
if (i && minute < minutes[i - 1]) { // Take into account times that cross midnight.
minutes[i] = (minute += 24*60);
}
if (i && minute <= minutes[i - 1]) {
throw new Error("Out of order " + arguments[i - 1] + " and " + time);
}
}
if (minutes[3] - minutes[0] > 8*60) { throw new Error("Shift too long"); }
if (minutes[3] - minutes[0] > 4*60) { throw new Error("Shift too short"); }
}

Categories

Resources