Do you want to define the type before using the js variable?

topic description

Your task is to drive your car from 1 to 60 Kilometers per hour by printing a pattern.
A hyphen represents the speed of cars.
one hyphen is counted as 1 kph.
To put it simply, output 1 to 60 hyphens in turn.

sources of topics and their own ideas

(the title comes from this) [ https://edabit.com/challenge/.].

related codes

function Go(num) {
    var result = "";
    for(var i=0; i<num; PPi){
        result+="-";
    }
    return result;
}

what result do you expect? What is the error message actually seen?

num is tested from 1 to 60. So you should actually see 1 to 60 "-" output in turn. Such as "-", "-", "-". The above code is correct, but I try to put

var result = "";
Change

to

var result;

the result will be wrong. Why is that? Do I have to define result as a string first?

Apr.09,2021

you forget result+= "-" is actually result=result+ "-"

when considering character concatenation.

when result has no data, result is added to a character. Result is a "undefined". After this addition, result becomes "undefined-" and then continues. Then everything will be normal.

the problem is that an unassigned variable participates in string concatenation


var result = ""; declared to be a string type, var result; only declares no defined type. + = generally used on number or string types


Don't mislead others. Js makes it clear that there is no need to declare types because it is a dynamic language and does not need to be compiled in advance.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a6e7-2af57.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a6e7-2af57.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?