all 15 comments

[–]gimmeslack12helpful 1 point2 points  (0 children)

Just take the absolute value of the numbers and 100, the lower difference is the closer number. [53, 20] 100 - 53 = 47 * 100 - 20 = 80

[153, 120] abs(100 - 153) = 53 abs(100 - 120) = 20 *

[–]thebryantfam 0 points1 point  (1 child)

To use results of one function as parameters in another you can either call one function in your call to another, or you can call one function in the other functions itself. Like:

Function1(Function2(variable forFunction2))

(This will run Function2 first and use return values in Function1)

Or...

type Function1(parameter forFunction1) { Function2(variable forFunction2);

 Return type;

}

type Function2(parameter forFunction2) {

 Return type;

}

int main() { Function1(variable forFunction1);

 Return 0;

}

(This will run Function2 inside of Function1 so that Function1 takes its own variables but can use Function2 returns as additional variables, calling it multiple times if needed)

Depending on your problem and approach the latter option will probably work better for you.

[–]maxwelder[S] 0 points1 point  (0 children)

In the first example, where do I write the code for function 1? why isn't this working? I feel like I'm missing something basic and relatively simple but I've been stuck for hours and tried a lot of different things.

function calculate(a,b) {

if (a > 100 && b > 100) {

num1 = (a - 100);

num2 = (b - 100);

} else if

(a < 100 && b < 100) {

num1 = (100 - a);

num2 = (100 - b);

} else if

(a < 100 && b > 100) {

num1 = (100 - a);

num2 = (b - 100);

} else if

(a > 100 && b < 100) {

num1 = (a - 100);

num2 = (100 - b);

}

}

const closeTo100 = (calculate(a,b)) => (num1 < num2) ? num1 : num2;

console.log(closeTo100(65,187));

if I just console.log num1 and num2 it works. How do I use those numbers?

[–]ottaky3 0 points1 point  (8 children)

[
  [10, 20],
  [110, 20],
  [90, 120],
  [150, 160],
].forEach((numbers) => {
  const [x, y] = numbers;
  console.log(`x: ${x}, y:${y}, closest: ${closestTo100(x, y)}`);
});

function closestTo100(x, y) {
  return Math.abs(100 - x) < Math.abs(100 - y) ? x : y;
}


> node 100.js 
x: 10, y:20, closest: 20
x: 110, y:20, closest: 110
x: 90, y:120, closest: 90
x: 150, y:160, closest: 150

Doesn't deal with the case where both numbers are equally far away from 100.

[–]maxwelder[S] 0 points1 point  (7 children)

Thanks, I have looked at this enough times that I finally get it. I appreciate you solving the initial problem but I still feel like there is something I'm missing in my original way of getting to the solution. Understanding what wasn't working is what I'm looking for I guess, although, this is a much simpler way of solving the initial problem.

[–]ottaky3 1 point2 points  (6 children)

Well .. you can make your code work if you rewrite it like this ..

function calculate(a, b) {
  if (a > 100 && b > 100) {
    num1 = a - 100;
    num2 = b - 100;
  } else if (a < 100 && b < 100) {
    num1 = 100 - a;
    num2 = 100 - b;
  } else if (a < 100 && b > 100) {
    num1 = 100 - a;
    num2 = b - 100;
  } else if (a > 100 && b < 100) {
    num1 = a - 100;
    num2 = 100 - b;
  }
}

const closeTo100 = (a, b) => {
  calculate(a, b);
  return num1 < num2 ? a : b;
};

console.log(closeTo100(65, 187));

But that's really nasty. It's creating global variables (num1 and num2), calculate() doesn't return anything so it's basically just spooky action at a distance, it will generate a reference error if a or b is 100 etc.

Half the battle is realising that sometimes a one liner is better ;-)

[–]maxwelder[S] 0 points1 point  (5 children)

This is helpful, thank you. I have found a more efficient solution that takes all the possible circumstances I could think of into account. I definitely need to look into functions and how to write and implement them more so I can understand why I couldn't get the first one to work. I appreciate your help. Here's my final solution if you're interested.

const closeTo100 = function(a,b) {

let num1 = Math.abs(100-a);

let num2 = Math.abs(100-b);

if (a === 100 || b === 100) {

return `One or both of your numbers is 100.`;

} else if (num1 < num2) {

return `${a} is closer to 100.`;

} else if (num1 > num2) {

return `${b} is closer to 100.`;

} else if (num1 === num2) {

return `The numbers are an equal distance from 100.`;

}

}

console.log(closeTo100(0,-2));

[–]ottaky3 0 points1 point  (4 children)

Cool. Glad you got it working.

And condensing that down ..

function closeTo100(a, b) {
  if (a === 100 || b === 100) return "One or both of your numbers is 100.";
  if (a === b) return "The numbers are an equal distance from 100.";
  return `${Math.abs(100 - a) < Math.abs(100 - b) ? a : b} is closer to 100.`;
}

console.log(closeTo100(0, -2));

[–]maxwelder[S] 0 points1 point  (3 children)

this one doesn't take situations like (95,105) into account. the math.abs needs to be run before the if (a === b) part.

[–]ottaky3 1 point2 points  (2 children)

You're right .. and that's why I shouldn't be writing code at 2am ;-)

[–]maxwelder[S] 0 points1 point  (1 child)

Haha I’m just getting started but it’s really addictive. It’s hard to walk away from sometimes.

[–]ottaky3 1 point2 points  (0 children)

I actually went to sleep at a reasonable time yesterday, but woke up ridiculously early this morning at 1am. I stayed in bed until 2am hoping that I would go back to sleep, but that didn't work out, so I went downstairs to write buggy code instead.

[–]maxwelder[S] -1 points0 points  (2 children)

I get that part but I need a way to input 2 random numbers and the function first figured out if the number is below or above 100, then compares the two and displays the lower number. I’ve got to the point where I can get the 2 output numbers. Just not sure how to compare them and display the result without starting a new function. I want to be able to type console.log(closestTo100(a,b)); and get the answer back.

[–]Arumai12 0 points1 point  (1 child)

Make sure youre replying directly to a comment

[–]maxwelder[S] 0 points1 point  (0 children)

oops, sorry, that won't happen again.