Avatar billede karl_fritjof Nybegynder
03. november 1999 - 23:39 Der er 9 kommentarer og
1 løsning

Dumt spørgsmål

Jeg sidder og prøver mig lidt frem med JavaScript. Jeg har (prøvet på) at lave en "maskine", der kan lægge to tal sammen. Der er dog det problem at hvis jeg nu vil lægge 3 og 4 sammen bliver resultatet 34. Min kode ser sådan ud:
<html>
<head>
<Script Language="JavaScript">
function samm()  {
    var x=document.Admas.et.value
    var y=document.Admas.to.value
    var z=x+y
    document.Admas.ialt.value=z
}
</Script>
</head>
<body>
<center>
<form Name="Admas">
<input Type=Text Name=et Size=30 Value=0 onChange="samm()"><br>
+<br>
<input Type=Text Name=to Size=30 Value=0 onChange="samm()"><br>
=<br>
<input Type=Text Name=ialt Size=30 Value=0><br>
</form>
</body>
</html>
Avatar billede justincase Nybegynder
03. november 1999 - 23:54 #1
prøv med :
z = eval(x+y);

den betragter x og y som strenge
Avatar billede karl_fritjof Nybegynder
03. november 1999 - 23:59 #2
Det virker ikke. :-(
Avatar billede erikjacobsen Ekspert
04. november 1999 - 00:02 #3
Trick:

  z = (x-0)+(y-0);

Avatar billede justincase Nybegynder
04. november 1999 - 00:08 #4
Tjah, eller :
z = eval(x) + eval(y);


erik (er det OK), snildt trick. Det kan jeg li'.
Avatar billede stone Forsker
04. november 1999 - 23:03 #5
hvad med en lommeregner:
<html>

<head>

<title>Lommeregner</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<base target="indhold">
</head>

<body id="Me">

<h3>Java lommeregner</h3>
<script LANGUAGE="JavaScript" FOR="window" EVENT="onload">
    initCalculator();
</script>
<script LANGUAGE="JavaScript">
public_description = new createCalculator();
var inScriptlet = (typeof(window.external.version) == "string")
var m_memValue         = 0;
var m_stackValue         = 0;
var m_operator         = "";
var m_operatorSetLast     = false;
var m_justCleared     = true;
var m_buttonColor     = "silver";
var m_hiliteColor     = "red";
var m_buttonFont         = "Tahoma";
var m_backColor         = "#000000";
var m_textColor         = "#000099";
var m_current         = null;
var m_numHistoryElems    = 5;
var m_history        = new Array( 10 );
var m_timeoutID = null;
function createCalculator()
{
    this.get_Value = getCurrentValue;
    this.put_Value = setCurrentValue;
    this.get_BackColor = getBackColor;
    this.put_BackColor = setBackColor;
    this.get_ButtonColor = getButtonColor;
    this.put_ButtonColor = setButtonColor;
    this.get_HiliteColor = getHiliteColor;
    this.put_HiliteColor = setHiliteColor;
    this.get_TextColor = getTextColor;
    this.put_TextColor = setTextColor;
    this.get_ButtonFont = getButtonFont;
    this.put_ButtonFont = setButtonFont;
    this.evaluate = evaluate;
    this.event_OnChange = "";
    this.event_OnEvaluate = "";
}
function initCalculator()
{
    setButtonColor( m_buttonColor );
    setBackColor( m_backColor );
    setButtonFont( m_buttonFont );
    initHistory();
}

///////////////////////////////////////////////////////////////////////////
// OPERATION FUNCTIONS
///////////////////////////////////////////////////////////////////////////

function percent()
{
    //HACK:  Don't call setCurrentValue, or the Change event will fire twice
    currentValue.value = eval( getCurrentValue() ) / 100;
    evaluate();
}

function changeSign()
{
    setCurrentValue( -1 * eval( getCurrentValue() ) );
}

function invert()
{
    setCurrentValue( 1 / eval( getCurrentValue() ) );
}

function squareroot()
{
    setCurrentValue( Math.sqrt( getCurrentValue() ) );
}

function setOperator(newOp)
{
    if( m_operator != 0 )
        evaluate();

    m_operator = newOp;
    m_stackValue = eval( currentValue.value );
    m_operatorSetLast = true;
}

function evaluate()
{
    tempValue = eval( currentValue.value );

    if( m_operator == 1 )
        result = m_stackValue + tempValue;
    else
    if( m_operator == 2 )
        result = m_stackValue - tempValue;
    else
    if( m_operator == 3 )
        result = m_stackValue * tempValue;
    else
    if( m_operator == 4 )
    {
        if( tempValue != 0 )
            result = m_stackValue / tempValue;
        else
            result = "NaN";
    }
    else
        return;
   
    m_operator = 0;
    m_justCleared = true;

    setCurrentValue( result );

    if( inScriptlet )
        window.external.raiseEvent( "event_OnEvaluate", window.document );

    addToHistory( result );
}

///////////////////////////////////////////////////////////////////////////
// MEMORY FUNCTIONS
///////////////////////////////////////////////////////////////////////////

function setMemIndicator( memValue )
{
    if( memValue == 0 )
        MEMINDICATOR.innerText = " "
    else
        MEMINDICATOR.innerText = "M"
}

function memSet(number)
{
    m_memValue = number;
    setMemIndicator( m_memValue );
}

function memRecall()
{
    setCurrentValue( m_memValue );
}

function memPlus(number)
{
    memSet( eval( m_memValue ) + eval( number ) );
}

function clearValue(everything)
{
    setCurrentValue(0);
    m_stackValue = 0;
    setOperator(0);
    if( everything )
    {
        memSet(0);
        initHistory();
    }
    m_justCleared = 0;
}

///////////////////////////////////////////////////////////////////////////
// PROPERTY FUNCTIONS AND UTILITIES
///////////////////////////////////////////////////////////////////////////

function redoButtons()
{
    var col;
    var ct;

    col = document.all.tags("TD");
    for( ct = 0; ct < col.length; ++ct )
        if( isCalculatorButton( col[ct] ) )
        {
            col[ct].style.backgroundColor = m_buttonColor;
            col[ct].style.color = m_textColor;
        }
}

function getCurrentValue()
{
    return currentValue.value;
}

function setCurrentValue( newValue )
{
    currentValue.value = newValue;
    if( inScriptlet )
        window.external.raiseEvent( "event_OnChange", window.document );
}

function getBackColor()
{
    return m_backColor;
}

function setBackColor(newColor)
{
    m_backColor = newColor;
    Me.style.backgroundColor = m_backColor;
}

function getButtonColor()
{
    return m_buttonColor;
}

function setButtonColor(newColor)
{
    m_buttonColor = newColor;
    redoButtons();
}

function getHiliteColor()
{
    return m_hiliteColor;
}

function setHiliteColor(newColor)
{
    m_hiliteColor = newColor;
}

function getTextColor()
{
    return m_textColor;
}

function setTextColor(newColor)
{
    m_textColor = newColor;
    redoButtons();
}

function getButtonFont()
{
    return m_buttonFont;
}

function setButtonFont(newFont)
{
    m_buttonFont = newFont;
    Me.style.fontFamily = m_buttonFont;
}

function postNumber(number)
{
    if( !m_operatorSetLast && !m_justCleared )
        currentValue.value = currentValue.value + number;
    else
        currentValue.value = number;

    m_operatorSetLast = false;
    m_justCleared = false;

    if( inScriptlet )
        window.external.raiseEvent( "event_OnChange", window.document );
}

function postDot()
{
    if( isNaN( currentValue.value + "." ) )
        return;

    postNumber(".");
}

///////////////////////////////////////////////////////////////////////////
// HISTORY FUNCTIONS
///////////////////////////////////////////////////////////////////////////

function initHistory()
{
    var ct;

    for( ct = m_history.length - 1; ct >= 0; --ct )
    {
        if( ct % 2 == 0 )
            m_history[ ct ] = "";
        else
            m_history[ ct ] = "historyPick" + ( (ct+1) / 2 );
    }

    if( inScriptlet )
        window.external.setContextMenu( m_history );
}

function historyPick1()
{
    setCurrentValue( m_history[ 2 * 0 ] );
}

function historyPick2()
{
    setCurrentValue( m_history[ 2 * 1 ] );
}

function historyPick3()
{
    setCurrentValue( m_history[ 2 * 2 ] );
}

function historyPick4()
{
    setCurrentValue( m_history[ 2 * 3 ] );
}

function historyPick5()
{
    setCurrentValue( m_history[ 2 * 4 ] );
}

function addToHistory( result )
{
    var ct;

    // Bubble down the previous history elements
    for( ct = m_history.length - 1; ct >= 2; --ct )
    {
        if( ct % 2 == 0 )
            m_history[ ct ] = m_history[ ct-2 ];
        else
            m_history[ ct ] = "historyPick" + ( (ct+1) / 2 );
    }
   
    // Add the new one
    m_history[ 0 ] = result;
    m_history[ 1 ] = "historyPick1";

    if( inScriptlet )
        // Looks like we need to re-set the context menu
        window.external.setContextMenu( m_history );
}

///////////////////////////////////////////////////////////////////////////
// UTILITY FUNCTIONS
///////////////////////////////////////////////////////////////////////////

function isCalculatorButton( item )
{
    return ( item.id.substring(0, 3) == "Btn" );
}

function isOperator( val )
{
    if( val == "/" || val == "*" || val == "+" || val == "-" )
        return true;
    return false;
}

function handleOperator( op )
{
    if( op == "*" )
        setOperator( 3 );
    else
    if( op == "/" )
        setOperator( 4 );
    else
    if( op == "+" )
        setOperator( 1 );
    else
    if( op == "-" )
        setOperator( 2 );
}

///////////////////////////////////////////////////////////////////////////
// EVENT HANDLERS
///////////////////////////////////////////////////////////////////////////

function doInput( inText )
{
    if( !isNaN( inText ) )
        postNumber( inText );
    else
    if( inText == "CE" )
        clearValue( true );
    else
    if( inText == "C" )
        clearValue( false );
    else
    if( inText == "MC" )
        memSet( 0 );
    else
    if( inText == "sqrt" )
        squareroot();
    else
    if( inText == "MR" )
        memRecall();
    else
    if( inText == "MS" )
        memSet( getCurrentValue() );
    else
    if( inText == "1/x" )
        invert();
    else
    if( inText == "." )
        postDot();
    else
    if( inText == "=" )
        evaluate();
    else
    if( inText == "M+" )
        memPlus( getCurrentValue() );
    else
    if( inText == "%" )
        percent();
    else
    if( inText == "+/-" )
        changeSign();
    else
    if( isOperator( inText ) )
        handleOperator( inText );
}

function handleClick()
{
    var current = window.event.srcElement;

    // Return if not on an element
    if( current == null )
        return;

    // Return if not a "button"
    if( !isCalculatorButton( current ) )
        return;

    // Draw "depressed"
    current.style.backgroundColor = m_hiliteColor;
    current.style.color = m_textColor;

    // Do the right thing
    doInput( current.innerText );

    // Clean up
    m_current = current;

    // change the button's color back to not-depressed in 75 milliseconds
    m_timeoutID = setTimeout( "resetButtonColor( m_current, m_textColor, m_buttonColor );", 75 );
   
}

function resetButtonColor( item, newColor, newBackColor )
{
    item.style.color = newColor;
    item.style.backgroundColor = newBackColor;
    clearTimeout( m_timeoutID );
}

function handleMouseOver()
{
    var hotButton = window.event.srcElement;

        if( isCalculatorButton( hotButton ) )
    {
        hotButton.style.color = m_hiliteColor;
    }
}

function handleMouseOut()
{
    var hotButton = window.event.srcElement;

        if( isCalculatorButton( hotButton ) )
    {
        hotButton.style.color = m_textColor;
    }
}

function filterKeys()
{
    var keycode = window.event.keyCode;

    // Force the focus to be inside the textbox for keystrokes to work.  This solves a couple of problems.
    currentValue.focus();

    if( keycode == 46 )
    {
        // Only allow a "." if there isn't already one
        if( getCurrentValue().indexOf( "." ) != -1 )
            window.event.returnValue = false;
    }
    else
    if( keycode == 43 )
        doInput( "+" );
    else
    if( keycode == 45 )
        doInput( "-" );
    else
    if( keycode == 42 )
        doInput( "*" );
    else
    if( keycode == 47 )
        doInput( "/" );
    else
    if( keycode == 61 )
        doInput( "=" );

    if( ( keycode >= 48 && keycode <= 57 ) || keycode == 46 )
    {
        if( m_operatorSetLast || m_justCleared )
            currentValue.value = "";

        m_operatorSetLast = false;
        m_justCleared = false;
    }
    else
        window.event.returnValue = false;

    // Slight inconsistency here -- we're firing the event BEFORE the value has actually changed.
    // But it's changing immediately thereafter.  Hmmm...
    if( inScriptlet && window.event.returnValue )
        window.external.raiseEvent( "event_OnChange", window.document );
}

</script>


<table CELLSPACING="1" CELLPADDING="1" BORDER="1" WIDTH="100" onclick="handleClick()" onmouseover="handleMouseOver()" onmouseout="handleMouseOut()" onkeypress="filterKeys()">
  <tr>
    <td ID="TxtValue" ALIGN="CENTER" VALIGN="CENTER" COLSPAN="6" HEIGHT="13%"><input NAME="currentValue" VALUE="0" size="20"></td>
  </tr>
  <tr>
    <td WIDTH="59%" ID="LblM" ALIGN="CENTER" COLSPAN="4" HEIGHT="19%"><b><span ID="MEMINDICATOR"></span></b></td>
    <td WIDTH="20%" ID="BtnCE" ALIGN="CENTER" HEIGHT="19%">CE</td>
    <td WIDTH="20%" ID="BtnC" ALIGN="CENTER" HEIGHT="19%">C</td>
  </tr>
  <tr>
    <td WIDTH="20%" ID="BtnMC" ALIGN="CENTER" HEIGHT="17%">MC</td>
    <td WIDTH="16%" ID="Btn7" ALIGN="CENTER" HEIGHT="17%">7</td>
    <td WIDTH="16%" ID="Btn8" ALIGN="CENTER" HEIGHT="17%">8</td>
    <td WIDTH="16%" ID="Btn9" ALIGN="CENTER" HEIGHT="17%">9</td>
    <td WIDTH="16%" ID="BtnDiv" ALIGN="CENTER" HEIGHT="17%">/</td>
    <td WIDTH="16%" ID="BtnSqrt" ALIGN="CENTER" HEIGHT="17%">sqrt</td>
  </tr>
  <tr>
    <td WIDTH="20%" ID="BtnMR" ALIGN="CENTER" HEIGHT="17%">MR</td>
    <td WIDTH="16%" ID="Btn4" ALIGN="CENTER" HEIGHT="17%">4</td>
    <td WIDTH="16%" ID="Btn5" ALIGN="CENTER" HEIGHT="17%">5</td>
    <td WIDTH="16%" ID="Btn6" ALIGN="CENTER" HEIGHT="17%">6</td>
    <td WIDTH="16%" ID="BtnMult" ALIGN="CENTER" HEIGHT="17%">*</td>
    <td WIDTH="16%" ID="BtnPercent" ALIGN="CENTER" HEIGHT="17%">%</td>
  </tr>
  <tr>
    <td WIDTH="20%" ID="BtnMS" ALIGN="CENTER" HEIGHT="17%">MS</td>
    <td WIDTH="16%" ID="Btn1" ALIGN="CENTER" HEIGHT="17%">1</td>
    <td WIDTH="16%" ID="Btn2" ALIGN="CENTER" HEIGHT="17%">2</td>
    <td WIDTH="16%" ID="Btn3" ALIGN="CENTER" HEIGHT="17%">3</td>
    <td WIDTH="16%" ID="BtnSubtract" ALIGN="CENTER" HEIGHT="17%">-</td>
    <td WIDTH="16%" ID="BtnInvert" ALIGN="CENTER" HEIGHT="17%">1/x</td>
  </tr>
  <tr>
    <td WIDTH="20%" ID="BtnMPlus" ALIGN="CENTER" HEIGHT="17%">M+</td>
    <td WIDTH="16%" ID="Btn0" ALIGN="CENTER" HEIGHT="17%">0</td>
    <td WIDTH="16%" ID="BtnChangeSign" ALIGN="CENTER" HEIGHT="17%">+/-</td>
    <td WIDTH="16%" ID="BtnDot" ALIGN="CENTER" HEIGHT="17%">.</td>
    <td WIDTH="16%" ID="BtnAdd" ALIGN="CENTER" HEIGHT="17%">+</td>
    <td WIDTH="16%" ID="BtnEquals" ALIGN="CENTER" HEIGHT="17%">=</td>
  </tr>
</table>
<font color=yellow size=3 face=impact></font>


</body>
</html>
Avatar billede karl_fritjof Nybegynder
05. november 1999 - 15:53 #6
jooo det..
Avatar billede justincase Nybegynder
05. november 1999 - 17:58 #7
Hahahaha

Stone >> Gå lidt amok, vil du ikke :))
Avatar billede stone Forsker
05. november 1999 - 20:00 #8
han får da noget for sine point!!
ikk?
Avatar billede justincase Nybegynder
05. november 1999 - 20:09 #9
Det må du nok sige. Nu gælder det bare om for dig at sørge for at han har fået et svar og ikke en masse ekstra ballade !
Avatar billede dar Nybegynder
29. december 1999 - 23:12 #10
Den lommeregner er bare for fed!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester