How to Calculate Interest on a JS Node API?

Asked

Viewed 126 times

-2

I created a function that takes a value and adds 10% on top of the value.

The question is:

  • I want that if the user enters a value equal to or less than 100 it adds 10%. Now if the value is greater than 100 it adds 15%.

Take the example:

const express = require('express')
const bodyParser = require('body-parser')

const app = express()

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

const porcentagem =  parseFloat (10/100)

const calculaImpost = (valor) => {
  return parseFloat(valor * porcentagem)
}

app.post('/calcular', (req, res) => {
  res.json({
    imposto: calculaImpost(req.body.valor)
  })
})

app.listen(3000, (req, res) => {
    console.log('Server is Running')
}) 

1 answer

0

Hello,

What is the need for parseFloat? 10/100 = 0.1, parseFloat(10/100) = 0.1

answering your question

const calculaImpost = (valor) => {
  if(valor === 100 || valor < 100) {
   return valor * (10/100);
  } else {
   return valor * (15/100);
  }
}
  • show!!! Thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.