You must Sign In to post a response.
  • Category: JavaScript

    Java Script vs Switch Statement conversation

    Hello Member
    I have assignment of my class and I need to be solution so please read the question and answer with details,
    You need to study the following Javascript code carefully. Write the below code without if-else to do the same job by using Switch Statement?

    1 . If (MousePosition="Div1") {
    2. Alert("Div1");
    3. } else {
    4. If(MousePosition="Div2") {
    5. Alert("Div2");
    6. } else {
    7. If(MousePosition="Div3") {
    8. Alert("Div3");
    9. } else {
    10. If(MousePosition="Div4") {
    11. Alert("Div4");
    12. } else {
    13. If(MousePosition="Div5") {
    14. Alert("Div5");
    15. } } } } }

    Please provide solution of the above code.
  • #769650
    Here is the javascript switch statement without else-if

    switch(MousePosition){
    case 'Div1':
    alert('Div1');
    break;
    case 'Div2':
    alert('Div2');
    break
    case 'Div3':
    alert('Div3');
    break
    case 'Div4':
    alert('Div4');
    break
    case 'Div5':
    alert('Div5');
    break
    default:
    alert('invalid position');
    }

    Regards,
    B. Ramana Reddy

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments