Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

JS - Switch statement

0 votes
7 views
added Jul 9 in Javascript by LC Marshal Lieutenant (14,570 points)
edited Jul 12 by LC Marshal
let a = 2 + 2,
    b = a++, //5
    c = a * b, //4 * 5 = 20
    d = a + b + c;  //4 + 5 + 20

    console.log(d);

switch (c) {
  case 28:
    alert( 'Too small' );
    break;
  case 29:
    alert( 'Exactly!' );
    break;
  case 30:
    alert( 'Too large' );
    break;
  default:
    alert( "I don't know such values" );
}

2 Responses

0 votes
responded Jul 9 by LC Marshal Lieutenant (14,570 points)
edited Jul 12 by LC Marshal
let lcmarshal= prompt("My kids name?")
switch (lcmarshal) {
  case 'adam':
    console.log( 'My eldest son' );
    break;

  case 'fatimah':
    console.log( 'my only daughter' );
    break;

  case 'musa':
    console.log( 'my youngest boy!' );
    break;
  default:
    console.log( 'not my kids for sure' )
}
0 votes
responded Jul 10 by LC Marshal Lieutenant (14,570 points)
edited Jul 12 by LC Marshal
//sharing condition outcome with switch

let emilie = +prompt('give the number', '');

switch(emilie) {
  case 0 :
    alert(0);
    break;

  case 1 :
    alert(1);
    break;
    
  case 2 :
  case 3 :
    alert('2, 3');
    break;
}
commented Jul 10 by LC Marshal Lieutenant (14,570 points)
edited Jul 12 by LC Marshal
//Alternative switch method for 'emilie' case
let emilie = +prompt('give the number', '');

switch(emilie) {
  case 0 :
  case 1 :
    alert(`${emilie}`);
    break;
    
  case 2 :
  case 3 :
    alert('2, 3');
    break;
}
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...