Novice Javascripter, Looking for advice

As recommended in the thread I made a few days ago, I started taking the courses for JS on codecademy, and it's great! I've learned so much in just a few days, though I already had a basic understanding of programming since I also know a bit of ruby.

In order to pass time, I go a bit out of my way on codecademy's interactive tutorials, and on the "breather" pages I start to program random small things. Here's what I wrote for this particular piece of advice:
 

Code:
var map = [    [1, 2, 3], //Y1    [4, 5, 6], //Y2    [7, 8, 9]  //Y3];var locate = [1, 1];var callMap = function(){    for (var loc1 = 0; loc1 < map.length; loc1++){        if (locate[0] == loc1){            for (var loc2 = 0; loc2 < map[loc1].length; loc2++){                if (locate[1] == [loc2]){                    map[loc1][loc2] = "here";                } else {                    map[loc1][loc2] = map[loc1][loc2];                }            }        }    }    for (var i = 0; i < map.length; i++){        console.log(map[i]);    }};
for (var i = 0; i < map.length; i++){
console.log(map);
}
*facepalm* Oh my gawd, I can't believe I forgot that. Thanks!

EDIT: Alright, that fixed the display problem. But it's not changing the element to read "here" when I call the map.
Yeah, the upper part is a little wrong too. First of all, what are the three numbers inside each array on the "map" array?
They're supposed to be coordinates. For example, the location variable is set to [0, 1], so that location on the "map" is supposed to be map[0][1]. 

What I'd like to display when calling the map is [1, "here", 3] if I call the first element inside the map array.
Soo.. on this line:

[1, 2, 3],
You're configuring a position as X = 1 and Y = 3. And the "2" is what is going to be displayed? Then you want the number 2 to be replaced by "here" when the player goes to the tile 1, 3 ?
No no, If the player is on position 0, element 1, It should change it from "2" in the first array, to "here". 

The map looks like this:

    X  X  X

Y [1, 2, 3],
Y [4, 5, 6],
Y [7, 8, 9]

Due to the way programming index's from 0, [0, 1] on the map is actually [Y1, X2]. If the player is standing on those coordinates on the map, that location should change from "2" to "Here" to show that's where the player is currently at.

EDIT: I should add, I know it's not laid out like a proper coordinate grid, I made it numbers 1-9 just so I know it's accessing the right information.
Ahh, got it.

Basically you're not comparing the positions.

Instead of:

if (location[loc1]){Use

if (location[0] == loc1)

You also need to change this line:

if (location[loc1][loc2]){

By the way, if you're testing this on a browser, I suggest changing the name of the location variable, because the browsers already have a variable called location.
Ahh thank you! That did it! 🙂 And I changed the variable, thanks for the heads up.
for (var i = 0; i < map.length; i++){

console.log(map);

}
 

 

why not: 

var something = map.map(function(mapArray){ return _.contains(value, mapArray) });console.log(something);// ????Its cleaner and simpler. There might be a reason behind your solution that I over looked but I never trust for loops unless I have too 😛
why not: 

var something = map.map(function(mapArray){ return _.contains(value, mapArray) });console.log(something);// ????Its cleaner and simpler. There might be a reason behind your solution that I over looked but I never trust for loops unless I have too 😛
I'm not familiar with underscore.js, so I wouldn't have been able to come up with something like that. 🙁
I'm not familiar with underscore.js, so I wouldn't have been able to come up with something like that. 🙁

.map walks over an array of objects or arrays. so in this case mapArray would be  the individual arrays. _.contains() checks to see if the value you are looking for out out of a specific array exists. The only piece of underscore here is the _.contains.
If you don't want to use any library (because you won't for most of the cases)

var something = map.map(function(array){ return array.indexOf(value) !== -1; }); //indexOf returns -1 if not foundAnyway, for loops are used most of the time for performance reasons (or because people don't know about .map, .forEach, etc), because using a for loop is (for some strange reason) still faster, even when probably the map implementation is made in the VM.

If you want to compare, you can always do that from here. I know than performance should never be done prematurely, but that doesn't always apply on videogames, after all we have to do all the updates and checks in 16ms.
If you don't want to use any library (because you won't for most of the cases)

var something = map.map(function(array){ return array.indexOf(value) !== -1; }); //indexOf returns -1 if not foundAnyway, for loops are used most of the time for performance reasons (or because people don't know about .map, .forEach, etc), because using a for loop is (for some strange reason) still faster, even when probably the map implementation is made in the VM.

If you want to compare, you can always do that from here. I know than performance should never be done prematurely, but that doesn't always apply on videogames, after all we have to do all the updates and checks in 16ms.
Interesting, I didn't know about .map, but like I said... novice and all that. Is there any like... security issues with using a for loop instead of .map? Or does it come down to preference at that point?
for me its preference. How ever in this case .map is the thing you want because with out it you would have a nested for loop with an if statement and the above code is easier to read, easier to understand and easier to debug.
for me its preference. How ever in this case .map is the thing you want because with out it you would have a nested for loop with an if statement and the above code is easier to read, easier to understand and easier to debug.
I don't think I understand how .map and .indexOf work, because I can't get it to work the same way this works:

var callMap = function(){ for (var loc1 = 0; loc1 < map.length; loc1++){ if (locate[0] == loc1){ for (var loc2 = 0; loc2 < map[loc1].length; loc2++){ if (locate[1] == [loc2]){ map[loc1][loc2] = "here"; } else { map[loc1][loc2] = map[loc1][loc2]; } } } } for (var i = 0; i < map.length; i++){ console.log(map); }};The above code works exactly the way I want it to, but it could be my lack of understanding JS, however I tried using this:

var callMap2 = map.map( function(array){ return array.indexOf(locate) !== -1; });and it just returns [false, false, false]

instead of the full map with the location being reassigned to "here"
.map will return a new array of elements

.indexOf will return the index of the element in the array of -1

If you want to find the index of or at least validate (with out underscore) that set of locations presented in the array of locate exist any where in any of the map arrays you can write:
 

var mapElement = map.map(function(mapArray){ var locateElement = locate.map(function(singleElement){ var elementIndex = mapArray.indexOf(singleElement); if (elementIndex !== -1) { console.log(elementIndex); } else { console.log('Element Index was -1 and thus does not exist in: ' + mapArray); } });});// Given the data:map = [ [1,2,3], [4,5,6], [7,8,9]];locate = [1,2];// We get the output:0 // Found1 // Found2VM1082:16 Element Index was -1 and thus does not exist in: 4,5,6 // Not found2VM1082:16 Element Index was -1 and thus does not exist in: 7,8,9 // Not found.What are we doing here? .map is an easier and more readable way of looping over arrays in Javascript. I lined to the documentation above. And we know the indexOf Will show you the index that the element was found at. 

So we loop over your map array, we then loop over the location array, we ask each of the nested arrays in the map array what the index of the values in the location array is and if they are not -1 then out put the index and if it is we "throw an error"

But what is 0 and 1?

That states the index, so the value of 1 in the locate array is at index 0 in the first array of the map array, and the value of 2 in the locate array is the index of 2 in the first array of of the map array.

Its not exactly clear what you are trying to do though .... 
What I'm doing here is basically just creating a map that would change an element based on the location (locate variable) of the player. Think like a text based RPG. The map looks like this:

[1, 2, 3],

[4, 5, 6],
[7, 8, 9]

The locate variable is currently set to 1, 1. That would be position 5 on the map, since the player is at that position currently, whenever the callMap function is used, it would display like this:

[1, 2, 3],

[4, "Here", 6],
[7, 8, 9]

Replacing 5 on the map with the string "here" to show that's where the player is at. The code I have works exactly like this, but since I want to learn more about JS and get more advanced, I'd like to also make it work with the suggestions you guys are making. I just don't seem to understand what I'm doing with your suggestions.
Ok here you go:

Code:
map = [  [1,2,3],  [4,5,6],  [7,8,9]];var locationToChange = [2, 8];var text = 'Yo Dawg';var mapArrayElement = map[locationToChange[0]]; // [7,8,9]var mapIndexOfMapArrayElement = map.indexOf(mapArrayElement); // 2// Walk over this array of elements.// You would want to make sure that mapArrayElement !== -1 in the future.var elementToChange = mapArrayElement.map(function(arrayElement){    // Did we find the element we want to change?    if (arrayElement === locationToChange[1]) {        // What is its index?        var indexOfMatchingElement = mapArrayElement.indexOf(arrayElement);        // Change it to the text supplied        mapArrayElement[indexOfMatchingElement] = text;        // return it, remember that we do get an array back with .map        return mapArrayElement;    }});// So now we have an array: [undefined, Array[3], undefined] What now?// Why do we have undefined? Because we walked through the array 3 times, changed one element and returned that// whole array back. Two of those times the value we wanted didn't exist so we get "undefined"// Now we want to change the main array, so walk through the element to change array we go back from .mapfor (var i = 0; i < elementToChange.length; i++) {    // You are not undefined right?    if (elementToChange[i] !== undefined) {        // We have the index from above, so replace the element at that index.        map[mapIndexOfMapArrayElement] = elementToChange[i];    }}console.log(map);// Now you have:map = [  [1,2,3],  [4,5,6],  [7,'Yo Dawg',9]];

Original thread: https://forums.rpgmakerweb.com/threads/novice-javascripter-looking-for-advice.45976/