Skip to content

intro-javascript/chapitre_5/js/calculatrice.js #8

@eliottealderson

Description

@eliottealderson

Pour calculatrice les deux propositions ci-dessous peuvent-elles être envisagées ?

function calculer(a, string, b) {

    if (string == "+") {
        return a + b;
    } else if (string == "-") {
        return a - b;
    } else if (string == "*") {
        return a * b;
    } else if (string == "/") {
        return a / b;
    }

}

console.log(calculer(4, "+", 6)); // Doit afficher 10
console.log(calculer(4, "-", 6)); // Doit afficher -2
console.log(calculer(2, "*", 0)); // Doit afficher 0
console.log(calculer(12, "/", 0)); // Doit afficher Infinity
//2ème possibilité sous toute réserve 
function calculer(a, string, b) {
  return eval(a + string + b);
}

console.log(calculer(4, "+", 6)); // Doit afficher 10
console.log(calculer(4, "-", 6)); // Doit afficher -2
console.log(calculer(2, "*", 0)); // Doit afficher 0
console.log(calculer(12, "/", 0)); // Doit afficher Infinity

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions