[SOLVED]javascript question

Forum rules
Before you post please read how to get help
Post Reply
User avatar
boyo1991
Level 1
Level 1
Posts: 21
Joined: Mon Jul 30, 2018 7:24 am

[SOLVED]javascript question

Post by boyo1991 » Mon Aug 20, 2018 1:03 pm

yes, I am working in javascript. Ive got a very small pet project that I am working on, that just seems would work better in javascript for my purposes. However, I am having a small issue.

Code: Select all

var DB = new Array();
DB[0] = new Array();
DB[0][0] = 'hello';
DB[0][1] = 'world';

DB[1] = new Array();
DB[1][0] = 'goodbye';
DB[1][1] = 'dave';


var search = prompt('query:');
var i;

for(i=o; i <= DB.length(); i++) {
if (search == DB[i][0]) {
prompt(DB[i][1]);
} else {
prompt('could not locate');
}
Im trying to get a box to popup and ask for an input, after which point it will search if your request matches any DB[0] and if it does it will returen DB[1] in another prompt box.

anyone got some ideas?
Last edited by boyo1991 on Wed Aug 22, 2018 1:19 pm, edited 1 time in total.

gm10
Level 10
Level 10
Posts: 3409
Joined: Thu Jun 21, 2018 5:11 pm

Re: javascript question

Post by gm10 » Mon Aug 20, 2018 1:30 pm

boyo1991 wrote:
Mon Aug 20, 2018 1:03 pm
anyone got some ideas?
Sure, use an IDE that highlights your mistakes for you. Your code:

Code: Select all

for(i=o; i <= DB.length(); i++) {
what it should be:

Code: Select all

for(i=0; i < DB.length; i++) {
or even better for performance reasons:

Code: Select all

for(var i = 0, len = DB.length; i < len; i++) {
Also you probably wanted to use alert() rather than prompt() in that loop.

User avatar
boyo1991
Level 1
Level 1
Posts: 21
Joined: Mon Jul 30, 2018 7:24 am

Re: javascript question

Post by boyo1991 » Wed Aug 22, 2018 11:57 am

hi there,

its sort of my 'style' to use prompts. (especially since i mainly write programs for myself) I fixed the bad syntax errors a bit ago and that didnt help. Someone on reddit basically re wrote the script and took it into a totally different line of thought. Which worked, sort of... in that it made it work once but then you needed to refresh the page if you wanted to search again.... (could just add a recursive function, im okay with that :P)

anywho any ideas? i want to use nested arrays for this in particular. They are visually appealing to me.

gm10
Level 10
Level 10
Posts: 3409
Joined: Thu Jun 21, 2018 5:11 pm

Re: javascript question

Post by gm10 » Wed Aug 22, 2018 12:23 pm

I don't understand what you are asking now. I already gave you the answer why the code in the OP didn't work. Here's your code, properly cleaned up:
https://jsfiddle.net/ph4fxtd1/20/

It's quite obvious that you'll need to put this into a function if you want to run this more than once, and there would be nothing recursive about it. Consider finding a book or a good tutorial on javascript and/or programming in general. Once you understand the basic concepts it will make things much easier for you in the future. Good luck! :)

edit: I'll add the cleaned up code here as well in case somebody comes looking for this in the future when the linked fiddle has gone away:

Code: Select all

var DB = [
  ['hello', 'world'], 
  ['goodbye','dave']
];


var search = prompt('query:');

for (var i = 0, len = DB.length; i < len; i++) {
  if (search == DB[i][0]) {
    prompt(DB[i][1]);
  } else {
    prompt('could not locate');
  }
}
Last edited by gm10 on Wed Aug 22, 2018 1:27 pm, edited 1 time in total.

User avatar
boyo1991
Level 1
Level 1
Posts: 21
Joined: Mon Jul 30, 2018 7:24 am

Re: javascript question

Post by boyo1991 » Wed Aug 22, 2018 1:10 pm

im a little confused as to what i did then.

Not because your code doesnt work, but that when i did what you did, it seemed to not work. i mustve been careless and not included a semicolon somewhere and got frustrated before checking..

thanks,

solved

Post Reply

Return to “Scripts & Bash”