Home > Software engineering >  Number of lines in a text file (10) is smaller than a specific number (5)
Number of lines in a text file (10) is smaller than a specific number (5)

Time:11-07

I am trying to make an application where there is a button that after you click it, it will count the number of lines a specific text file has (in my case the text file has 10 lines of random words), and it will compare the number of the lines in a text file with a specific number (in my case the number is 5), and if the number of the lines in a text file(10) is bigger than the specific number(5), it will console.log('true') otherwise it will console.log('false'). But for some reason it console.log('false'), and I don't know the reason why it's happening because 10 is bigger than 5. Can anyone please help me?

Here is the content inside my text file

test
test
test
test
test
test
test
test
test
test

Here is the content inside my json file

{
    "data": [
        {
            "number": 5,
            "textFile": "data.txt"
        }
    ]
}

Here is the content inside my ejs file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <% data.data.forEach(function(item) { %>
        <div id="shopItem">
            <button id="btn">Click Me!</button>
            <div id="number" style="opacity: 0;"><%= item.number %></div>
            <div id="textFile" style="opacity: 0;"><%= item.textFile %></div>
        </div>
    <% }) %>

    <script src="index.js"></script>
</body>
</html>

Here is the content of my javascript file

document.getElementById('btn').addEventListener('click', function() {
    let textFile = document.getElementById('textFile').innerHTML
    let number = document.getElementById('number').innerHTML

    fetch('/number', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            textFile: textFile
        }),
    })
    .then((Response) => {
        return Response.text()
    })
    .then((nLines) => {
        if (nLines >= number) {
            console.log('true')
        } else if (nLines < number) {
            console.log('false')
        }
    })
    .catch((error) => {
        console.error(error);
    })
})

And here is the content inside of my nodejs file

const express = require('express')
const app = express()
const fs = require('fs')
const path = require('path')

app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.json())

app.get('/', (req, res) => {
    fs.readFile('data.json', (error, data) => {
        if (error) {
            res.status(500).end()
        } else {
            res.render('index.ejs', {
                data: JSON.parse(data)
            })
        }
    })
})

app.post('/number', (req, res) => {
    const textFile = req.body.textFile

    const filePath = path.join(__dirname   `/public/${textFile}`)
    const fileData = fs.readFileSync(filePath, 'utf-8')

    let nLines = 0;
    for( let i = 0; i < fileData.length;    i ) {
      if( fileData[i] === '\n' ) {
            nLines;
      }
    }

    res.json(nLines   1)
})

app.listen(3000, console.log('Server started!'))

CodePudding user response:

nLines and number are strings. nLines >= number is a lexicographical comparison. You could convert the strings to numbers:

if ( nLines >=  number) {
    console.log('true')
} else {
    console.log('false')
}
  • Related