I'm trying to use the value of a variable inside the element selector that is p:nth-child(0), instead of a number that is 0, I want to put the value of a variable, in this case, It is i of for loop.
But It is giving an error
throw new Error("n-th rule couldn't be parsed ('" + formula + "')");
^
Error: n-th rule couldn't be parsed ('')
I don't know what the freak is happening here. As far as I can see, My code seems correct.
Here's my code -
const express = require("express");
const request = require("request");
const cheerio = require("cheerio");
const app = express();
app.get("/", function(req, res){
var url = "randomGameDatabaseWebsite.com";
// get content of url by request module
request(url, function(err, response, html){
if(!err && response.statusCode == 200){
//load html to cheerio
const $ = cheerio.load(html);
//get game size
var gameSizeP;
var gameSizePStatus = false;
for(var i = 0; gameSizePStatus == true; i++;){
var text = $('p:nth-child('+i+')').text();
if(text.includes("Size: ")){
gameSizePStatus = true;
gameSizeP = i;
} else {
gameSizePStatus = false;
}
}
const gameSize = $('p:nth-child('+gameSizeP+')').text();
console.log(gameSize);
}
});
});
app.listen(3000, function(){
console.log("Application is successfully started on port 3000");
})
Edit : For more proper understanding, What I'm trying to achieve here is to get the size of the game. It is listed on the website. But the Problem is the paragraph where the game size is written doesn't always stay the same. So What I'm doing here is trying to create is that check all paragraphs and find the paragraph which has text "Size: ".
In order to achieve this, I'm doing a for loop, which will loop through all paraphs and find game size's paragraph's nth-child number. So I created gameSizeP which will hold the paragraph number since we don't know the value, It is just initialized not declared. And I created an Boolen (gameSizePStatus) which is declared as false. So For Loop will continue it's loop until gameSizePStatus is true. and It will become true when It gets a Paragraph that contains text
And sorry for typo mistake I did on for loop's condition, I typed , instead of ;.
And I'm stupid, I re-declared gameSizeP and gameSizePStatus. Fixed it but no changes to error. Getting same error.
And now when I try to run it, It throws an error. What is that error? wrote about it already above.