Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
A little confused, I have the below code that loops through an array and if the value is not in another array push it in. But whether the indexOf statement evaluates to true or false it gets pushed into the array - not quite sure why that is.
function findUnique(fieldId) {
let uniques = [];
for (let i = 0; i < data.length; i++){
console.log(uniques.indexOf(data[i][fieldId]) === -1);
if (uniques.indexOf(data[i][fieldId] === -1)) {
uniques.push(data[i][fieldId]);
}
}
return uniques;
}
Say there are 2 items in data...
first pass through will log out -1 because it is not in the array, then pushes to array...
second pass logs out 0 because it is in the array but all pushes it into the array...
uniques logs out as ['a', 'a']
Just a typo. You're currently looking for false in your array.
Change
if (uniques.indexOf(data[i][fieldId] === -1)) {
to
if (uniques.indexOf(data[i][fieldId]) === -1) {
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
var legalCharactersForSaveCode = ["B","C","D","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "+", "/",];
var saveCode = [];
function generateSaveCode() {
for(var c = 0; c > 30; c++){
v = Math.round(Math.random() * legalCharactersForSaveCode.length);
saveCode.push(legalCharactersForSaveCode[v]);
}
saveCode.push(store.get("money"));
alert(saveCode);
}
I'm trying to make it so it pushes a random character.
When I ask it to alert the saveCode array all it does is alert the money.
(I'm using store.js)
I was expecting for it to push a random characters from the legalCharacters array into the saveCode array.
Your "for" loop does not work as the condition is c > 30, and should be c < 30
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
i am trying to filter the unique ones from a given array using filter method but its throwing an empty array, here is the code
function dual(a) {
if (Array.isArray(a)) {
let unique = a.filter((val, index) => {
a.indexOf(val) == index
})
return unique;
}
}
console.log(dual([3, 1, 1, 2, 2])) //expected result [3,1,2] but showing []
You may use Set() to create a unique array:
const arr = [3,1,1,2,2]
var uniqueArray = [...new Set(arr)]
console.log(uniqueArray)
You are missing return statements from both your function and your filter array method.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple code, its essence is to remove duplicate numbers from a sheet. But on my server, the data received does not match the data on leetcode. For what reason could this be?
var deleteDuplicates = (head) => {
for(let i = 0; i < head.length; i++) {
for(let a = i + 1; a < head.length; a++) {
if(head[i] === head[a] ) {
head.splice([a], 1)
}
}
}
return head;
};
console.log(deleteDuplicates([1,1,2]));
Output: [1,2]
Leetcode:
Your input
[1,1,2]
Output
[1,1,2]
Expected
[1,2]
How about using a Set? It's simpler and you'll avoid modifying a list that you're iterating over.
var deleteDuplicates = (head) => [...new Set(head)];
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why does this if statement always log true no matter how long pin actually is?
const pin = 1
if (pin.toString().length = 4 || 6) {
console.log(true);
}
//logs true
Edit: If a mod sees this, should I delete this question? I was very bad at javascript when I asked this.
Both statements in your || (or) statement will resolve to true, so the log will always be called.
pin.toString().length = 4
resolves to true because you're SETTING the length to 4 and then the check becomes 'is there a length' which is only falsy if the length === 0.
The second part of the equality is simply '6'. Any number that's not 0 is truthy, so will resolve to true.
You probably mean something like this:
const pin = 1;
if (pin.toString().length === 4 || pin.toString().length === 6) {
console.log(true);
}
This will never log true, because '1'.length === 1.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to use a for loop to display all the elements in my array that are greater than 5. I am using an if-statement to determine the values greater than five, but when I run the code, I get values printed to the console that are both below and over five. I have tried creating variables to store the index values and tried using the && operator, but none of these have worked.
Here is the code for reference:
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Original: " + myArray);
console.log("Values greater than 5");
// the part I have issues with
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] > 5) {
console.log(i);
}
}
You are printing i not the array element.
try
console.log(myArray[i]);