What does math problems have to do with anything!

Tell us what’s happening:
I fail at math so this stupid thing makes no sense to me, i am pretty sure my difficulty lies in my mathematical ineptitude.

where the instructions tell you to work out what the formula does i see :

the number will be rounded down, random and multiplied by a min subtract a max and add 1. then it is added to the min for some reason.

am i missing something in my analysis?

Your code so far


// Example
function ourRandomRange(ourMin, ourMax) {

  return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
}

ourRandomRange(1, 9);

// Only change code below this line.

function randomRange(myMin, myMax) {

  return 
nothing is ever good enough for you, you stupid program!!!!!!!!; // Change this line

}

// Change these values to test your function
var myRandom = randomRange(5, 15);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range

Math.random() generates a random number between 0 and 1. It is possible to generate 0, but not 1. That’s just how Math.random() works.

for example: Math.random() could generate 0.3

If you want generate a random number over a larger range, then you can do that by multiplying the result of Math.random() by the size of the range. Remember that Math.random() results are inclusive of 0, but not 1, so if you want an inclusive range, you need to add 1 to the range.

for example 0.3 * 9 is 2.7

If you don’t just want to generate a random number between 0 and some positive number, then you need to “shift” the result by the minimum expected value.

for example 2.7 + 1 is 3.7

Because Math.random() generates a number between 0 and 1, you will almost always get a decimal number as your random result. If you only want to generate random whole numbers, then you use Math.floor() to round down.

for example Math.floor(3.7) is 3

so i should be able to put
Math.floor(Math.random() * myMax)
but how do i include the myMin

math has everything to do with programming, like it , or lump it…

maybe if the challenges had more purpose behind them or explanation as to why you’d need to do what they are asking. and not just ‘oh lets say you want to organize your records so make this function’. how does this ‘record organization’ apply to an app or website, what context will i be using such functions in the real world. ya know.

I wouldn’t go that far. You can write plenty of front-end code and only need very little math. How much math is involved depends on the code domain as well.

1 Like

the same system could hypothetically be applied to books, parts, inventory of any kind…

i see that yea, but i am greener than the Irish countryside and coming in to JavaScript -not knowing how it interacts with a website makes it all very abstract and vague like are these codes you write so a click of the button makes the inventory sort? how do you attach such code to the button? and then add in the complication of learning the code language and trying to have it make logical sense somehow…

i am still stuck on the challenge.

you give me such hope :bowing_woman:

have you learned html and css? if not it might be worth your while to go and study those first inorder to gain a basic understanding first and after come back to javascript…

they are the first ones i did. got my certificate and everything.
javascript is a beast that has nothing similar to html or css.

Simply put JavaScript is a scripting language that runs in a browsers ‘JavaScript engine’ which more ore less is the same across browsers though each browser is responsible for shipping the added features to javascript (ie: Internet Explorer is notoriously out of date).

JavaScript has it’s data types (strings, numbers, objects), operators (+, -, /), control structures (loops, conditionals, etc) and more. The Browser takes your HTML and CSS files and creates a single object with nested objects and properties. This super object is the DOM (Document Object Model) and is how JavaScript interacts with the page. The page is rendered and re-rendered live as this DOM changes. JS can interact with nodes and properties via the window and document objects (note: document is the same as window.document, window is the root object).

1 Like

JavaScript interacts with the DOM and the user interacts with the DOM. When a button element is clicked, you can listen to that <button> html element for an event trigger (such as a click event) and call a function to do something.

Another example, while CSS considers the hover state of an element (ie: element:hover {}), there is the ‘mouseenter’, ‘mouseleave’, and ‘mouseover’ event triggers that you can listen for with JavaScript.

1 Like

You need to be able to do basic things: producing a random value is a basic thing and a relatively common need. In programming, you divide problems down into simple parts, then build up solutions to your problem using those simple parts. This is giving you a building block from which to build solutions, as with most other challenges, and yes, it may seem abstract. But in many ways it has to be done this way, because if you take a full problem, it will be too complex at this point – instead of a single concept, you would be faced with multiple concepts that you need to apply at once, and you’d get stuck.

And more generally, it is, as with most of the challenges, repeatedly making you write out functions and variables and exercise JS’ built in functions.

Well what might you need to generate a random whole number for? JS is a general-purpose programming language, there are many different contexts where you may want to do this. You have a collection of numbered things and you want to pick one of them at random, there are no constraints on what those things are

Note that a new curriculum is being worked on that is entirely project-based, which in future should be something closer to what you’re looking for. It in no way obviates what I’ve said above though, you need to learn the basics.

1 Like

:star_struck: thank you!!! so much!!:man_superhero: its getting clearer now

For what it’s worth, programming is the first time I’ve used complex math in a ‘real-world’ application. I think if I had been taught ‘math as applied in programming’, I would have enjoyed it much more when I was young, because, like you, I didn’t enjoy it much because I didn’t see the point – it was too abstract.
A great example of how the above would be used is in a real application is gaming: if you are building a game, a random value between 1-6 would allow you to build a dice game. A random value between x and y multiplied by your attack score would be used in a game that involves fighting, a random value could be used to return a random comic/blog post/photo from a collection, etc.

There are lots of real world applications for math in programming! It’s really hard at first, and you’ll probably have to re-learn a lot of algebra, but it’s worth it in the end, so keep at it! You’ll be amazed in six months how far you’ve come.

1 Like

i had to take the ‘dumb’ math class in high school lol so i am super worried that because of that i am automatically at a disadvantage because i don’t know how to manipulate numbers in complex mathematical equations. i never learned my multiplication tables and i have a hard time with fractions, like when you tell me something is 4 and 5/8 inches long i’m like :no_mouth::thinking: whaaaat??

That’s maybe an issue. This particular problem is super basic maths and there is quite a bit of that. And algebra (very basic high school level) helps a lot because it’s the same thing. Nothing really more complex than that for web

It’s amazing how much math there is in just the simplest of things. Generating a random number in itself is a complex computer science topic that has been abstracted by standard libraries.

Math.random() does with a certain concept called ranges of values, a mathematical concept that uses these characters to represent them: + - ] [ ) ( ∞

Math.random() in JS yields a range of [0, 1) which means, a range of 0 (inclusive, meaning you may get 0) to 1 (exclusive, you will NEVER get 0 but you may get an infinitely approximate decimal like 0.9999).

Randomness is used in programming to shuffle stuff around, deal with probability and statistics, randomized games, and more.

There are other math applications in programming, some are:

  • Algebra: Almost everything that deals with variables and formulas, numeric methods and physics.
  • Linear Algebra: AI, ML, DL, computer statistics, video-game engine design, 2D tile-based games, Markov chain-based predictions, and more.
  • Vectorial Calculus: VG engine design and multidimensional stuff. Computer graphics, OpenGL, and more.
  • Calculus: There may be cases where you need to know some calculus, especially in numeric methods implementations and physics.
  • Arithmetic: Self-explanatory, this is everywhere, you can’t escape it.
  • Modular arithmetic: This one is a very interesting topic, it prevents soooo many unnecessary loops and deals with ranges of values that cycle such as the clock (1-12 and back), the alphabet (A-Z a-z) and more.
  • Sum and Product: Why do loops when you can use the magic formulas and theorems for summing stuff or multiplying ranges (from i to j given certain steps). Often denoted by the Greek symbols Σ (Sigma) and ∏ (uppercase Pi).
  • Statistics and Probability: Useful for machine learning, deep learning and AI, and ofc, statistical software. Especially Bayesian statistics.
  • Set theory: This deals with collections of objects. Some of the challenges here at FCC deal with this topic (diff and intersections), and it’s the basis of some SQL operations like JOIN.
  • Graph theory: Graphs are useful for mapping relations (quantified or not) between entities, either directed or not. You may see an application of this stuff in GraphQL, Facebook’s friend finding capabilities, shortest paths and route optimization algorithms, path traversal and exploration, rock paper scissors (and beyond), tree-like comment-systems, and more.
  • Lambda calculus and Category theory: Ever watched a hardcore functional programming conference on YouTube? It’s 99% probable you’ll hear these terms mentioned at least once. Category theory’s monads, monoids, functors and applicatives are everywhere in FP books and talks, they’re very useful concepts for data-processing, IO and functional composition.
  • Physics: You’re either an actual physicist or a game engine (computer graphics too) developer. But I’m sure there are more applications of physics in programming. I’m not sure if “chaotic systems” are a part of chaos theory in math/physics but one of my cousins uses this concept for his thesis in Computer Science.
  • Recursiveness and Tree-like structures: This stuff is everywhere, anything that has ramifications or self-iteration. Many computer science problems such as sorting, finding the most optimal “something”, fractals, tree operations, exploration, game AI. Divide and conquer for many problems can be solved recursively.
  • Assorted discrete math topics: Such as converting between numeric systems (hex, binary, octal, decimal) and performing arithmetic operations on them, Boolean operations and logic gates, Karnaugh maps, (graph theory and set theory are here too but I mentioned them separately), etc.
  • Geometry and Trigonometry: You can’t possibly aim to become a game developer or computer graphics engineer without at least knowing how to compute the hypotenuse of a triangle. Sin? Cosine? You should learn them or at least superficially.

And those are the math topics that I know at the top of my head that are very useful in programming. Now, for the web, I’d advise to review your arithmetics and probably a bit of modular arithmetic.

1 Like

It sounds like a math refresher course might be a good thing to start with, and then come back to coding.
I know there are free resources out there to learn such as https://www.khanacademy.org/math

You could also go back and forth – learn coding, and when you hit a math problem you can’t solve, go back to the math course and return to the coding when you’ve got that down.

Good luck!