Typescript find sum from user input numbers -
Typescript find sum from user input numbers -
my task: have 1 input numbers separated commas & button outputs sum onto dom.
i wasn't confident start this. think have @ to the lowest degree of start... after searching web , stackoverflow, haven't come other ideas.
i suppose utilize array. also, professor noted initial start first adding class ("class calculate{...}) method use, i've since strayed it. not sure @ point if need go that.
html
<input id="numbers"> <input type="button" onclick="calculate()" value="click"> <div id="output"></div>
typescript
function calculate(){ var numbersinput:htmlinputelement = <htmlinputelement>document.getelementbyid('numbersinput'); var numbers:number[] = [parsefloat(numbersinput.value)]; var sum = 0; for(var i:number = 0; i, numbers.length; i++){ console.log(numbers[i]); sum += numbers[i]; } var outputelement:htmldivelement = <htmldivelement>document.getelementbyid('output'); outputelement.innerhtml = sum.tostring(); }
if understand correctly, numbersinput variable going have comma-delimited list of numbers (i.e. "3.0, 2, 1.3"). if case, can utilize string.prototype.split turn numbers array.
once have numbers in array, can run through them in loop , sum them (also see array.prototype.reduce if want challend).
you want verify each number in array valid before seek add together sum. can utilize number cast string number, utilize isnan determine if number valid before adding sum.
that should need, purposely left out code have figure part out on own.
input sum typescript addition variadic
Comments
Post a Comment