Another swtich and loop version. This time in JavaScript. More intersting solutions to come later.