presentation of fractions in javascript

Asked

Viewed 638 times

0

needed help on this issue, I managed to solve it perfectly but in the exits I think it is a little better!

7) Make an algorithm that calculates and displays the sum of the first 30 terms of the grade:

480 - 475 + 470 - 465 ...
10 11 12 13 (these are fractions, respectively with numerator and denominator, but I couldn’t even represent them here)

in my resolution the exits were like this:

The numerator of fraction 01 is: 475 The denominator of fraction 01 is: 11 With signal: Negative

but wanted more objectively presenting the fraction itself with the sign, but found no way, my code is as follows,

var numerador = 480
    var denominador = 10
    var sinal = "Positivo"
    for(i=0;i<=29;i++){
        numerador-=5
        denominador++
        if(sinal=="Negativo"){
            sinal = "Positivo"
        }else{
            sinal = "Negativo"
        }
        console.log("O numerador da fração " + i+1+" é: "+ numerador+"\nO denominador da fração " + i + 1 + " é: " + denominador+"\nCom sinal: " + sinal )


    }

  • What is the format of these numbers 480 - 475 + 470 - 465? is a string? array?

  • are normal whole numbers

  • But you have the numbers like this: var numeros = '480 - 475 + 470 - 465'; or so: var numeros = [480, -475, 470, -465];?

2 answers

1

What you can do is implement a class in Javascript that represents a fraction. In the class you can implement methods to sum two fractions and to format the fraction when displayed.

For example:

class Fraction {
  constructor(numerator, denominator) {
    this.numerator = numerator
    this.denominator = denominator
  }

  get value() {
    return this.numerator / this.denominator
  }

  add(other) {
    const numerator = this.numerator * other.denominator + this.denominator * other.numerator
    const denominator = this.denominator * other.denominator

    // Implemente uma função que calcule o MDC e use-a aqui...
    const MDC = mdc(numerator, denominator)

    return new Fraction(numerator/MDC, denominator/MDC)
  }

  toString() {
    return `${this.numerator}/${this.denominator}`
  }
}

So, to create a function, you do:

const a = new Fraction(480, 10)  // 480/10
const b = new Fraction(-475, 11)  // -475/11

To add the two, just use the method add.

const result = a.add(b)

Note: as addition is a mathematical operation that has the commutativity property, it is also possible to do b.add(a) and get exactly the same result.

In doing console.log(`Resultado ${result} (= ${result.value})`) the exit would be Resultado 53/11 (= 4.818181818181818) indicating that the result of the addition was 4.8181... which can be represented by the irreducible fraction 53/11.

If you don’t need the result to be an irreducible fraction you can skip the step of calculating the MDC between the values.

1

If I understood correctly it would be something like this:

for (let numerador = 480, denominador = 10; denominador < 41; numerador -= 5, denominador++) {
    console.log((denominador % 2 == 0 ? "+" : "-") + numerador);
    console.log("----");
    console.log(" " + denominador);
    console.log("\n");
}

I put in the Github for future reference.

I don’t know if I should have that sign or if it needs to be something different because the question doesn’t have the statement clearly.

I simplified using the for the way he was conceived and not the "cake recipe" that everybody uses. I preferred to use two variables to not look too different but the ideal would be to have one and the other to be calculated in step, for example the denominator is only the beginning of it minus 5 times the result of the numerator minus 10.

I simplified the use of the signal by calculating why the positive occurs in pairs. And then a conditional operator is enough to decide which sign should be printed, including I put the sign instead of spelling out what the signal is, because it doesn’t seem to make sense.

If you want to draw a fraction the ideal is a more sophisticated mechanism for this, but we can simulate something close on the console simply by looking at how we do on paper and play here, then put the numerator (with the signal, you may prefer nothing if you are positive), on the bottom line the bar that divides the fraction, below the denominator. You can format even more, I think now you have a parameter of how to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.