﻿function solveEquation(equation) { qterms = new Array(0,0,0); // Split equation into two sides f = equation.split('='); if (f.length != 2) { output = 'There needs to be two sides to this equation.'; } else if (f[0].match(/x/) && f[1].match(/^[0-9]+\$/)) { answer = f[1]; parts = f[0].split(/(\+|\-)/); for (i=0; i -1) { if (term[0]) { Debug.trace(parts); if (i > 0 && parts[i-1] == '-') { // Negative term? multiplier = -1; } else { multiplier = 1; } if (term[1]) { qterms[power] += Number(term[1])*multiplier; } else { qterms[power] += 1*multiplier; } } } } a = qterms[2]; b = qterms[1]; c = qterms[0]-answer; if (a == 0) { output = 'The value of x^2 cannot be zero.'; } else { discrim = Math.pow(b, 2)-4*a*c; if (discrim < 0) { output = 'There are no real solutions to this equation.'; } else { x1 = (-b + Math.pow(discrim, 0.5))/(2*a); x2 = (-b - Math.pow(discrim, 0.5))/(2*a); if (x1 == x2) { // Same as discriminant = 0 output = 'x=' + x1; } else { output = 'x=' + x1 + ' or x=' + x2; } output += '\n\nDiscriminant: ' + discrim; output += '\na=' + a + ' b=' + b + ' c=' + c; } } } else { output = 'Could not determine terms.'; } return output; } function OnEvent_Initialize(MessengerStart) { } function OnEvent_Uninitialize(MessengerExit) { } function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, Type) { if (Type == 1) { if (Message.substring(0,6) == '!quad ') { equation = Message.substring(6); ChatWnd.SendMessage('QUADRATIC SOLVER\n'+solveEquation(equation)); } else if (Message == '!quad') { ChatWnd.SendMessage('To use the quadratic solver, you must enter an equation.\n\nFor example:\n!quad x^2+7x+3=9\n!quad x^2=4'); } } }