# [RESOLVED] Associative array problem, what if item is missing?

• 02-01-2013, 12:26 PM
wbport
[RESOLVED] Associative array problem, what if item is missing?
I have the following function in various related pages:
Code:

```function getNoteLength(notelet) {  // Returns duration of note , whole = 768 var NoteLengths = { "16th":48, "32nd":24, "4th":192, "64th":12, "8th":96, "Half":384, "Whole":768, "undefined":-1 } parts = notelet.split(","); var  noteLength= -1; noteLength = NoteLengths[parts[0]];   for (var i = 1; i < parts.length; i++) {     if (parts[i].substr(0,9) == "DblDotted") {           noteLength = noteLength * 7 / 4; }         else       if (parts[i].substr(0,6) == "Dotted") {             noteLength = noteLength * 3 / 2; }           else             if (parts[i].substr(0,7) == "Triplet") {               noteLength = noteLength * 2 / 3; } }   return noteLength; }```
This is my first attempt at using associative arrays (I had been using a traditional search), but was wondering how to I test for a not found condition such as looking for "128th" or "4nd". I would like to return -1, but noteLength comes back as "undefined". The function receives a string such as "4th,Dotted,Slur" for example and would return 288.

TIA
• 02-01-2013, 07:40 PM
Logic Ali
Code:

`var noteLength = ( NoteLengths[parts[0]] === undefined ) ? -1 : NoteLengths[parts[0]];`
• 02-01-2013, 08:19 PM
wbport
That's a lot more concise than what I ended up using
Code:

```if (typeof(noteLength) == "undefined")   return -1;```
Thanks for the input.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.17189 seconds
• Memory Usage 2,347KB
• Queries Executed 11 (?)
Template Usage (20):
• (3)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates