Javascript News Ticker - javascript

I'm using this code to fade in and out different messages.
It works ok. The only problem is that it delays the first loop. And I have to wait before the first message shows up. I tried to fix it but with no luck!
var loops = 0;
function t() {
var news = new Array("Text01", "Text02");
var l = news.length;
var fade = 300;
var delay = 6000;
var process = delay+fade+fade;
var n = 0;
var f = '';
for(i=0; i<l; i++){
f += "<span class='ticker' id='str"+i+"'>"+news[i]+"</span> ";
$("#newsticker").html(f);
}
for(i=0; i<l; i++){
n++;
var offset = i*process;
$("#str"+i).delay(offset).fadeIn(fade).delay(delay).fadeOut(fade);
}
var loop_process = n*process;
if(loops==0){
setTimeout("t()", 0);
}
setTimeout("t()", loop_process);
loops++;
}
t();
HTML
<span id="newsticker"></span>

Related

Efficient Table Seating Function

I'm creating a table seating function in javascript, directly below, for a scenario where there is a population of P participants, in this case tested with 80, and S seats per table, in this case tested by 8, each participant may only visit each table once with a total of ten tables and each participant may not meet another participant more than once for a minimum of 10 rotations.
How can I make 10 unique sets of P/S, 10 times?
To explain my naming, arrayArray is the array for each of the tables and the outer arrays inner array elements are the table seating, arrayArrayPrevious is the list of everyone whose already been to a table, and the participantArray is an array of all possible participants.
The trouble seems to be when finding two participants have already met and moving the second one to the end of the participantArray to be tried again later results in only one participant ever being placed.
I'm placing the entire code below the function snippet in the event someone can help solve it and its useful for others in the future.
function notMetAlready(w, arrayArray, participantAlreadyMet){
if(arrayArray.includes(participantAlreadyMet)){
var moveToEnd = arrayArray[w][0];
console.log(moveToEnd);
participantArray.push(moveToEnd);
console.log(participantArray);
return true;
//console.log(index);
}
if(!arrayArray.includes(participantAlreadyMet)){
return true;
}
else{
return false;
}
}
Full Code
<html>
<head>
<script type="text/javascript">
var tables = 10;
var participants = 80;
var participantPool = [];
var seatingPerTable = participants/tables;
var arrayArray = new Array(tables);
for(var i = 1; i <= participants; i++){
participantPool.push(i);
}
var count = 1;
var participantArray = new Array(participants);
var participantAlreadyMet = new Array(participants);
var arrayArrayPrevious = new Array(tables);
for (var i = 0; i <= tables; i++) {
arrayArrayPrevious[i] = [];
arrayArray[i] = [];
}
for (var i = i; i < participantAlreadyMet.length; i++) {
participantAlreadyMet[i] = [i];
}
function MatchingPairs() {
for (var i = 0; i < tables; i++) {
arrayArray[i] = [];
}
for(var h = 0; h < participants; h++){
participantArray[i] = i+1;
}
for(var w = 0; w < arrayArray.length; w++){
if(tablesHaveNotIncluded(w,0)){
// for(var n = 1; n < participants; n++){
do{
if(tablesDoNotInclude(0) && notMetAlready(w, arrayArray[w], participantAlreadyMet[0])){
arrayArray[w].push(participantArray[0]);
arrayArrayPrevious[w].push(participantArray[0]);
participantArray.shift();
}
}while(participantArray >= 0);
}
}
function notMetAlready(w, arrayArray, participantAlreadyMet){
if(arrayArray.includes(participantAlreadyMet)){
var moveToEnd = arrayArray[w][0];
console.log(moveToEnd);
participantArray.push(moveToEnd);
console.log(participantArray);
return true;
}
if(!arrayArray.includes(participantAlreadyMet)){
return true;
}
else{
return false;
}
}
for(var z = 0; z < tables; z++){
var plus = z + 1;
console.log("Table " + plus + " " + arrayArray[z] );
}
console.log("Rotation " + count);
count++;
function tablesHaveNotIncluded(w,n){
var outerArray = arrayArrayPrevious[w];
if(!outerArray.includes(n)){
return true;
}
return false;
}
function tablesDoNotInclude(n){
for(var w = 0; w < tables; w++){
if(!arrayArray[w].includes(n)){
return true;
}
}
return false;
}
}
</script>
</head>
<body>
<button onclick="MatchingPairs()">Combinations</button>
</body>
</html>

js Page Freezing on from .length?

This function is freezing my page.
function findMode (array)
{
var modeArr = [];
var modeCounter = [];
modeArr.length = array.length;
modeCounter.length = array.length;
}
However, when I remove this it runs just fine.
modeArr.length = array.length;
modeCounter.length = array.length;
Here is all of my code:
<html>
<head>
</head>
<body>
<p> Please enter a series of numbers, each separated by a new line.<br><p>
<textarea id="myTextArea" rows = "7" cols = "50"></textarea><br>
<button onclick="processData()">Done</button>
<p id = "mean"></p>
<p id = "median"></p>
<p id = "count"></p>
<p id = "summation"></p>
<p id = "mode"></p>
<p id = "variance"></p>
<p id = "sd"></p>
<script type = "text/javascript">
var mean = 0;
var median = 0;
var count = length;
var mode = 0;
var variance = 0;
var standard_deviation = 0;
var meanOutput = document.getElementById('mean');
var medianOutput = document.getElementById('median');
var modeOutput = document.getElementById('mode');
var countOutput = document.getElementById('count');
var summationOutput = document.getElementById('summation');
var varianceOutput = document.getElementById('variance');
var sdOutput = document.getElementById('sd');
function processData()
{
var arrayOfLines = document.getElementById('myTextArea').value.split('\n');
var sum = findSum(arrayOfLines);
findMean(arrayOfLines, sum);
findMedian(arrayOfLines);
findMode(arrayOfLines);
findVariance(arrayOfLines);
findStandardDeviation(arrayOfLines);
findVariance(arrayOfLines);
}
function findSum (array)
{
var count = array.length;
var sum = 0;
for (var a = 0; a < array.length; a++)
{
sum += parseInt(array[a]);
}
countOutput.innerHTML = "Count: " + array.length;
summationOutput.innerHTML = "Sum: " + JSON.stringify(sum);
return sum;
}
function findMode (array)
{
var modeArr = [];
var modeCounter = [];
modeArr.length = array.length;
modeCounter.length = array.length;
for (var a = 0; a < array.length; a++)
{
for (var b = 0; b < modeArr.length; b++)
{
if (modeArr[a] == modeArr[b])
{
modeCounter[a]++;
}
if (a == 0)
{
b--;
}
}
modeArr[a] = array[a];
}
modeOutput.innerHTML = "Mode: ";
}
function findMean (array, sum)
{
mean = sum/array.length;
meanOutput.innerHTML = "Mean: " + mean.toPrecision(2);
}
function findMedian (array)
{
for(var i=0; i < array.length; i++)
{
array[i] = +array[i];
}
var sortedArrayOfLines = array.sort(function(a, b){return a - b});
if (array.length % 2 == 1)
{
median = sortedArrayOfLines[((array.length - 1)/2)]
}
else
{
median = (sortedArrayOfLines[array.length/2] + sortedArrayOfLines[(array.length/2)+1])/2
}
medianOutput.innerHTML = "Median: " + median;
}
function findVariance (array)
{
var mean = mean(array);
return mean(array.map(function(num)
{
varianceOutput.innerHTML = Math.pow(num - mean, 2);
}));
}
function findStandardDeviation (array)
{
medianOutput.innerHTML = Math.sqrt(variance(array));
}
</script>
</body>
</html>
So the issue isn't the length it's a infinite loop.
The problem is this bit of code
if (a == 0)
{
b--;
}
This is inside the following loop with b as the iterator. See below.
for (var b = 0; b < modeArr.length; b++)
a is set to zero by the outer loop. Thus a==0 is always true inside the inner loop. b will never increase only decrease. Thus this is a infinite loop because b will never be greater than modeArr.length.
So I would consider revising the function, below is a example of a possible candidate for a mode function:
Get the element with the highest occurrence in an array

replace string using array in javascript

abc1 abc2 abc3
abc4 abc5 abc6
abc7 abc8 abc9
using above csv file, load text by replacing the double quotes from the bellow sentence.
Sentence:
"" is going with "" to "" for something to know.
Expected out put is:
abc1 is going with abc2 to abc3 for something to know.
abc3 is going with abc5 to abc6 for something to know.
like this in javascript, php.
Code I have tried so far:
var s = 'Neque porro "" estqui "" dolorem';
var insert = [["a1", "b1"]["c1","d1"]];
console.log(insert);
var words = new Array();
words = s.split(" ");
console.log(words);
var count = 0;
for (i = 0; i < words.length; i++) {
for (j = 0; j < insert.length; j++) {
if(words[i] == '""')
s = s.replace(/""/, insert[j]);
}
}
console.log(s);
Hurray I got the answer:
reader.onload = function (e) {
var sent = ''; var sentt = ''; var senttt = '';
sent = $('#mixmsg').val();
var quoteLength = (sent.match(/""/g) || []).length;
var rowcells = [];
rows = e.target.result.split("\n"); alert(rows);
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(",");
rowcells.push(cells);
}
var rowcellso = rowcells.slice(0, -1);
console.log(rowcellso);
for (var ro = 0; ro < rowcellso.length; ro++) {
for (var scol = 0; scol < quoteLength; scol++) {
sent = sent.replace(/""/,rowcellso[ro][scol]);
console.log(rowcellso[ro][scol]);
} sentt +=sent+'\n'; sent= $('#mixmsg').val();
}
$('#container').html(sentt);
}

allocation of amount to different fields javascript

I have a problem, in fact at least I do not know how. I have several fields revealing different amount  each position is identified by an id example: p1, p2, p3 etc. ...
So this fields contain amounts, in their decimal form.
what I wish to make. Because in fact I will be led to allocate an amount entered in a field that is a deposit.
that is to say as long as the amount of deposit is> 0 then I spread over the various post
For example let's say I have
300 = p1, p2 and p3 = 120 = 50
and I have a deposit for an amount of 450 Euros, so I would
p1 = 0 and o1 fields (fields that is revealing what has been imputed) 300
p2 = 0 and the fields 120 o2
p3 = 20 fields and o3 = 30
I actually tried by a number of conditions but I tangled brushes.
this is what it gave:
<script type="text/javascript">
function ventilation()
{
var montantacompte = document.getElementById("montantacompte").value;
var p1 = document.getElementById("p1").value;
var p2 = document.getElementById("p2").value;
var p3 = document.getElementById("p3").value;
var p4 = document.getElementById("p4").value;
var p5 = document.getElementById("p5").value;
var p7 = document.getElementById("p7").value;
var p8 = document.getElementById("p8").value;
var p9 = document.getElementById("p9").value;
var p10 = document.getElementById("p10").value;
var p11 = document.getElementById("p11").value;
var p12 = document.getElementById("p12").value;
var p13 = document.getElementById("p13").value;
if(p1>0 &&p1-montantacompte>=0)
{
f1=p1-montantacompte;
document.getElementById('p1').value=f1;
document.getElementById('o1').value=Math.round(montantacompte*100)/100;
}
if(p1>0 &&p1-montantacompte<=(p1+p2))
{
reste1=montantacompte-p1;
impute=p1;
impute2=reste1;
f1=m1-impute;
document.getElementById('p1').value=f1;
document.getElementById('o1').value=Math.round(impute*100)/100;
document.getElementById('o2').value=Math.round(impute2*100)/100;
}
}
</script>
I also tried with a loop but well I also tangled brush, my small knowledge of javascript does not help.
<script type="text/javascript">
function ventilation()
{
var reste = document.getElementById("montantacompte").value;
var ac = document.getElementById("montantacompte").value;
var p1 = document.getElementById("p1").value;
var p2 = document.getElementById("p2").value;
var p3 = document.getElementById("p3").value;
var p4 = document.getElementById("p4").value;
var p5 = document.getElementById("p5").value;
var p7 = document.getElementById("p7").value;
var p8 = document.getElementById("p8").value;
var p9 = document.getElementById("p9").value;
var p10 = document.getElementById("p10").value;
var p11 = document.getElementById("p11").value;
var p12 = document.getElementById("p12").value;
var p13 = document.getElementById("p13").value;
var c = 1;
while(reste>0 && c<13)
{
if(reste> 'p'+c)
{
reste = 'p'+c - ac;
document.getElementById('p'+c).value=Math.round(honoraires*100)/100;
print reste;
}
}
</script>
suddenly, I do not know because every time I do not arrived more advancing in both cases.
in advance thank you to all and anyone who can help me progress.
Sincerely yours.
As I understand this issue, the user inputs a Payment Amount then this amount is applied to line items on an order.
Input: 17100
Item 1: <input id="p1" class="items" value="16000.00 €"/>
Item 2: <input id="p1" class="items" value="535.00 €"/>
Item 3: <input id="p1" class="items" value="955.00 €"/>
function getItems()
{
var items = new Array();
var itemCount = document.getElementsByClassName("items");
for(var i = 0; i < itemCount.length; i++)
{
items[i] = document.getElementById("p" + (i+1)).value;
}
return items;
}
function setItems(items,payAmt)
{
document.getElementById("inputField").value = payAmt;
for(var i = 0; i < items.length; i++)
{
document.getElementById("p" + (i+1)).value = items[i];
}
}
function itemSum(items)
{
var sum = 0;
for(var i=0; i < items.length; i++)
{
sum = items[i] + sum;
}
return sum;
}
function payment(inputElm)
{
var items = getItems();
var payAmt = document.getElementById("inputField").value;
var i = 0;
var sum = itemSum(items);
while(payAmt != 0 && sum != 0)
{
var temp = items[i] - payAmt;
if(temp > 0)
{
items[i] = temp;
break;
}
else if(temp < 0)
{
items[i] = 0;
payAmt = temp*-1;
}
i++;
sum = itemSum(items);
}
setItems(items, payAmt);
}
I think that should do it if I understand your problem correctly. You'll need to make sure the IDs match up and this is untested so there'll be a bug or two in there more than likely.

going through substr with loop and then want to identify the index

Im trying to get the index number of corresponding characters in a string.
I mean a loop does make it possible to treat characters in a string like an 'array' of characters with the string method charAt() and indexOf(), right?
here's the code:
/** ****** WINDOW ONLOAD EVENT HANDLER **************** */
window.onload = function(){
// DOM elements
var theButton = document.getElementById('theButton');
var form = document.formISBN;
var numberField = document.getElementById('theInput')
theButton.onclick = function(){
var number = numberField.value;
console.log(number)
controlNr = calculControlNr(number);
// console.log(controlNr);
}
}
function calculControlNr(number) {
number = number.replace(' ','','g');
number = number.replace('-','','g');
var sum = 0;
var sumEven = 0;
var sumUneven = 0;
var factor = 3;
var numberExtract = number.substr(0,11);
console.log(numberExtract.length)
for (var i = 0; i < numberExtract.length; i++) {
console.log(numberExtract.indexOf(numberExtract.charAt(i)));
}
}
How about
var numberExtract = number.split('');
console.log(numberExtract.length)
for (var i = 0; i < numberExtract.length; i++) {
if (i==11) break; // you only wanted the first 11?
console.log(i+':'+numberExtract[i]);
}
But your code does work anyway.
Here is what I did to test it
<script>
/** ****** WINDOW ONLOAD EVENT HANDLER **************** */
window.onload = function(){
// DOM elements
var theButton = document.getElementById('theButton');
var form = document.formISBN;
var numberField = document.getElementById('theInput')
theButton.onclick = function(){
var number = numberField.value;
alert(number)
controlNr = calculControlNr(number);
// alert(controlNr);
}
}
function calculControlNr(number) {
number = number.replace(' ','','g');
number = number.replace('-','','g');
var sum = 0;
var sumEven = 0;
var sumUneven = 0;
var factor = 3;
var numberExtract = number.substr(0,11);
alert(numberExtract.length+':'+numberExtract);
for (var i = 0; i < numberExtract.length; i++) {
alert(i+':'+numberExtract.indexOf(numberExtract.charAt(i)));
}
}
</script>
<form name="formISBN">
<input id="theInput" type="text" value="01234567890-A A" />
<input id="theButton" type="button" value="click"/>
</form>

Categories