Java Reference
In-Depth Information
// Check if n is divisible by any odd integers between 3 and sqrt(n).
var sqrt = Math.sqrt(n);
for (var i = 3; i <= sqrt; i += 2) {
if (n % i === 0) {
return false;
}
}
return true; // If we get here, it is a prime number.
}
// Check few nubmers for being primes
var num = 8;
var isPrimeNum = isPrime(num);
print(num + " is a prime number: " + isPrimeNum);
debugger;
num = 37;
isPrimeNum = isPrime(num);
print(num + " is a prime number: " + isPrimeNum);
Debugging Standalone Scripts
To run or debug a standalone Nashorn script in NetBeans, first you need to open the
script file in the NetBeans IDE. Figure 13-1 shows the script shown in Listing 13-1 open in
NetBeans. To run the script, right click in the editor showing the script and select the Run
File menu item. Alternatively, press Shift + F6 while the script pane is active.
Figure 13-1. A Nashorn script opened in the NetBeans IDE
 
Search WWH ::




Custom Search