NEWBIE JAVASCRIPT ARRAY QUESTION. PLEASE HELP!?

Question by unibumps:
Newbie JavaScript Array question. Please help!?
I’m new to JavaScripting and I am currently just trying out new things and seeing how they function to get a feel for the script syntax and functionality. With the script listed below, I was trying to create an array that would match up a person’s name with their job title, and when the user entered the user’s name into the text box and hit the submit button, it would pop up an alert with stated person’s job title.
Sounds simple enough, right? Well, I have been stumped for the past five hours, and no tutorial sites have given any clue as to how I can remedy this. I am trying to keep the literal notation (just to see how it works), but would not mind a dense notation if it’ll help me figure it out!
What it’s doing currently is looping through the array and displaying ALL of the names and then ALL of the job titles, and then changes the text box’s value to “John” or Engineer”, and I have tried numerous variations of the loop and if…else loops (including a “while” loop) and NO closer to a conclusion.
Thank you in advance!
Hopefully one day soon I will be laughing at questions like this.
Code (including basic body HTML):
- John
- Robert
- Felicia
- Jose
UPDATE:
I have since updated the JavaScript portion of the header to look as it does below (splitting the array into two separate arrays), but the only name that brings back the correct job is John (Engineer). The rest return the alert as "undefined".
------------------------------------------
Answer by Silent
Well, for starters, you need to learn the difference between = and ==.
In JavaScript, the = operator is used to assign a value to a variable. The == operator is used to test whether two values are equal. So, in this line:
if (formTest.value = nameJob[i])
you should be using == instead of =.
Secondly, I am not sure why you have both the names and jobs arrays as elements of another array. This would be much easier if you kept the two arrays separate. As long as they are the same length, you can use the same index for both of them. So, for example, you could loop over the names array and, when you find the correct name, use the current value of "i" as the index for the jobs array.
Edit:
Again, you want to use the variable i as the index for the jobs array, not i * nameName[i].length + j. You also only need one loop if you are doing it this way. Look carefully at the code and make sure you comprehend what it's doing; do not just guess.
------------------------------------------
Give your own answer to this question below!









about 1 year ago
Check that the file subdirectory. [where "subdirectory" is the name of the subdirectory] (a dot after the backslash) exists. It appears to be a file if the subdirectory itself exists. (This will work in MacOS, Windows, Linux, FreeBSD, Unix …)
about 1 year ago
read -p “Enter a subdirectory: ” SUBDIR
if [ ! -d "$SUBDIR" ]; then
echo “not valid”
exit 1
fi
ls $SUBDIR
Could also do something like:
ls $SUBDIR 2>/dev/null || echo “not valid”; exit 1