Increase integer variable by one on click - javascript

I am trying to increase the value for the globally defined variable highestPurposeValue in case the if condition is true, but it does not work. It returns every time 1. What am I doing wrong?
var highestPurposeValue = 0;
$('#addJobPurposeButton').on('click', function(){
var value = $('#jobPurposeInputForm').val();
$('#jobPurposeList li').map(function () {
var item = $(this).find("input[name^='jobPurpose']").attr('name');
var itemNumber = item.match(/\d+/); // returns an array.
if (itemNumber[0] > highestPurposeValue) {
var num = parseInt(itemNumber[0]);
var plusOne = num+1;
highestPurposeValue = plusOne;
}
});

Related

Having problems reassigning a variable

So my variable timeAmount needs to vary depending upon the game mode (let gamea). Thus, I am trying to reassign var timeAmount for each game mode in a ternary, as can be seen below.
window.onload = function() {
var timeAmount = 20,
display = document.querySelector("#time");
startTimer(timeAmount, display);
let gamea = document.querySelectorAll("#words, #words1, #words2");
for (let i = 0; i < gamea.length; i++) {
randomnum == 1 ?
((gamea[i].style.display = "block"), (timeAmount = 10)) :
(gamea[i].style.display = "none");
}
};
What do I need to do to var timeAmount so that I can change the value for each particular situation? Ie. different game modes.
You need to reassign timeAmount before you use it in the call to startTimer.
There's also no need to check randomnum every time through the loop, since it doesn't change. Set a variable to the display mode, and assign that to all the elements.
window.onload = function() {
var timeAmount = randomnum == 1 ? 10 : 20,
display = document.querySelector("#time"),
displayMode = randomnum == 1 ? "block" : "none";
startTimer(timeAmount, display);
let gamea = document.querySelectorAll("#words, #words1, #words2");
for (let i = 0; i < gamea.length; i++) {
gamea[i].style.display = displayMode;
}
};

Google apps script store script property value as an integer

How to store script property value as an integer.
if(PropertiesService.getScriptProperties().getProperty('setnumber'))
{
var setnumber = Number(PropertiesService.getScriptProperties().getProperty('setnumber')) + Number(1);
}
else
{
var setnumber = Number(PropertiesService.getScriptProperties().setProperty('setnumber', 1)); // this value gets stored as 1.0 or returns undefined
}
var numofrow = 500;
var maxvalue = numofrow * setnumber; //returns undefined
The setProperty doesn't returns the value, it rather sets a value to a property. Refer the below code.
var setnumber = 1;
if(PropertiesService.getScriptProperties().getProperty('setnumber'))
{
setnumber = parseInt(PropertiesService.getScriptProperties().getProperty('setnumber')) + 1;
PropertiesService.getScriptProperties().setProperty('setnumber', setnumber);
}
else
{
PropertiesService.getScriptProperties().setProperty('setnumber', setnumber); // you can't assign this to a variable
}
var numofrow = 500;
var maxvalue = numofrow * setnumber; //returns undefined
Logger.log(maxvalue)

How do I write a for loop that stops at every third index item, and continues inserting items to a new (carousel) page?

I have the following code:
page.listSuccess = function (data, status, xhr) {
console.log(data);
if (data && data.items) {
var itemsQty = Math.ceil(data.items.length / 3);
for (var index = 0; index < itemsQty; index++) {
var itemData = {};
var itemClone = page.insertItemDOM(itemData);
if (index == 0) {
itemClone.addClass("active");
console.log(itemClone);
}
}
}
for (var index = 0; index < data.items.length; index++) {
var testimonialData = {}
testimonialData.title = data.items[index].title;
testimonialData.body = data.items[index].body;
testimonialData.starRating = data.items[index].starRating;
testimonialData.id = data.items[index].id;
page.insertTestimonialDOM(testimonialData);
}
}
At the moment, this code inserts all(5) my testimonials on one page. I need three per page, but I'm unsure as to how to go about it.
itemClone = a page on the carousel
testimonialData = a testimonial
insertItemDOM function =
page.insertItemDOM = function (itemData) {
var newItem = $($("#itemTemplate").html()).clone();
var targetLoc = $('.carousel-inner');
targetLoc.attr("data-target", "true");
targetLoc.append(newItem);
return newItem;
}
insertTestimonialDOM function =
page.insertTestimonialDOM = function (testimonialData) {
var newTemplate = $($("#testimonialTemplate").html()).clone();
newTemplate.find('.title').html(testimonialData.title);
newTemplate.find('.body').html(testimonialData.body);
newTemplate.find('.starRating').html(testimonialData.starRating);
var targetLoc = $('.carousel-inner');
targetLoc.attr("data-target", "true");
targetLoc.append(newTemplate);
}
Let me explain the concept for you. What you want is after every 3 loop execution the loop shall work it again and enter details in a new page.
Let's make two function.
var index;
var theinitial = function(z)
{
index = z
var counter = 1;
insertionfunction();
}
var insertionfunction = function()
{
for (var zzz = index; zzz < data.items.length; zzz++) {
if (counter <= 3)
{
itemClone = page.insertItemDOM(itemData);
}
else
{
theinitial(zzz);
}
}
Here the first request you make to 'theinitial function' with 1 as value of z then the function will call the insertion function and now the loop will run for 3 times then after three times the value of zzz is used to call the initial function again. Now your index is 4, hence the loop will start from 4th element and run for 3 more times as counter is reinitialized to 1.
I don't really know how your page insertion thing works therefore I am explaining the concept to you.

How to switch streams based on some EventStream changes in Bacon

Consider this example from http://baconjs.github.io/
var up = $('#up').asEventStream('click');
var down = $('#down').asEventStream('click');
var counter =
// map up to 1, down to -1
up.map(1).merge(down.map(-1))
// accumulate sum
.scan(0, function(x,y) { return x + y });
// assign observable value to jQuery property text
counter.assign($('#counter'), 'text');
What if I have one more button for resetting counter and an event stream from this button clicks. How do I switch counter stream based on reset clicks stream to reset counter? I know that I have to use .flatMapLatest method, but referring this example I dont know how to do it.
You don't need flatMapLatest here. You can use a powerful and a much simpler Bacon.update as in this example we have a simple state machine.
var up = $('#up').asEventStream('click');
var down = $('#down').asEventStream('click');
var reset = $('#reset').asEventStream('click');
var counter = Bacon.update(0,
[up], function (prev, unused) { return prev + 1; },
[down], function (prev, unused) { return prev - 1; },
[reset], function (prev, unused) { return 0; }
);
Assuming #reset is a button for resetting
var up = $('#up').asEventStream('click');
var down = $('#down').asEventStream('click');
var reset = $('#reset').asEventStream('click');
var counter = reset.flatMapLatest(function () {
return up.map(1).merge(down.map(-1))
.scan(0, function(x,y) { return x + y });
});
counter.assign($('#counter'), 'text');

setInterval delays

I'm trying to execute a rotating banner (Calling it through an array). I set an interval but the image only shows after 10 seconds (10000) and only then begins the rotation. I removed the cluttered HTML of the array, but here's the rest of it:
var current = 0;
var banners = new Array();
banners[0]=;
banners[1]=;
banners[2]=;
banners[3]=;
var myTimeout = setInterval("rotater()",10000);
function rotater() {
document.getElementById("placeholderlayer").innerHTML=banners[current];
if(current==banners.length-1){
current = 1;
}else{
current += 1;
}
}
window.onload = rotater();
window.onload = rotater;
is the correct syntax. You don't want to call the function. However, the bulletproof solution is rather this:
onload = function() {
rotater();
window.myTimeout = setInterval(rotater, 10000); // Never pass a string to `setInterval`.
};
ProTip™: Don't use new Array(), use an array literal. For example, this:
var arr = new Array();
arr[0] = 'Hello';
arr[1] = 'world!';
Should be written as:
var arr = ['Hello', 'world!'];
Just a comment:
Instead of:
if(current==banners.length-1) {
current = 1;
} else {
current += 1;
}
you can do:
current = ++current % banners.length;

Categories