[SOLVED]javascript question

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
boyo1991

[SOLVED]javascript question

Post by boyo1991 »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
gm10

Re: javascript question

Post by gm10 »

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.
boyo1991

Re: javascript question

Post by boyo1991 »

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

Re: javascript question

Post by gm10 »

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.
boyo1991

Re: javascript question

Post by boyo1991 »

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
Locked

Return to “Scripts & Bash”