简体   繁体   中英

How can I add body to JavaScript section?

I am a new Javascript learner. This code had grabbed my interest. How can I remove what is inside the sections and add it to the sections. In other words, I want it to be a full Javascript code. Can anyone please lead or teach me to the right path or give me an example of how the code is going to look like. Thanks

Body:

<body>
    <p id="demo"></p>
    <p id="demoNew"></p>
</body>

JavaScript:

<script>
    var n = parseInt(prompt("How many numbers will be entered(max 20)-"));
    var numArray = [];
    for (var i = 0; i < n; i++) {
        numArray[i]=parseInt(prompt("Enter number between 10 and 100 "));
    }
    var uniqueNum = [...new Set(numArray)];
    document.getElementById("demo").innerHTML = "Count of items entered: "+n;
    document.getElementById("demoNew").innerHTML = "Numbers-dduplicates removed: "+uniqueNum;
</script>

Your code looks like getting 1~20 inputs between integer 10 and 100 from prompt and make a set with them but you want this without HTML.

 var p = document.createElement("p"); p.setAttribute("id", "demo"); document.getElementsByTagName('body')[0].appendChild(p); p = document.createElement("p"); p.setAttribute("id", "demoNew"); document.getElementsByTagName('body')[0].appendChild(p); //create element and append to body as a child var n = -1; while(n<1 || n>20) { n = parseInt(prompt("How many numbers will be entered(between 1 and 20)")); } var numArray = []; var input = 0; for (var i = 0; i < n; i++) { input = parseInt(prompt("Enter #"+(i+1)+" number between 10 and 100 ")); if(input>=10 && input<=100) { numArray.push(input); } else { alert("invalid input"); i--; } } var uniqueNumArray = new Set(numArray); console.log(uniqueNumArray.size); document.getElementById("demo").innerHTML = "Count of items entered: "+n; document.getElementById("demoNew").innerHTML = "Numbers-duplicates removed: "+Array.from(uniqueNumArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM