MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 72 lines 1.5 kB view raw
1import { test, summary } from './helpers.js'; 2 3console.log('Switch Tests\n'); 4 5let x = 2; 6let result = 0; 7switch (x) { 8 case 1: result = 10; break; 9 case 2: result = 20; break; 10 case 3: result = 30; break; 11 default: result = 99; 12} 13test('switch case 2', result, 20); 14 15let y = 1; 16let sum = 0; 17switch (y) { 18 case 1: sum += 1; 19 case 2: sum += 2; 20 case 3: sum += 3; break; 21 default: sum += 100; 22} 23test('switch fall-through', sum, 6); 24 25let fruit = 'apple'; 26let color = ''; 27switch (fruit) { 28 case 'apple': color = 'red'; break; 29 case 'banana': color = 'yellow'; break; 30 case 'grape': color = 'purple'; break; 31 default: color = 'unknown'; 32} 33test('switch string', color, 'red'); 34 35let z = 5; 36let msg = ''; 37switch (z) { 38 case 1: msg = 'one'; break; 39 case 2: msg = 'two'; break; 40 default: msg = 'other'; 41} 42test('switch default', msg, 'other'); 43 44let a = 0; 45let aResult = ''; 46switch (a) { 47 case 0: aResult = 'zero'; break; 48 case 1: aResult = 'one'; break; 49} 50test('switch case 0', aResult, 'zero'); 51 52let b = true; 53let bResult = ''; 54switch (b) { 55 case true: bResult = 'true'; break; 56 case false: bResult = 'false'; break; 57} 58test('switch boolean', bResult, 'true'); 59 60function getGrade(score) { 61 switch (true) { 62 case score >= 90: return 'A'; 63 case score >= 80: return 'B'; 64 case score >= 70: return 'C'; 65 default: return 'F'; 66 } 67} 68test('switch expression A', getGrade(95), 'A'); 69test('switch expression B', getGrade(85), 'B'); 70test('switch expression F', getGrade(50), 'F'); 71 72summary();