Jquery check if is last negative number in an object array - javascript

I need to check if in an object array the current value is the last negative.
For example:
var myArray = [
name:'a' value:'5','-1','-5','7','-3'
name:'b' value:'7','-5','-4','7','5'
]
In a for loop, if current value is negative, i need check if is the last negative value in the array for this name.
For example In name 'b':
current value = -5, it's negative, so, Is the last negative? No
current value = -4, it is negative, so, Is the last negative? Yes
Start code for the example:
lastName = '';
for(i = 0; i < myArray.length; i++){
if(myArray[i].value < 0 && lastName == myArray[i].name){
// then it's negative, now check if is the last negative value
}
lastName = myArray[i].name;
}
Inside the if I dont understand how to do this, can someone help me?

Not entirely sure what you're trying to achieve as the question is hard to understand and your example code is...well I dunno what it is but it ain't valid JS.
Anyhow,
If you're looking to find out if the last value of the values array per object is negative. Do the below
for(var i = 0; i < myArray.length; i++) {
var currObj = myArray[i];
var lastValue = currObj.value[currObj.value.length - 1];
var valIsNegative = parseInt(lastValue) < 0;
if (valIsNegative) {
// Last value is negative, do things.
}
if (!valIsNegative) {
// Last Value is positive, do things.
}
}
Again, not sure if this is even relevant to your question as it is hard to decipher. Let me know and I'll delete if it's not.

Related

Apps Script For Loop stuck at 0 in iteration although it is running

I have an array 'vnData' containing 5, 6 rows from where I am trying to extract 3rd column values (based on a criteria) and insert to a new array. Here is my code
for (odr = 0; odr < vnData.length; odr++){
Logger.log(vnData);
tempOdr = vnData[odr][3];
Logger.log(odr);
Logger.log(tempOdr);
Logger.log(vnData[odr][3]);
for(k = 0; k < vnData.length; k++){
if(vnData[k][3] = tempOdr){
odrVal = odrVal + vnData[k][11];
}
}
if(odrVal > 0){
affOdrSet.push(tempOdr);
}
Logger.log(affOdrSet);
}
Logger gives right value of odr in Logger.log(odr); but in Logger.log(vnData[odr][3]); I am always getting a result where value of odr is 0.
So for each iteration I get value from first row. Please help what is wrong in it.
One more thing, if I log Logger.log(vnData[3][3]) in place of Logger.log(vnData[odr][3]) then for first iteration it gives me right value from row 4 but for all subsequent iterations even Logger.log(vnData[3][3]) gives value from first row which is really weird.
The problem is the expression of the first if statement. Instead of
vnData[k][3] = tempOdr
use
vnData[k][3] === tempOdr
The above because = is the assign operator but it's very likely that instead of assigning tempOdr to vnData[k][3] what you want to compare them.

Javascript beginner inquiry. Identified problematic line of code but dont know why it doesnt work

I want the function NumberAddition to take a string and return an array of each number in it. For example argument "abc64,$ 22, 22xyz0" should return array [64, 22, 22, 0]. The 4th line in my for loop is the problem.
It's purpose is to move i up the string after a number is indexed so that it doesn't go over the same number again. If I delete this line the rest works fine with the problem of course being the same number being counted over and over until I naturally passes over it.
Why, when this line is included, does only the first number get counted (in my example the return would be [64]). I am less interested in other ways to solve it than why this line is not functioning how I had imagined it to. Thank you!
function NumberAddition(str) {
var array = [];
var nextInt;
for(var i = 0; i < str.length; i++){
nextInt = myParseInt(str.slice(i,10));
array.push(nextInt);
if (nextInt != null)
i = str.indexOf(nextInt,i) + nextInt.length -1;
else
break;
}
// code goes here
return array;
}
function myParseInt(str){
for(var i = 0; i < str.length; i++){
if (!isNaN(str[i]))
return parseInt(str.slice(i));
}
return NaN;
}
Numbers don't have a length; strings do. nextInt.length always returns undefined, which means you're setting i to NaN. Try nextInt.toString().length instead.

Result of FOR-loop if there is no related item

in a Podio calculation_field A I sum up the numbers of each related item (from another app) which contain "searchstring_1" in a text_field, in calculation_field B all related items which contain "searchstring_2"
No problem with the following code - IF there exists a related item. But if there exists no related item the "1" (= "nothing found"?) is displayed as "result". I tried several things, but can't find a solution for a calculation like: IF there is no related item then result = 0 (or "" or null), else let run the for-loop. Has anybody a tip what I can do?
Thanks,
Rainer
var str = all of text_field;
var num = all of number_fields;
var total = 0;
for(var i = 0; i < num.length ; i++) {
if (str[i].indexOf("searchstring_1") === 0) {
total += num[i];
}
}
The calculation field always returns the last used value if you don't explicitly specify the return value. Maybe in your case the last value of i, str[i].indexOf("searchstring_1") would return -1, I think...
To make sure that the value of totalis returned, simply add
total;
at the end of your calculation field value.
Enjoy,
Michael / Hamburg

Problems with the "+" and "-" operators

The problem is the - operator does not working (in 8th line). See my code below:
array = [0,0,0,0,3,0,0,0,0],
n = 0;
for(var i = 0; i < array.length; i++){
if(n < 9){ //the "n" variable there's only for don't crash the browser with a infinite loop
if(array[i] == 3){
array[i] = 0;
array[i - 1] = 3; //I believe that here is the problem
}
}
n++;
}
console.log(array);
So... I want to move the "3" value to the beginning of the array. But it only work if I use the + operator(in 8th line). Consequently, if I use the + one, the "3" value goes to the end of the array.
Anyone know why the - operator does not working in this case and the + works?
If you change line 8 to:
array[i+1] = 3;
then the number 3 will go all the way to the end of the array (well, beyond the end of the array and I'll be damned to find out what Javascript does then!). This is because the loop traverses the array in increasing order and the position i+1 will be checked right next.
On the other hand, with your current line 8, number 3 goes one position backwards (which has already been checked), so it doesn't go all the way to the beginning of the array, just one position. If you want it to go to all the way in the same fashion, you should reverse the loop (make it traverse the array in descending order of the position i).
What do you think happens when i is 0 and you do - 1?
In your first iteration of the loop, when i is zero, (i - 1) is -1, so you're trying to access array[-1], which is invalid.
Okay, instead of answering "Anyone know why the - operator does not working in this case and the + works?", I will answer what I think is the real question, as stated in the original post: I want to move the "3" value to the beginning of the array. I think this does what is desired:
var array = [0,0,0,0,3,0,0,0,0];
var first_val = 3;
var index = array.indexOf(first_val);
if (index > 0) {
array.splice(index, 1);
array.unshift(first_val);
}
console.log(array);
Inspired by this.
I want to move the "3" value to the beginning of the array
Use the correct answer provided in here. Then use .indexOf() to move it.
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
return this; // for testing purposes
};
var array = [0,0,0,0,3,0,0,0,0];
var result = array.move(array.indexOf(3),0);
console.log(result);
JSFiddle Demo

Trying to search through array of strings for a pattern the user inputs - javascript

var p = ["array of words", "i really hate the sun", "i'm TIred"]
function findword(contents,pattern){
var answer;
var idx;
var counter = 0;
var pat_lc = pattern.toLowerCase();
for (var i = 0; i <= contents.length-1; i++){
var str_lc = contents[i].toLowerCase();
idx = str_lc.indexOf(pat_lc)
if (idx >= 0){
answer = i
}
else{
answer = "-1"
}
}
alert(answer)
}
findword(p,"words")
I'm trying to find the index of the array for a certain word, in an array of strings, however it only works for certain words in the array above. For example, on the last line of the code, when I chose to search for "words" it returns the value of "answer" and "idx" -1 when it should be 0 and 9 respectively. However, when I search for "tired", the value of "answer" is 2 and "idx" is 4 (which is correct). Why do some words work and others return the value of -1?
Here's a modified working version that will find all matches
var p = ["array of words", "i really hate the sun", "i'm TIred"]
function findword(contents,pattern){
var matches=[];
var idx;
var pat_lc = pattern.toLowerCase();
for (var i = 0; i <= contents.length-1; i++){
var str_lc = contents[i].toLowerCase();
idx = str_lc.indexOf(pat_lc)
if (idx >= 0){
matches.push({ idx: i, position: idx})
}
}
return matches
}
Several problems
answer and idx get overwritten ever pass of the for loop. So regardless if a match was found...only last pass is returned. I saved the results into separate array that gets returned. Code you provided didn't do anything with idx.
Can test output for length to see if matches exist...no matches will return empty array with no length
Come to think of it...not sure what output you want
DEMO
You should stop as soon as you find a solution.
Your program only stops when it iterated over the whole array. But what if the answer what not in the last element ? Then answer will contain -1.
You can init answer at -1 and continue your for loop as long as i is less than content.length and answer is equal to -1 : http://jsfiddle.net/#&togetherjs=dbVcN8JkKP

Categories

Resources