-1

I would like to get your help.

  1. What the difference between console.log and alert? Just the way they appear? alert = pop windows and console.log in the console? Why use console.log? end-user doesn't see the console area.

  2. when I alert/console.log an array it seems to give me an all element values as string. For example:

var cars = ["Fer", "Aud", "Did"];

alert = Fer,Aud,Did

console.log = ["Fer", "Aud", "Did"], I can open it, and give some details about the array.

So what the actual output? a string? because when I use if for equal it won't work.

3.

 for(var i =1; i<=5; i++)
        {
            console.log("**********");
        }

it shows me this,https://i.imagesup.co/images2/c563895d971adaa0b798b7dfe83c51c6b5318140.png why can't I see a "rectangle" of "*" ? I can't even open "5" sign and the rectangle.

4.

       var sum = 0;
        var grade = 0;
        for(var i = 1; i<=6; i ++)
        {
            grade = prompt("Enter the grade");
            sum+=grade;
        }

        alert(sum);

Why it gives back a string of "500505050"? I defined the var with "=0" so it's should be a number.

Thanks for help !

lir5222
  • 73
  • 1
  • 8

2 Answers2

1

console.log( object ) is for the developer, usually for debugging, just in the console. It can include interactive object structures.

alert( string ) is a pop-up message for the user, formated as raw text. If you feed it an object, it will output '[object Object]'.

You can not see a rectangle of asterisks becasue the console considers repeating it redundant and will just show you the amout. You can however include '\r\n' newlines in your console messages instead like this:

console.log( "*****\r\n".repeat( 5 ) );

grade is a string. You set it to a string in grade = prompt("Enter the grade"); To make it a number, use grade = Number.parseInt( prompt( 'Enter the grade' ) );

Peri
  • 574
  • 1
  • 3
  • 19
  • Coll, thanks. Just about r\n, it doesn't show you the "real" output. You just did the loop/duplicate with repeat. there is another way? – lir5222 Nov 09 '19 at 17:20
  • You can log an empty message after, but that will leave gaps between the lines. `for ( let i = 0; i < 5; i++ ) { console.log( '*****' ); console.log( '' ); }`. If this annoys you, the only other way is to implement a custom console.log that gathers the logged lines, adds newlines in between and outputs to the real console every so often or after calling a method. – Peri Nov 09 '19 at 17:24
0

1, 2. You can find plenty info in questions like this. Alert is a message box that appears (the argument is converted to a string) and blocks user interaction with the rest of page: it's rarely used to output stuff. Likewise, prompt is rarely used to get input. Console logging is usually there for debugging purposes of the developer, it's supposed to be hidden. If the argument is solely an object/array, its content will be logged (with a catch), otherwise it will be stringified. E.g.

console.log(anObject)

will log the object content, whereas

console.log(""+anObject)

will stringify it.

  1. Browser consoles usually collapse consecutive identical logs, indeed you get 5 times the asterisks

  2. prompt takes any input as a string, so you are effectively concatenating strings in that loop

Danielozzo
  • 76
  • 1
  • 4