Home > Web Front-end > JS Tutorial > A simple method to calculate the number of lines of code using js with code_javascript skills

A simple method to calculate the number of lines of code using js with code_javascript skills

WBOY
Release: 2016-05-16 19:10:17
Original
1350 people have browsed it

A piece of code may have dozens of lines or thousands of characters. To count the number of lines, you can first convert the code string into an array, and then return the length of the array.
Traditional approach:




After converting to an array, the efficiency is much higher:


msgbox("hello world!")

<script> <BR>var counter = 0; <BR>var str = abc.value; <BR>for(i=0; i<str.length; i++) <BR>{ <BR> if(str.substr(i,1)=="\n") <BR> counter +=1; <BR>} <BR>alert(counter); <BR></script><script> <BR> alert(abc.value.split("\n").length) <BR></script> <script> <BR> alert("代码总行数:"+(abc.value.split("\n").length-1).toString()) <BR></script>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template