Quick actions

cmd+k|ctrl+k

Navigation

Languages

Kvadratic - JS

Snippet info

Language

JavaScript

Visibility

public

Author

jsonkody

Created

2024-04-02T19:45:32.612492Z

Updated

2024-04-03T09:52:41.763062Z

function kvadratic(a,b,c) {
    const disc = discrim(a,b,c)
    
    if(disc < 0) {
        return "Diskriminant je zaporny, koren neexistuje pro realna cisla."
    }
    
    if(disc === 0) {
        return one_root(a,b)
    }
    
    return two_roots(a,b, disc)
}

function discrim(a,b,c) {
    return b * b - 4 * a * c
}

function one_root(a,b) {
    return [-b / (2 * a)]
}

function two_roots(a,b,disc) {
    const root1 = (-b + Math.sqrt(disc)) / (2 * a)
    const root2 = (-b - Math.sqrt(disc)) / (2 * a)
    
    return [root1, root2]
}

console.log(kvadratic(2,6,-20))
INFO