Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:05+00:00 2026-05-28T05:55:05+00:00

This piece of code causes Chrome to crash: function forecastTemp(forecast) { var end =

  • 0

This piece of code causes Chrome to crash:

function forecastTemp(forecast) {
    var end = forecast.indexOf('&'),
        fcWeather = forecast.substring(0, end).length - 3,
        temp = forecast.substring(fcWeather, end);
    if (temp.substr(0) != 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || '-') {
        do {
            fcWeather = fcWeather + 1;
            temp = forecast.substring(fcWeather, end);
        }
        while (temp.substr(0) != 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || '-');
    }
    console.log('fcWeather: ' + fcWeather + '');
    console.log('end: ' + end + '');
    console.log('temp: ' + temp + '');
    return (temp);
}

Is there a more effective way to see if the first character of a string is an integer or a negative sign and if it isn’t to delete that character from the string?

Here are two types of strings that get parsed through this.

Clear. Fog overnight. Low of -2°C with a windchill of -6°C. Winds from the ESE at 15-25 km/h.

Partly cloudy. Low of 3°C. Winds from the ESE at 40-45 km/h.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:55:06+00:00Added an answer on May 28, 2026 at 5:55 am

    You need to read up on how JavaScript (and most other languages’) == / != and || operators work. This line:

    if (temp.substr(0) != 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || '-') {
    

    …does not check to see if temp.substr(0) is one of the values given. It checks if temp.substr(0) != 0, or (separately) it checks the value 1 (which is always true), etc. So that condition will always be true.

    Since you’re doing string/character stuff, the easiest thing would probably be to use indexOf:

    if ("0123456789-".indexOf(temp.charAt(0)) >= 0) {
        // The character matches one of the ones in the string
    }
    else {
        // It doesn't
    }
    

    …or a regular expression.

    The general case for that sort of “is this value any of the following” is a switch statement:

    switch (temp.charAt(0)) {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '-':
            // It matches one of the above
            break;
        default:
            // It doesn't
            break;
    }
    

    …but again, as you’re doing string stuff, indexOf is a lot more concise and easier to read.

    Also note that I’ve put the values in quotes in the switch example. If you’re comparing strings to strings, you want to be sure that you really do that. The expression '5' == 5 will consider those two values equal (even though they’re not), the switch won’t. The reason == does is that it’s the loose equality operator. It uses the abstract equality comparison algorithm, which effectively (in this case) turns the expression into Number('5') == 5 (not '5' == String(5)). The == operator happily compares values of different types, performing a series of checks and conversions to come up with something to compare (see the link above). switch doesn’t do that. For one of the case clauses of a switch to match the expression you’re testing, the values must be of the same type — a switch will never consider '5' and 5 to match. switch uses the === (strict equality) operator, not the == (loose equality) operator.


    Note that I’ve used temp.charAt(0) rather than just temp.substr(0) (thanks Felix, I wasn’t even looking at that). temp.substr(0) just makes a copy of the whole string. You could use temp.substr(0, 1) or temp.charAt(0).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this piece of code where I lock a part of the function correct?
I have this piece of code // TR Fading when deleted $('.delete').live('click', function() {
For instance, take this piece of code: var person = new Person(); or for
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is from Adobe docs : package { import flash.display.Loader; import
This piece of code is supposed to go through a list and preform some
This piece of code gives a syntax error at the colon of elif process.loop(i,
This piece of code used to work in MVC 1 but it does not
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
Saw this piece of code in a Ruby on Rails book. This first one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.