[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript switch

Posted on 2009-07-12
2
Medium Priority
?
330 Views
Last Modified: 2012-05-07
I have a callback function (onContentLoad) that is parsing some json.  I have a bunch of if/else statements that I'd like to turn into a swtich.
onContentLoad: function (json) {
    //Parse the returned JSON response
    if (json.result == 'success' && json.addremove == 'add') {
 
}
    else if (json.result == 'success' && json.addremove == 'remove') {
 
}
    else if (json.result == 'fail' && json.message == 'action-failed') {
 
}
    else if (json.result == 'fail' && json.message == 'number-exceeded') {
 
}
    else if (json.result == 'fail' && json.message == 'not-logged-in') {
 
}
}

Open in new window

0
Comment
Question by:aznprncritic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 1000 total points
ID: 24835544
You cannot turn a condition in which you are testing two equalities into a single switch. What you can do is:

//Parse the returned JSON response
if (json.result == 'success')
{
    switch (json.addremove)
    {
    case 'add':
        // Code
        break;
    case 'remove':
        // Code
        break;
    }
}
else if (json.result == 'fail')
{
    switch (json.message)
    {
    case 'action-failed':
        // Code
        break;
    case 'number-exceeded':
        // Code
        break;
    case 'not-logged-in':
        // Code
        break;
    }
}

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24835560

if ( json. result == 'success' ) {
  if ( json.addremove == 'add' ) {
//  #1 - json.result == 'success' && json.addremove == 'add' 
  } else {
//  #2 - json.result == 'success' && json.addremove == 'remove' 
  }
} else {
  switch ( json.message ) {
    case 'action-failed' :
//    #3 json.result == 'fail' && json.message == 'action-failed'
      break;
    case  'number-exceeded' :
//    #4 json.result == 'fail' && json.message == 'number-exceeded'
      break;
    case 'not-logged-in' :
//    #5 json.result == 'fail' && json.message ==    'not-logged-in'
      break;
    default :
      alert( 'oops' );
  }
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question