javascript weird console logs

javascript alternative console log
javascript funny
wtf javascript
conditional console log
console.log() in javascript
javascript console log flush
console log separator
alternatives to console log

I've got a assignment to make function that gives you a sorted array with 6 random numbers from 1 to 45. None of the array values should equal to one another.

I thought about a solution that would work in Java but the JavaScript console logs I get are pretty confusing. Could anyone help me out?

"use strict";

var numbers = [];
for(var x = 1; x <46;x++){
    numbers.push(x);
}


function LottoTipp(){
    var result = [];

    for(var i = 0; i <6; i++){
      var randomNum = Math.round(Math.random()* 45);
      var pushed = numbers[randomNum];
      result.push(pushed);
      numbers.splice(randomNum)
    }

    return console.log(result) + console.log(numbers);

}

LottoTipp(); 

the console logs

[ 34, 7, undefined, undefined, undefined, undefined ]

[ 1, 2, 3, 4, 5, 6 ]

Beyond console.log(). There is more to debugging JavaScript…, There is more to debugging JavaScript than console.log to output values. onto anything that follows, there's no “end tag”, which is a bit weird. console.log — Logs a message or an object to the console. console.info — Logs a message or an object to the console, which is informational. console.warn — Logs the console log message as a

If you only want an array with random unique numbers I would suggest doing it like this:

<script>
     var array = [];
     for(i = 0; i < 6; i++) {
        var number = Math.round(Math.random() *45);
        if(array.indexOf(number) == -1) { //if number is not already inside the array
            array.push(number);
        } else { //if number is inside the array, put back the counter by one
            i--;
        }
     }
     console.log(array);
</script>

JavaScript Console Debugging: Beyond the Basics, log('Hello World!'); console.info('Informational Logging'); console.warn('Warning to indicate something weird'); console.error(� The corresponding list item is passed to the call function so that the the keyword in the console statement logs the innerHTML of the correct object. Objects can have methods, likewise functions being objects can also have methods. In fact, a JavaScript function comes with four built-in methods which are: Function.prototype.apply()

There is no issue with the console statement the issue is that you are modifying the numbers array in your for loop. As you are picking the random number between 1-45 in this statement:-

var randomNum = Math.round(Math.random()* 45);

and you expect that value would be present in the numbers array at that random index. However you are using array.splice() and providing only first parameter to the function. The first parameter is the start index from which you want to start deleting elements, find syntax here. This results in deleting all the next values in the array.Therefore if you pick a random number number say.. 40, value at numbers[40] is undefined as you have deleted contents of the array.

if you want to generate unique set of numbers follow this post.

hope it helps!

denysdovhan/wtfjs: A list of funny and tricky JavaScript , So this is what happens I try to console.log the original array before splicing it. So I expect an array with the length of 5, because its not yet … The second console.log statement prints the word Ford Ranger which is weird because in our first console.log statement it printed Ford Mustang. The reason to this is that the getCarName method has a different "owner" object that is the window object.

Just add the number in the result if it is unique otherwise take out a new number and then sort it. Here is an implementation:

let result = []
while(result.length < 6) {
    let num = Math.round(Math.random() * 45);
    if(!result.includes(num)) {
        result.push(num);
    }
}
result.sort((a,b) => {
    return parseInt(a) - parseInt(b);
});
console.log(result);

Weird Behavior logging an array with console.log : javascript, I am aware that, this is how JS works but it makes me feel weird about JS. console.log(isNaN('jiten')) => prints true (Instead use Number. That absolutely works. Weird how the FCC ‘console log’ just quit working. It was working fine for months and stopped about an hour ago. Tried everything from updating browser to restarting.

Weird parts of JavaScript?, Go beyond silly alert statements and learn how to use the console to get a better view into Console Logging Basics. by kirupa | filed under JavaScript 101 It's funny! In both of these situations, what you need is some extra visibility into what � Console logging — great for small, simple, isolated errors, but not so great in larger, more complex projects. Using developer tools — whether the tools integrated in major browsers or the debugger in your IDE, this is almost certainly the most reliable way to debug code.

Console Logging Basics, The JavaScript console is an invaluable tool to help develop and debug our console.log is the usual method we use to log values out to the console: For example, here we have two counters, one for even values and one for odd values :. Introduced in ES6, they are a great help in avoiding some of the weird things about var declarations. Changing var to let in the loop variable is going to work fine: const operations = [] for ( let i = 0 ; i < 5 ; i ++ ) { operations . push (() => { console . log ( i ) }) } for ( const operation of operations ) { operation () }

A Look at the JavaScript Console API, Who hasn't peppered their code with console.logs in an attempt to find that pesky bug? Tagged with javascript, webdev, productivity, tutorial. 'Python is a powerful, opinionated and idiosyncratic (odd) scripting language,� Javascript is weird. Don’t believe me? Try converting an array of strings into integers using map and parseInt. Fire up your console (F12 on Chrome), paste in the following, and press enter (or

Comments
  • You don't use splicecorrectly
  • Also, since you added it back, how is Java relevant?
  • your splice is messed up, it should be splice(index, number of delete items)
  • just add splice(randomNum, 1) and it outputs (6) [33, 7, 25, 24, 26, 28] (39) [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]
  • still, your logic isnt right since random numbers could eventually produce the same arrays. also, splice modifies the original array, so you need to make a copy before you start splicing it.
  • Shouldn't it be var randomNum = Math.floor(Math.random()* numbers.length);
  • thanks a lot !well i did that but now the console shows TypeError: numbers.lenght is not a function
  • thats because it has to be length and not lenght :)
  • I have to insist that Math.floor is correct. math.round leads to undefined entry, if you click "Run Code Snippet" often enough.
  • got one more point only for your information: you dont need to use .indexOf in this case, since randomNum is already the index of the number you've pushed. My Fault, updated again. :)
  • While this could be a nice solution, the OP will not get whats going wrong with its own code. So there will be no learning effect.
  • @J.Sadi is right but i appreciate the help and perspective on another way to do this assignment. thank you :)
  • Welcome to Stack Overflow. While it may answer the question, try your best to not just paste code, but give explanation along.
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: How to anwser. Thanks!