Как объединить результат двух функций в Javascript? - PullRequest
0 голосов
/ 29 октября 2019

Первые две функции производят числа, а третья должна присоединиться к ним, но я не уверен, почему joinNums() не работает.

// random number between specified params
function randInteger() {
  let min = 1;
  let max = 10;
  const randomInteger = Math.floor(Math.random() * (max - min + 1)) + min;
  console.log(randomInteger);
}

function randDecimal() {
  let decimal = 100;
  let integral = 100;
  const randomDecimal =
    Math.floor(
      Math.random() * (integral * decimal - 1 * decimal) + 1 * decimal
    ) /
    (1 * decimal);
  console.log(randomDecimal);
}

function joinNums() {
  let randomDecimal;
  let randomInteger;
  randInteger();
  randDecimal();
  console.log("" + randomDecimal + randomInteger);
}

Ответы [ 2 ]

2 голосов
/ 29 октября 2019
  1. Вы не назначаете результаты функции переменным.

  2. Ваши функции должны return результат, а не console.log it.

  3. Вам нужно позвонить joinNums() (вы, похоже, не).

Итак (см. ***):

// random number between specified params
function randInteger() {
  let min = 1;
  let max = 10;
  const randomInteger = Math.floor(Math.random() * (max - min + 1)) + min;
  return randomInteger; // ***
}

function randDecimal() {
  let decimal = 100;
  let integral = 100;
  const randomDecimal =
    Math.floor(
      Math.random() * (integral * decimal - 1 * decimal) + 1 * decimal
    ) /
    (1 * decimal);
  return randomDecimal; // ***
}

function joinNums() {
  let randomDecimal = randDecimal();  // ***
  let randomInteger = randInteger();  // ***
  console.log(`${randomDecimal}${randomInteger}`);
}

joinNums();  // ***

Или, конечно, вы можете просто вызывать их напрямую, а не записывать в переменные сначала:

function joinNums() {
  console.log(`${randDecimal()}${randInteger()}`); // ***
}

// random number between specified params
function randInteger() {
  let min = 1;
  let max = 10;
  const randomInteger = Math.floor(Math.random() * (max - min + 1)) + min;
  return randomInteger; // ***
}

function randDecimal() {
  let decimal = 100;
  let integral = 100;
  const randomDecimal =
    Math.floor(
      Math.random() * (integral * decimal - 1 * decimal) + 1 * decimal
    ) /
    (1 * decimal);
  return randomDecimal; // ***
}

function joinNums() {
  console.log(`${randDecimal()}${randInteger()}`);
}

joinNums();  // ***
0 голосов
/ 29 октября 2019

Это будет работать для вас

function randInteger() {
  let min = 1;
  let max = 10;
  const randomInteger = Math.floor(Math.random() * (max - min + 1)) + min;
return randomInteger ;
  console.log(randomInteger);


}

function randDecimal() {
  let decimal = 100;
  let integral = 100;
  const randomDecimal =(Math.random() * (integral * decimal - 1 * decimal) + 1 * decimal)/(1 * decimal);

return randomDecimal;
  console.log(randomDecimal);
}

function joinNums() {
  let randomDecimal;
  let randomInteger;
  randInteger();
  randDecimal();
  console.log( + randDecimal() + randInteger());
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...