|
function generateLayout(seats, zIndex) {
var busLayoutHtmlTmpl = '';
var maxNoOfColumns = 0;
var missingPathWayRowNO = -1;
var missingPathWayRowNOTmp = 0;
var isfirtRow = 0;
var isDriverSeat = true;
var rowSeatValues = [];
var columnSeatValues = [];
_.each(seats, function (columnSeats, rowIndex) {
if (isfirtRow == 0) {
missingPathWayRowNOTmp = rowIndex;
isfirtRow = 1;
} else {
if (parseInt(missingPathWayRowNOTmp) + 1 != rowIndex) {
missingPathWayRowNO = parseInt(missingPathWayRowNOTmp) + 1;
} else {
missingPathWayRowNOTmp = parseInt(missingPathWayRowNOTmp) + 1;
maxNoOfColumns = columnSeats.length > maxNoOfColumns ? columnSeats.length : maxNoOfColumns;
}
}
});
var v = [];
_.each(zIndex, function (seats, t) {
$.inArray(seats, v) === -1 && v.push(seats)
}),
_.each(seats, function (columnSeats, rowIndex) {
_.each(columnSeats, function (seat, columnIndex) {
if ($.inArray(seat.x, rowSeatValues) === -1) {
rowSeatValues.push(seat.y);
}
if ($.inArray(seat.y, columnSeatValues) === -1) {
columnSeatValues.push(seat.y);
}
});
});
var seatLayOutMax= new Array();
var count = 0;
_.each(rowSeatValues, function (row, columnIndex) {
seatLayOutMax[row] = new Array();
_.each(columnSeatValues, function (seat, columnIndex) {
seatLayOutMax[row][seat] = "";
});
});
_.each(seats, function (columnSeats, rowIndex) {
_.each(columnSeats, function (seat, columnIndex) {
seatLayOutMax[seat.x][seat.y] = seat
});
});
var pathComparision = 0; var enterInRowsCount = 0;
_.each(seatLayOutMax, function (seats, index) {
busLayoutHtmlTmpl += '<tr>';
_.each(seats, function (col, ind) {
var seat = col;
if (seat == undefined) { return; }
pathComparision = seat.x;
if (seat != "") {
var l = seat.height;
var w = seat.width;
if (l == 1 && w == 1) {
busLayoutHtmlTmpl += '<td title="Seat : ' + seat.id + 'style="width:8%; padding:2px;"><div id="div' + seat.seat_no + "_" + '</div></td>';
} else if (l == 1 && w == 2) {
busLayoutHtmlTmpl += '<td title="Seat : ' + seat.id + 'style="width:8%; padding:2px;"><div id="div' + seat.seat_no + "_" + ' </div></td>';
} else if (l == 2 && w == 1) {
if (rowSeatValues.height == 1) {
busLayoutHtmlTmpl += '<td title="Seat : ' + seat.id + 'style="width:8%; padding:2px;" rowspan="2"><div id="div' + seat.seat_no + "_" + '</div></td>';
} else {
busLayoutHtmlTmpl += '<td title="Seat : ' + seat.id + 'style="width:8%; padding:2px;"><div id="div' + seat.seat_no + "_" + '</div></td>';
}
}
} else {
busLayoutHtmlTmpl += '<td></td>';
}
});
if (seats === undefined && busLayoutHtmlTmpl != "<tr>" && !busLayoutHtmlTmpl.includes('<tr><td><div style="width: 28px; height: 24px; margin-right: 5px;"></div></td><td></td></tr>')) {
busLayoutHtmlTmpl.replace('<tr></tr>', '');
busLayoutHtmlTmpl += '<tr><td><div style="width: 28px; height: 24px; margin-right: 5px;"></div></td><td></td></tr>';
} else {
busLayoutHtmlTmpl += '</tr>';
}
});
return busLayoutHtmlTmpl;
}
function SeatLayout() {
$.ajax({
url: "seats.json",
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);
designCanvas(json);
}
});
}
function designCanvas(json) {
var seatSelected = [];
var divLower = document.getElementById('tblLowerSeat');
var divUpper = document.getElementById('tblUpperSeat');
var lowerSeats = [];
var upperSeats = [];
if(json.seats.z === 0){
lowerSeats.push(json.seats);
}
if(json.seats.z === 1){
upperSeats.push(json.seats);
}
lowerSeats = lowerSeats.sort(function (a, b) { return b.id - a.id; });
upperSeats = upperSeats.sort(function (a, b) { return b.id - a.id; });
var r = [];
lowerSeats.forEach(function (e) {
if (r.indexOf(e.x) == -1) { r.push(e.x); }
});
var g = new Array(r.length);
for (var i = 0; i < r.length; i++) {
g[i] = new Array();
for (var j = 0; j < lowerSeats.length; j++) {
if (lowerSeats[j].x == r[i]) {
g[i].push(lowerSeats[j]);
}
}
}
htmlLower = generateLayout(g, 0);
divLower.innerHTML = htmlLower;
}
<button class="btn btn-primary" id="btnViewSeat" onclick="SeatLayout();">
View Seats class="fa fa-arrow-circle-down">
</button>
<p id="demo"></p>
<link href="/TPT-2020/Assets/css/seatStyle.css" rel="stylesheet" />
<link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
<link href="/TPT-2020/Assets/css/bootstrap.min.css" rel="stylesheet" />
<div id="Viewseatcollapse" class="collapse busseatmng" style="display:inherit">
<div class="col-sm-12 card">
<div class="card-body">
<div class="row">
<div class="col-sm-8">
<div class="upperbusdivwrp" id="divSeatsUpper">
<div class="row busseatstructure">
<div class="col-sm-12 bg-dark pd-10 text-white text-center"><h5>Upper Deck</h5></div>
<div class="col-sm-12 busseatsinner">
<div class="row">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-1 col-1 driver"></div>
<div class="col-sm-11 col-11">
<table id="tblUpperSeat">
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="lowerbusdivwrp">
<div class="row busseatstructure">
<div class="col-sm-12 bg-dark pd-10 text-white text-center"><h5>Lower Deck</h5></div>
<div class="col-sm-12 busseatsinner">
<div class="row">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-1 col-1 driver"><img src="/TPT-2020/images/stairing.png"></div>
<div class="col-sm-11 col-11">
<table id="tblLowerSeat">
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="/TPT-2020/scripts/BindData1.js"></script>
<script type="text/javascript" src="/TPT-2020/scripts/GetDynamicSeats2.js"></script>
|
|
|
|
|
You seem to have forgotten to ask a question or describe a problem.
Just dumping all of your code and telling us "there is some problem", without providing any details of what that problem is, doesn't make a good question.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a global Array.
And there are multiple asynchronous events getting fired from different ends in the code.
In one of the events - Event1(), I'm iterating all the items in the global Array and using the values.
In another event Event2(), I'm removing a particular item from the global Array.
We have no guarantee about the timing or sequence of the event triggers.
Will this ever land in a crash? & How do we safeguard this.
modified 11-Feb-20 5:43am.
|
|
|
|
|
Unless you're using web workers[^], Javascript is single-threaded. Callbacks from async operations will be queued and executed on a single thread, so you don't need to worry about synchronizing access to your global state.
Even with web workers, it's hard to cause problems:
However, since web workers have carefully controlled communication points with other threads, it's actually very hard to cause concurrency problems. There's no access to non-threadsafe components or the DOM. And you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great answer. Happy to know about the Web Worker.
I was thinking if there happens a context-switch between the Async, altering the count of the Array by deleting an element, & thus making the iteration in another code block go out of scope with the index.
I see this is never possible. Thanks to the single-threaded design. :+1:
modified 11-Feb-20 7:32am.
|
|
|
|
|
ugent help please! here my criteria is to drag 3 images and drop them in 3 different locations. validate before dropping in the location if it already has an image, if yes them pop up a message.
Quick response will be highly appreciated. Thanks!
|
|
|
|
|
You need to provide much more detail of your problem.
|
|
|
|
|
here my criteria is to drag 3 images and drop them in 3 different locations. validate before dropping in the location if it already has an image, if yes them pop up an error message.
Basically how do I capture drop location ID where the image is dropped?
Can someone please help me out urgenlty on this?
|
|
|
|
|
There's a secret error somewhere in your secret code. You should fix that.
Seriously, how do you expect anyone here to help you fix your code, when you haven't shown us any of your code?
We can't see your screen. We can't access your computer. We can't read your mind. All we have to work with is what you type in the "message" box. If you can't explain the problem clearly, then we can't help you.
And adding "urgent" and "quick response" to your questions simply tells us that you can't manage your own time. It's not going to force us to answer your unanswerable question any quicker.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
A sign that I've appreciated in a couple of different supply rooms:
"A lack of planning on your part does not constitute an emergency on mine"
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Member 14732785 wrote: validate before dropping in the location if it already has an image, Simple. Write code for the drop event and to check if the img tag already has a src attribute.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
You should probably work on one DND interface first, and then create 2 more of them and DND each image to a specific DND box. Get the hang of DND first and how it works. It's not as easy as it sounds and DND has some events you have to listen for.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
function isAvgWhole(arr) {
return Number.isInteger(arr.reduce((accum, val) => accum + val) / arr.length)
}
function isAvgBowl(arr)
{
return Number.isInteger(arr.reduce(function(accum,val)
{
return (accum+val)/arr.length
}))
}
console.log(isAvgWhole([1,1,1,1]))
console.log(isAvgBowl([1,1,1,1]))
|
|
|
|
|
Because you're using two completely different calculations:
Method 1:
Sum up all of the items in the array. Divide the sum by the length of the array.
arr.reduce((accum, val) => accum + val) / arr.length
var accum = arr[0];
for (var i = 1; i < arr.length; i++) {
accum = accum + arr[i];
}
var result = accum / arr.length;
Method 2:
For each value in the array, add that value to the accumulator, and divide the intermediate result by the length of the array.
arr.reduce((accum, val) => (accum + val) / arr.length)
var accum = arr[0];
for (var i = 1; i < arr.length; i++) {
accum = accum + arr[i];
accum = accum / arr.length;
}
var result = accum;
Step through both methods with a debugger, or write out the steps on a piece of paper, and it should be obvious why the second method is wrong.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hey,
I am solving a problem on hackerrank(https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen). The problem states to complete the function
compareTriplets which must return an array of two integers, the first being ALice's score and the secoond being Bob's.
compareTriplets has the following parameter(s):
a: an array of integers representing Alice's challenge rating
b: an array of integers representing Bob's challenge rating
For more detail on the problem, refer to the following link:
https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen
For the same challenge i wrote the following solution:
function compareTriplets(a, b) {
for (let i = 0; i < 4; i ++) {
points=[];
pointsArray=[];
if (a[i] > b[i]) {
points[i]=1;
pointsArray.push(points[i])
}
if (a[i] === b[i]) {
points[i]=0;
}
if (b[i] > a[i]) {
points[i]=1;
pointsArray.push(points[i])
}
return pointsArray;
}
}
The problem states that the output should be like [1,1] i.e. [points earned by alice, points earned by bob]
But somehow it displays a array with four elements with the values [1], [], [1], []
Refer to https://ibb.co/GCkwM9j for further details.
Can anybody suggest anything?
|
|
|
|
|
Manish169 wrote: Can anybody suggest anything? I would suggest to begin by solving it by hand with a pen and a piece of paper, so that you have a clear knowledge of input and output data as well as required steps. As of now the code you showed does not pass the simplest logic checks: you are provided with triplets (3 values) but operate on array of four values; you have an inconditional return statement inside a loop, rendering this loop kind of useless (it will return at the first iteration); you create two data structures where you only need one; you do not add points to existing values but rather overwrite them.
So, do it by hand first; there is no chance for you to come up with a proper algorithm if you cannot solve it yourself with a pen and paper.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I am looking for a method to close the current tab of the browser window using JavaScript.
|
|
|
|
|
Unfortunately you can't for security purposes. You can only close windows that were opened by Javascript.
|
|
|
|
|
|
Member 14713926 wrote: window.close(); If you know about this, why are you asking then?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Because it's an excuse to post a link to his spam "best programming tutorials" site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: "best programming tutorials" I could use some of that.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
In earlier times you got the full client path from a file upload and we were running ActiveX on our web pages. What's your point?
|
|
|
|
|
That is why websites were easily hacked in the earlier times due to inefficient security.
Like I said, javascript can only close windows that was opened by it.
|
|
|
|
|
Please see attached screenshot. I have question with two options. If user select option 1 then I would like to redirect to URL1 and if user select option 2 then I would like to redirect to URL2.
How can I do that?
JSON code is here. I am using HTML to show the question.
[
{
"question_code": "Q1",
"type": "radio",
"question": "Question1",
"required": "true",
"options": [
{
"code": "Q1OP1",
"text": "Option1",
"child": [ ],
"steps": 11
},
{
"text": "Option2",
"code": "Q1OP2",
"child": [ ],
}
}
]
|
|
|
|