Asked  7 Months ago    Answers:  5   Viewed   30 times

I see this code from a book:

var a = "one";
var b = "four";
a>b; // will return true

but it doesn't mention why "one" is bigger than "four". I tried c = "a" and it is smaller than a and b. I want to know how JavaScript compares these strings.



Because, as in many programming languages, strings are compared lexicographically.

You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z.

This answer is in response to a java question, but the logic is exactly the same. Another good one: String Compare "Logic".

Tuesday, June 1, 2021
answered 7 Months ago

Maybe the following is a solution:

// loop through array1
foreach($array1 as $line) {
    // check if the word is found
    $word_found = false;

    // explode on every word
    $words = explode(" ", $line);

    // loop through every word
    foreach($words as $word) {
        if(in_array($word, $array2)) {
            $word_found = true;

    // if the word is found do something
    if($word_found) {
        echo "There is a match found.";
    } else {
        echo "No match found."

Should give you the result you want. I'm absolute sure there is a more efficient way to do this.. but thats for you 2 find out i quess.. good luck

Friday, May 28, 2021
answered 7 Months ago

Don't use Use new Date(). Because of the format of your date string, I would recommend grabbing each field individually and passing them into the constructor:

var startYear = parseInt(document.getElementById('startYear'), 10);
var startMonth = parseInt(document.getElementById('startMonth'), 10) - 1; // as per Residuum's comment
var startDay = parseInt(document.getElementById('startDay'), 10);
var start = new Date(startYear, startMonth, startDay);

etc. If you're handed a date string, then you can use fuzzy lollipop's method to get each field from the string. I'm not sure if the Date constructor will accept unparsed strings for the individual fields, however.

The, once you have the two dates you'd like to compare, just compare their values in milliseconds since the epoch:

function isValid(start, end) {
    return start.getTime() < end.getTime();
Tuesday, August 24, 2021
answered 4 Months ago

Does your column thats adding whitespace happen to be a CHAR or NCHAR instead of VARCHAR or NVARCHAR? For example if your columns datatype is CHAR(10) and you insert a "Hello" into it, there will always be 10 characters in that column so 5 whitespace characters will get added after "Hello". However, if you use VARCHAR(10), this won't happen.

Sunday, November 7, 2021
answered 1 Month ago

There is white space under your canvas, because the HTML <canvas> element has a default style of display: inline;. Inline elements are designed to contain text, and therefore white space gets added underneath the element for descenders (the bits that hang off the bottom of 'y' and 'p').

To fix the problem, apply display: block; to the element instead.

Using Javascript, you would do that like this: = "block";

//declare variables
var body = document.getElementById("body");
var canvas = document.getElementById("canvas");
var iwidth = window.innerWidth;
var iheight = window.innerHeight;

//puts canvas in top right corner = "0";

//changes the canvas's style namely color, margin, width and height = "red"; = "0"; = "block";
canvas.width = iwidth;
canvas.height = iheight;
<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
<body id="body">
  <canvas id="canvas"></canvas>
  <script src="script.js"></script>

You can also fix the problem by applying font-size: 0; to the canvas, but display: block is better because it is made to do what you want your canvas to do.

See also

Saturday, December 4, 2021
answered 2 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :