Resolviendo el reto /Solving the challenge

avatar
(Edited)

version español

"Programming is like cooking: you can do it well or you can do it really well." - Larry Wall

This is a series of posts that will focus on solving the daily problems posed by the user @ydavgonzalez step by step and we will also teach the programming approach.

challenge link

Problem:

There are 8 red balls, 6 blue balls, and 4 green balls in a bag. If you pick one ball at random, what is the probability that it is red or blue?

Steps to solve the problem:

  1. Necessary mathematical knowledge:
  • Probability: It is the ratio of favorable outcomes to the total number of possible outcomes.
  1. Necessary programming knowledge:
  • Variables: To store the values of red, blue, and green balls.
  • Logical operators: To combine conditions, such as "or".
  • Subtractions: To calculate the total number of balls and the number of red and blue balls.
  • Division: To calculate the probability.
  1. JavaScript code:// Number of red, blue, and green balls
    const redBalls = 8;
    const blueBalls = 6;
    const greenBalls = 4;

// Calculate the total number of balls
const totalBalls = redBalls + blueBalls + greenBalls;

// Calculate the number of red or blue balls
const redBlueBalls = redBalls + blueBalls;

// Calculate the probability of picking a red or blue ball
const probabilityRedBlue = redBlueBalls / totalBalls;

console.log("Probability of picking a red or blue ball:", probabilityRedBlue);
Result:Probability of picking a red or blue ball: 0.7



0
0
0.000
1 comments