Loop check if on last iteration? - javascript

How do I check if I'm on the last iteration of this loop? I'm sorry for asking this question. I'm used to programming in VB.NET and javascript seems very cryptic by nature.
if (QuerySplit.length > 1) {
var NewQuery
for (i=0; i<QuerySplit.length; i++)
{
// if we're not on the last iteration then
if (i != QuerySplit.length) {
// build the new query
NewQuery = QuerySplit[i].value + " AND "
}
}
}

Your i is always smaller than QuerySplit.length - that's your loop condition. In the last iteration it will have a value of QuerySplit.length-1, that's what you can check against:
if (i < QuerySplit.length - 1)
Btw, you'd do better to use the join Array method for what you're trying to do:
var NewQuery = QuerySplit.map(function(x){return x.value;}).join(" AND ");

Take note that you need var NewQuery = ""; and check for length - 1. Also, the last if statement is just a guess of what you probably want to do:
if (QuerySplit.length > 1) {
var NewQuery = "";
for (i = 0; i < QuerySplit.length; i++) {
// if we're not on the last iteration then
if (i != QuerySplit.length - 1) {
// build the new query
NewQuery += QuerySplit[i].value + " AND "
} else {
NewQuery += QuerySplit[i].value;
}
}
}
If QuerySplit.length is 4, then:
0, 1, 2, 3
...are the indexes. So you want to check for when the index is 3 and that's your last iteration.

The array is 0-based. This means if there are 3 items in the array, your indexes will be 0,1,2. The last one is one less than the length.
You'll have to check like this: (i < QuerySplit.length -1)

Related

Creating new array from unique elements found in array

I was given an assignment:
Finding unique elements in an array and creating a new array from these unique elements.
The professor gave us the pseudocode to code this assignment - it should be straightforward but my code is not working.
Here is my attempt:
// search for unique birthdays in the array
function find(birthdays) {
var uniqueBirthdays = [];
for (var i = 1; i <= birthdays.length; i = i + 2) {
var count = 0;
for (var j = 1; j <= birthdays.length; j = j + 2) {
if (birthdays[i] == birthdays[j]) {
count++;
}
}
if (count == 1) {
var n = uniqueBirthdays.length;
uniqueBirthdays[n] = birthdays[i - 1];
}
}
return uniqueBirthdays;
}
I have tried checking for indentation errors as well as a number of other things but can not figure out why as the array is traversed it is giving each element a count of only 1 (meaning there are no matching elements) - it does not seem to be traversing the array more than once so no elements have a count greater than 1 - even though I am using nested for loops.
I have increased the intervals by 2 because I need to compare every other element - there is a number assigned to each birthday so the array may look like:
['0001'][12/15]['0002'[03/12]...
I am brand new so I may be overlooking simple but ive tried so many things and i can not understand why this code isnt working - it is returning back all of the elements that are assigned to the birthdays instead of just the unique ones.
Any help that will point me in the right direction is very much appreciated.
You were very close, and there were just a couple mistakes. The only things that did not work were the way you wrote your for loops:
for (var i = 1; i <= birthdays.length; i = i + 2) {
Array indexes start at 0, so if you want to process the first element, use var i = 0;
Since these indexes start at 0, for an Array of 3 elements, the last index is 2. So you only want to run your loop while i is less than the array length: i < birthdays.length
You were skipping elements by doing i = i + 2. There seems to be no reason for it?
Something else worth mentionning: in JS, indentation does not matter - well, it does, but only to avoid making your eyes bleed. In fact, most websites use minified versions of their code, which fits on a single (often very long and ugly) line (example).
Here is your code, with only two lines fixed:
function find(birthdays) {
var uniqueBirthdays = [];
for (var i = 0; i < birthdays.length; i = i + 1) { // <-----
var count = 0;
for (var j = 0; j < birthdays.length; j = j + 1) { // <-----
if (birthdays[i] == birthdays[j]) {
count++;
}
}
if (count == 1) {
var n = uniqueBirthdays.length;
uniqueBirthdays[n] = birthdays[i];
}
}
return uniqueBirthdays;
}
// I used letters instead of birthdays for easier demo checking
var birthdays = ['a', 'b', 'a', 'c'];
console.log( find(birthdays) ); // ["b", "c"]
JS have direct methods tor that use Array.indexOf(), Array.lastIndexOf() and Array.filter()
uniques elements have same first position and last position
sample code:
const initailArray = [...'ldfkjlqklnmbnmykdshgmkudqjshmjfhmsdjhmjh']
const uniqueLetters = initailArray.filter((c,i,a)=>a.indexOf(c)===a.lastIndexOf(c)).sort()
console.log(JSON.stringify(uniqueLetters))

Understanding how the JS engine is looping through to get the index of an array element

I wrote a function to output the names and real-life indexes of people waiting in a line.
var line = ["Sarah", "Mike", "Bob"];
function currentLine(line) {
if (line.length === 0) {
document.write(`The line is currently empty.`);
} else {
var array = [];
for (var i = 0; i < line.length; i++) {
array.push(` ${line.indexOf(line[i+1])}. ${line[i]}`);
}
document.write(`The line is currently:` + array);
}
}
currentLine(line);
When I run the function, the output is:
The line is currently: 1. Sarah, 2. Mike, -1. Bob
How is the JavaScript engine interpreting the loop? How is Bob -1? Last time I checked 2 + 1 = 3.
I want to fix this myself but I'm trying to understand what is going on in this seemingly straight forward loop.
The answer is right in your analysis:
How is Bob -1? Last time I checked 2 + 1 = 3
On the third iteration of the loop, i = 2. This line executes with i = 2:
line.indexOf(line[i+1])
So what does this say? It says get me the element at position (i + 1), which is position 3, or the forth element. There is no forth element, so line[i+1] is undefined.
Pass that to the indexOf call and what you are saying is, find me the position of undefined in the line array. line contains 'Sarah', 'Mike', and 'Bob'. It does not contain undefined.
I made a small change to the array.push line and it works correctly now:
var line = ["Sarah", "Mike", "Bob"];
function currentLine(line) {
if (line.length === 0) {
document.write(`The line is currently empty.`);
} else {
var array = [];
for (var i = 0; i < line.length; i++) {
array.push(` ${i+1}. ${line[i]}`);
}
document.write(`The line is currently:` + array);
}
}
currentLine(line);
The problem with ${line.indexOf(line[i+1])} is that on the last iteration, where i is 2, it checks for line[3]. That doesn't exist so it spit's out a -1, as that is the return value of indexOf if something doesn't exist. Just go with this:
array.push(` ${i+1}. ${line[i]}`);
This just prints i + 1, instead of looking for index.
var line = ["Sarah", "Mike", "Bob"];
function currentLine(line) {
if (line.length === 0) {
document.write(`The line is currently empty.`);
} else {
var array = [];
for (var i = 0; i < line.length; i++) {
console.log(line.indexOf(line[i+1])); //On last iteration, it's -1 because it's not found in the array!
array.push(` ${i+1}. ${line[i]}`);
}
document.write(`The line is currently:` + array);
}
}
currentLine(line);
I fixed the loop. I overcomplicated the indexing:
${line.indexOf(line[i+1])}
which should be:
${[i+1]} - is that legitimate syntax?
Also, if anyone could shed light on what my incorrect code was doing and how JS was iterating, I would greatly appreciate it.

Is this the right way to iterate through an array?

Here is the code in question:
var L1 = [];
var Q1 = [];
function populateListOne() {
var limit = prompt("please enter a number you would like to fill L1 to.");
for (i = 2; i <= limit; i++) {
L1[i] = i;
}
for (n = 2; n <= L1.length; n++) {
var count = 2;
if (n == count) {
var index = L1.indexOf(n);
L1.splice(index, 1);
Q1[n] = n;
count = count + 1;
}
for (j = 0; j <= L1.length; j++) {
if (L1[j] % 2 == 0) {
var secondIndex = L1.indexOf(j);
L1.splice(secondIndex, 1);
}
}
}
document.getElementById("demo").innerHTML = "iteration " + "1" + ": " + L1 + " Q1 = " + Q1;
}
I’m currently working on a homework assignment where I have to setup a queue. All is explained in my JSFiddle.
Problem description
Essentially, the part I’m stuck on is iterating through each instance of the array and then taking the value out if the modulus is identical to 0. However, as you can see when I run the program, it doesn’t work out that way. I know the problem is in the second for loop I just don’t see what I’m doing wrong.
The way I read it is, if j is less than the length of the array, increment. Then, if the value of the index of L1[j] modulus 2 is identical to 0, set the value of secondIndex to whatever the index of j is. Then splice it out. So, theoretically, only numbers divisible by two should be removed.
Input
A single number limit, which will be used to fill array L1.
L1 will be initialized with values 2, 3, ... limit.
Process
Get the starting element of array L1 and place it in array Q1.
Using that element, remove all values in array L1 that are divisible by that number.
Repeat until array L1 is empty.
You're going to have issues with looping over an array if you're changing the array within the loop. To help with this, I tend to iterate from back to front (also note: iterate from array.length - 1 as the length element does not exist, arrays are key'd from 0):
for(j = L1.length - 1; j >=0 ; j--)
For your first loop, you miss the elements L1[0] and L1[1], so I would change the first loop to:
L1 = [];
for(i = 2; i <= limit; i++)
{
L1.push(i);
}
In this section:
for(j = 0; j <= L1.length; j++){
if(L1[j] % 2 == 0)
{
var secondIndex = L1.indexOf(j);
L1.splice(secondIndex, 1);
}
}
you should splice with j instead of secondIndex.
Change L1.splice(secondIndex, 1); to L1.splice(j, 1);
Array indices and putting entries
You initial code used an array that was initialized to start at index 2. To avoid confusion, of what index to start at, start with index 0 and iterate until array.length instead of a predefined value limit to ensure that you go through each element.
The following still works but will be more of a headache because you need remember where to start and when you will end.
for (i = 2; i <= limit; i++) {
L1[i] = i; // 'i' will begin at two!
}
Here's a better way:
for (i = 2; i <= limit; i++) {
// 'i' starts at 2 and since L1 is an empty array,
// pushing elements into it will start index at 0!
L1.push(i);
}
Use pop and slice when getting values
When you need to take a peek at what value is at the start of your array, you can do so by using L1[0] if you followed my advice above regarding array keys.
However, when you are sure about needing to remove the starting element of the array, use Array.slice(idx, amt). idx specifies which index to start at, and amt specifies how many elements to remove beginning at that index (inclusive).
// Go to 1st element in L1. Remove (1 element at index 0) from L1.
var current = L1.splice(0, 1);
Use the appropriate loops
To make your life easier, use the appropriate loops when necessary. For loops are used when you know exactly how many times you will iterate. Use while loops when you are expecting an event.
In your case, 'repeat until L1 is empty' directly translates to:
do {
// divisibility checking
} while (L1.length > 0);
JSFiddle
Here's a complete JS fiddle with in-line comments that does exactly what you said.

how to check which elements of an array match relative to position

Trying to create a function or two that will be able to check the elements of an array and output wheater the elements of the two arrays are identical (ie same number and identical position is present), or the number is present but does not match the same position as the other array. Basically, I'm attempting to recreate a simple game called mastermind. The main problem im having is a case senarior when say the right answer is [1,2,3,4] and the user will guess [0,1,1,1], my function will out put that the number 1 is present 3 times, and I need to figure out how to just have it say the number 1 is present 1 time. Here is the function that checks the arrays:
function make_move(guess, answ){
var myguess = document.getElementById("mymoves");
var correct_number_correct_spot= 0;
var correct_number_wrong_spot= 0;
for(var i = 0; i < 4; ++i)
{
if(answ[i] == guess[i]){
++correct_number_correct_spot;
}
else if(answ[i] !== guess[i] && $.inArray(guess[i], answ) !== -1){
++correct_number_wrong_spot;
}
}
console.log(answ);
console.log(guess);
myguess.innerHTML += correct_number_correct_spot + " were right!" +correct_number_wrong_spot+ "there but not in the right order";
}
You can keep the count of missed numbers in an object, and subtract the guessed ones that appear in the answer. Then you can calculate the correct_number_wrong_spot subtracting the number of correct_number_correct_spot and the missed ones.
function make_move(guess, answ){
var myguess = document.getElementById("mymoves");
var correct_number_correct_spot = 0;
// Initialize missed counts to the numbers in the answer.
var correct_number_wrong_spot = answ.length;
var missed = {};
for (var j = 0; j < answ.length; j++) {
missed[answ[j]] = (missed[answ[j]] || 0) + 1;
}
for(var i = 0; i < answ.length; ++i)
{
if(answ[i] == guess[i]){
++correct_number_correct_spot;
}
// Subtract the guessed numbers from the missed counts.
if (guess[i] in missed) {
missed[guess[i]] = Math.max(0, missed[guess[i]] - 1);
}
}
// Subtract the correctly spotted numbers.
correct_number_wrong_spot -= correct_number_correct_spot;
// Subtract the remaining missed numbers.
for (var number in missed) {
correct_number_wrong_spot -= missed[number];
}
console.log(answ);
console.log(guess);
myguess.innerHTML += correct_number_correct_spot + " were right!" +correct_number_wrong_spot+ "there but not in the right order";
}
Check demo
EDIT: My try to explain doubts exposed in the comments:
would you mind explining how this code works: for (var j = 0; j < answ.length; j++) { missed[answ[j]] = (missed[answ[j]] || 0) + 1; }
missed[answ[j]] = (missed[answ[j]] || 0) + 1;
This is a quick way to increment the count for a number or initialize it to 0 if it doesn't exists yet. More or less the statement works like this:
If missed[answ[j]] is undefined then it is falsy and hence the || (or operator) evaluates to the 0. Otherwise, if we already have a value greater than 0, then it is truthy and the || evaluates to the contained number.
If it looks weird, you can replace this line with:
if (!(answ[j] in missed)) {
missed[answ[j]] = 0;
}
missed[answ[j]] += 1;
also if (guess[i] in missed) { missed[guess[i]] = Math.max(0, missed[guess[i]] - 1);
missed[guess[i]] = Math.max(0, missed[guess[i]] - 1);
In this case I use Math.max to make sure we don't subtract below 0. We don't want repeated numbers in the guess that exceeds the number of those present in the answer count. I mean, we subtract at most until the number of repeated numbers in the answer.
if (missed[guess[i]] > 0) {
missed[guess[i]] -= 1;
}
Try this fiddle!
Without changing your original function too much, you can use an object as a map to keep track of which numbers you have already matched.
var number_matched = {};
// ...
if(!number_matched[guess[i]]) {
number_matched[guess[i]] = true;
}

How to get the position of the second occurrence?

Let's say I have this array:
var a = [1,2,99,3,4,99,5];
I would like to get the position of the second 99, something like:
a.indexOf(99, 2) // --> 5
However the second argument in indexOf just tells where to start the search. Is there any built in functions to make this? If not how would you do it?
Thanks!
There's only indexOf and lastIndexOf. You could loop over it:
var a = [1,2,99,3,4,99,5];
var matches = []
for (var i=0; i<a.length; i++){
if (a[i] == 99) {
matches.push(i)
}
}
console.log(matches); // [2, 5]
If you always want the second occurrence Jan's method is also good:
a.indexOf(99, a.indexOf(99) + 1)
The indexOf call on the right finds the first occurrence, +1 then limits the search to the elements that follow it.
There is no built in function, but you can easily create your own, by iteratively applying indexOf:
function indexOfOccurrence(haystack, needle, occurrence) {
var counter = 0;
var index = -1;
do {
index = haystack.indexOf(needle, index + 1);
}
while (index !== -1 && (++counter < occurrence));
return index;
}
// Usage
var index = indexOfOccurrence(a, 99, 2);
But Matt's solution might be more useful.

Categories

Resources