简体   繁体   English

如果在console.log 中调用了相应的函数,如何打印标签。 现在它不打印标签和结果?

[英]How can I print label if the corresponding function is called in the console.log. Now its not printing the label and result?

How can I print label if the corresponding function is called in the console.log.如果在console.log 中调用了相应的函数,如何打印标签。 Now its not printing the label and result ?现在它不打印标签和结果? Can someone please advise有人可以请指教

    let num1 = 100;
    let num2 = 200;
    let text = 'add';
    calculationOfNumbers(text, num1, num2);
    
    function calculationOfNumbers(text, num1, num2) {
    if (text == 'add') {
        let label = "Adding";
        let result = addFunction(num1, num2);
        console.log(`This is : ${label}`+result);
    } else if (text == 'subtract') {
        let label = "Subtracting";
        let result = subtractFunction(num1, num2);
        console.log(`This is : ${label}`+result);
    }

}

function addFunction(num1, num2 ) {
    let result = parseInt(num1) + parseInt(num2);
    return result;
}

function subtractFunction(num1, num2) {
    let result = parseInt(num2)  - parseInt(num1) ;
    return result;
}

those return label, result does nothing.那些return label, result什么都不做。 It is the same as doing return result .和做return result Learn about comma operator .了解逗号运算符 There's no tuple data type in JS yet. JS 中还没有元组数据类型。

What you could do instead is the following:你可以做的是:

function addFunction(num1, num2, label) {
    let result = parseInt(num1) + parseInt(num2);
    return [label, result];
}
function calculationOfNumbers(x, num1, num2) {
    let label, result;
    if (x == 'add') {
       [label, result] = addFunction(num1, num2, "adding");
    } else if (x == 'subtract') {
       [label, result] = subtractFunction(num1, num2, "subtracting");
    }
    console.log(`This is : ${label} ${result}`);
}

ie., return arrays and use destructuring.即,返回数组并使用解构。

Or even:甚至:

function calculationOfNumbers(x, num1, num2) {
    let result;
    if (x == 'add') {
       result = addFunction(num1, num2);
    } else if (x == 'subtract') {
       result = subtractFunction(num1, num2);
    }
    console.log(`This is: ${label} ${result}`);
}

function addFunction(num1, num2) {
    let result = parseInt(num1) + parseInt(num2);
    return label="adding", result;
}

function subtractFunction(num1, num2) {
    let result = parseInt(num2)  - parseInt(num1) ;
    return label="subtracting", result;
}

(I'm not saying this is the best approach tho) (我不是说这是最好的方法)

Since you cannot return multiple values with one return statement in JavaScript, one option you could use is to instead return an object like so:由于您无法在 JavaScript 中使用一个return语句返回多个值,因此您可以使用的一种选择是返回一个object如下所示:

 let total = 0; let num1 = 100; let num2 = 200; let text = 'add'; let label = ""; calculationOfNumbers(text, num1, num2); function calculationOfNumbers(x, num1, num2) { let info; if (x == 'add') { info = addFunction(num1, num2, "adding"); } else if (x == 'subtract') { info = subtractFunction(num1, num2, "subtracting"); } console.log(`This is ${info.label} : ${info.result}`); } function addFunction(num1, num2, label) { let result = parseInt(num1) + parseInt(num2); return { label, result }; } function subtractFunction(num1, num2, label) { let result = parseInt(num2) - parseInt(num1); return { label, result }; }

Notice how I assign each function call to the variable info then you can access the required properties with info.label and info.results , respectively.请注意我如何将每个函数调用分配给变量info然后您可以分别使用info.labelinfo.results访问所需的属性。

there are few things needs to be amended in your code您的代码中需要修改的东西很少


 function calculationOfNumbers(x, num1, num2) {
    if (x == 'add') {
        // function is returning some values, but it was never stored somewhere to be used later
        addFunction(num1, num2, "adding");
    } else if (x == 'subtract') {
      // same situation, function is returning some values, but it was never stored somewhere to be used later
        subtractFunction(num1, num2, "subtracting");
    }
   // you are printing (result) but it was never declared before
    console.log(`This is : ${label}`+result);
}

function addFunction(num1, num2, label) {
  // it's better to use Number(num1) instead of parseInt()
    let result = parseInt(num1) + parseInt(num2);
    // you are returning label inside here, however you didn't amend the global variable result,
// also no need to pass it down to this function, the add function should be returning only the result of the calculation
    return label, result;
}

function subtractFunction(num1, num2, label) {
    let result = parseInt(num2)  - parseInt(num1) ;
    return label, result;
}

So the code after amendment should look like below:所以修改后的代码应该如下所示:

let num1 = 100;
let num2 = 200;
let text = 'add';

calculationOfNumbers(text, num1, num2);

function calculationOfNumbers(x, num1, num2) {
  let label = x;
  let result = 0;

  if (x == 'add') {
    result = addFunction(num1, num2);
  } else if (x == 'subtract') {
    result = subtractFunction(num1, num2);
  }
  console.log(`This is : ${label}` + result);
}

function addFunction(num1, num2) {
  let result = Number(num1) + Number(num2);
  return result;
}

function subtractFunction(num1, num2) {
  let result = Number(num2) - Number(num1);
  return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在console.log()中为每个值打印标签 - How to print out a label for each value in console.log() 未调用函数且未打印console.log - Function not being called and console.log not printing console.log 中没有 output。 初学者问题 - No output in console.log. Beginner Question 尽管在console.log中显示了它的存在性,但仍然无法访问对象数据成员。 尝试的访问返回未定义 - Unable to access objects datamember, despite showing its exsistence in console.log. Attempted access returns undefined JQuery/Javascript - 如何<label>根据其对应的</label>变化<select> - JQuery/Javascript - How to change <label> based on its corresponding <select> 如何让父组件成为其子组件的标签? - How can I make a parent component be the label of its child component? 如何在文本字段中放置标签? - How can I position a label inside its textfield? 如何使用javascript node.js中的console.log打印函数内的变量列表 - how can I print a list of variables within a function using console.log in javascript node.js 如何在此功能中添加控制台日志? - How can I add a console log in this function? 如何将 console.log 绑定到另一个 function 调用,以便我可以在调用它的控制台中看到脚本的行号? - How to bind console.log to another function call so I can see line number of the script in console where it is called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM