How to resolve the algorithm A+B step by step in the CoffeeScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm A+B step by step in the CoffeeScript programming language
Table of Contents
Problem Statement
A+B ─── a classic problem in programming contests, it's given so contestants can gain familiarity with the online judging system being used.
Given two integers, A and B. Their sum needs to be calculated.
Two integers are written in the input stream, separated by space(s):
The required output is one integer: the sum of A and B.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm A+B step by step in the CoffeeScript programming language
Source code in the coffeescript programming language
<html>
<script type="text/javascript" src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
<script type="text/coffeescript">
a = window.prompt 'enter A number', ''
b = window.prompt 'enter B number', ''
document.getElementById('input').innerHTML = a + ' ' + b
sum = parseInt(a) + parseInt(b)
document.getElementById('output').innerHTML = sum
</script>
<body>
<div id='input'></div>
<div id='output'></div>
</body>
</html>
{ stdin } = process
sum = ( a, b ) -> a + b
display = ( messages... ) -> console.log messages...
parse = ( input ) ->
parseInt x for x in ( x.trim() for x in input.split ' ' ) when x?.length
check = ( numbers... ) ->
return no for x in numbers when isNaN x
return no for x in numbers when not ( -1000 < x < 1000 )
yes
prompt = ->
display 'Please enter two integers between -1000 and 1000, separated by a space:'
stdin.once 'data', ( data ) ->
[ a, b ] = parse data
if check a, b
display "#{ a } + #{ b } = #{ sum a, b }"
else
display "Invalid input: #{ a }, #{ b }"
do prompt
return
# Resume input and set the incoming encoding.
stdin.resume()
stdin.setEncoding 'utf8'
# Start the main loop.
do prompt
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Sidef programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the Scala programming language
You may also check:How to resolve the algorithm Ordered partitions step by step in the D programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Arena storage pool step by step in the Oforth programming language