How to detect sign toggle in javascript array? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have example of javascript array as below.
var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4...];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4...];
I would to create function to detect sign change and return with position, e.g. ['index','num1','num2'] For example.
function getSignChange(exampleArr1){
...
return someArray;
}
return shoud be
[
[2,1,-2], //index = 2 change from 1 to -2
[5,-7,5], //index = 5 change from -7 to 5
[8,2,-3], //index = 8 change from 2 to -3
]
Any advice or guidance on this would be greatly appreciated, Thanks.

Maybe this will help you:
var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4];
function getSignChange(arr){
let positive = arr[0] >= 0;
return arr.map((item, index) => {
if (positive && item < 0 || !positive && item >= 0) {
positive = arr[index] >= 0
return [index-1, arr[index-1], item]
}
}).filter(x => x != null);
}
console.log(getSignChange(exampleArr1));
console.log(getSignChange(exampleArr2));

Related

filter method in Javascript isn't work. But the other filter methods are working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My program crashes when I want to add a filter function for the item status column. What I don't understand about this is that the filter function worked optimally for all other columns and when I did it for the "Article status" column in exactly the same way as for the other columns, the filter function no longer works. As I said before, I don't understand why this could be because I did it exactly the same as for the other columns. My json-Arrays:[enter image description here][1]
My error:
Uncaught TypeError: artikelstatus.includes is not a function
at searchAllTable (artikelSuche.js:124)
at filterByAll (artikelSuche.js:108)
at getValueFromArtikelstatus (artikelSuche.js:162)
at HTMLSelectElement.onchange (artikelSuche.html:29)
function searchAllTable(artikelnummerValue, materialbezeichnung1Value, materialbezeichnung2Value, teileartValue, materialklasseValue, artikelstatusValue, data) {
var filteredData = [];
for (var i = 0; i < data.length; i++) {
materialbezeichnung1Value = materialbezeichnung1Value.toLowerCase();
var materialbezeichnung1 = data[i].materialbezeichnung1.toLowerCase();
materialbezeichnung2Value = materialbezeichnung2Value.toLowerCase();
var materialbezeichnung2 = data[i].materialbezeichnung2.toLowerCase();
var artikelnummer = data[i].artikelnummer;
var teileart = data[i].teileart;
var materialklasse = data[i].materialklasse;
var artikelstatus = data[i].artikelstatus;
if (materialbezeichnung1.includes(materialbezeichnung1Value) && artikelnummer.includes(artikelnummerValue) && materialbezeichnung2.includes(materialbezeichnung2Value) && teileart.includes(teileartValue) &&
materialklasse.includes(materialklasseValue) && artikelstatus.includes(artikelstatusValue)) {
filteredData.push(data[i]);
}
}
return filteredData;
}
This snippet is based on an assumption all records are String type
if (materialbezeichnung1.includes(materialbezeichnung1Value) && artikelnummer.includes(artikelnummerValue) && materialbezeichnung2.includes(materialbezeichnung2Value) && teileart.includes(teileartValue) &&
materialklasse.includes(materialklasseValue) && artikelstatus.includes(artikelstatusValue)) {
filteredData.push(data[i]);
}
If you're relying on a String function, then force the value type to String using String(var)
if (String(materialbezeichnung1).includes(materialbezeichnung1Value) && String(artikelnummer).includes(artikelnummerValue) && String(materialbezeichnung2).includes(materialbezeichnung2Value) && String(teileart).includes(teileartValue) &&
String(materialklasse).includes(materialklasseValue) && String(artikelstatus).includes(artikelstatusValue)) {
filteredData.push(data[i]);
}

Jquery loop compare not getting expected result [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my code. I will explain everything step by step :
myobj ={ month: 1, total: 2 }{ month: 3, total: 1 }
newArray = [];
for(i=0;i<=5;i++){
$.each(myobj.function(k,v){
if(i==v.month){
newArray.push(v.month)
}else{
newArray.push(0)
}
})
}
After what I am getting is : 1,0,0,0,0,3,0,0,0,0
expected output : 1,0,3,0,0
I don't know what I am missing here. Can anyone please help me related this? I am stuck here
Your inner loop is pushing onto the new array for every item in the array, not just if the desired month is found.
Don't use an inner loop. Use find() to find the matching month, and push 0 if you don't find it.
for (i = 1; i <= 5; i++) {
if (myobj.find(el => el.month == i)) {
newArray.push(i);
} else {
newArray.push(0);
}
}
if you want to push the totals instead of the months, assign the result of find() to a variable so you can get the total from it.
for (i = 1; i <= 5; i++) {
var found = myobj.find(el => el.month == i);
newArray.push(found ? found.total : 0);
}

I need to sort out this algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
could you please your help.
how to answer this question ?
I need your help thinks
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * n ;
}
}
factorial(6)
this is not right.. i need to use writing code with Loop javascript.
Get the factorial of n. The inheritance of n is the number 1 through n multiplied by all numbers.
example)
answer should be like this
factorial(4); // => 24
factorial(5); // => 120
One way is to use a simple for loop.
var answer = 1;
for(var i = 2; i <= n; i++)
{
answer *= i;
}
return answer;
You can also do this recursively by multiplying your input by the result of the previous value.
function factorial(n){
if(n <= 1) return 1;
return n * factorial(n-1);
}

How to calculate the correct characters the user typed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
[Ask]
anyone know the reference for a case like this?
the user must type the word: "learn" -> total character 5
while the user types instead: "l3arn"
well I want to count the right word only, so the result 4
from google not yet find a solution, maybe someone knows the keyword/reference for the problem.
I want to implement it in javascript
You want to calculate the number of characters in the correct position?
In that case, it's a simple solution.
Javascript example:
function countCorrectCharacters(expectedString, string) {
var count = 0;
var l = Math.min(expectedString.length, string.length);
for (var i = 0; i < l; ++i) {
if (expectedString[i] === string[i]) {
++count;
}
}
return count;
}
var str = "learn";
var userInput = "l3arn";
var userInputArray = userInput.split('');
var counter = 0;
for(var i = 0; i< "l3arn".lenth(); i++){
if(str.indexOf(userInputArray[i]) !== -1) counter++;
}
If I understand correctly you want to count the number of letters in a string? Use this:
function getNumberOfLetters(string) {
let match = string.match(/[A-Za-z]/g);
return (match && match.length) || 0;
}
console.log(getNumberOfLetters("learn")); // 5
console.log(getNumberOfLetters("l3arn")); // 4
console.log(getNumberOfLetters("12345")); // 0

Brief explanation of the return statement in the given javascript code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Can anybody give me the brief explanation of the return statement in the given javascript code.
Please you should see my fiddle : http://jsfiddle.net/m6mxdt9u/
function isPrime(value) {
if (!isPrime.answers)
isPrime.answers = {};
if (isPrime.answers[value] != null) {
return isPrime.answers[value];
}
var prime = value != 1; // 1 can never be prime
for (var i = 2; i < value; i++) {
if (value % i == 0) {
prime = false;
break;
}
}
return isPrime.answers[value] = prime;
}
assert(isPrime(5), "5 is prime!");
assert(isPrime.answers[5], "The answer was cached!");
function assert(value, desc) {
var resultsList = document.getElementById("results");
if (!resultsList) {
resultsList = document.createElement('ul');
document.getElementsByTagName('body')[0].appendChild(resultsList);
resultsList.setAttribute('id', 'results');
}
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
resultsList.appendChild(li);
}
I struggled in this line :
return isPrime.answers[value];
What will be the return value of the above code and where does it go?
If you're asking about this particular part of the code:
if (isPrime.answers[value] != null) {
return isPrime.answers[value];
}
Then it'll return whether value is prime or not, based on the cached value (value saved on the answers hash). It'll be the value returned by the isPrime function.

Categories

Resources