source-academy / sicp

XML sources of SICP and SICP JS, and support for generating Interactive SICP JS, PDF, e-book and comparison editions

Home Page:https://sourceacademy.org/sicpjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solution to Exercise 3.28

clean99 opened this issue · comments

function logical_or(s1, s2) {
    return s1 === 1 || s2 === 1
  	   ? 1
  	   : s1 === 0 || s1 === 1
  	   ? s2 === 0 || s2 === 1
  	       ? 0
  	       : error(s2, "invalid signal")
           : error(s1, "invalid signal");
}
function or_gate(a1, a2, output) {
    function or_action_function() {
         const new_value = logical_or(get_signal(a1),
                                      get_signal(a2));
         after_delay(or_gate_delay,
                     () => set_signal(output, new_value));
    };
    add_action(a1, or_action_function);
    add_action(a2, or_action_function);
    return "ok";
}