Why is chrome giving me a TypeError when finding largest number in array?

I have the following code to find the largest number in an array:

function max(input) {
 var maxNum = 0;
 for(var i = 0; i <= input.length; i++) {
     if(maxNum < input[i]) {
         maxNum = input[i];
    return maxNum;
max([1, 2, 3, 7]);

When I run it in Google Chrome, Edge, or Firefox I get:

max.js:20 Uncaught TypeError: Cannot read property 'length' of undefined
    at max (max.js:20)
    at :1:1

but when I run it on pythontutor for JavaScript, I get the correct results without any syntax error.

javascript how to insert new elements at different locations in page

I’m writing a chrome extension that will add helper text instructions/reminders to specific location in the “new order” form we use at work. I’m really new to coding (basically using this project as a way to learn). I’ve created something that works – but I’m convinced there’s a more elegant solution that I just haven’t been able to figure out.

var helpText = "this is the message"
var customAlert = makeAlert(helpText)   //create html container for msg

function makerAlert(helpText){...}   //createElem, add class/style, append children

I’m okay with that bit (above). But should i be storing information on each message in objects instead? why would/wouldn’t i? what information would go in it?

function alertPlacer(customAlert){
    var par = document.getElementsByClassName("class-name")[i];
    var sib = par.childNodes[j];
    par.insertBefore(customAlert, sib);

really struggling with this bit (above). I have actually made alertPlacer() functions for each message because i can’t figure out how to create a function that will take different class name & index parameters. should i be breaking this up more? if i stored these bits of info in an object, would that be useful?

relevant info:

  • because the target locations are within a form, almost nothing has an “id” attribute. so i have to use getElementsByClassName & an index.
  • for each message, I know the target parent className & index and the child node index to “insert before”.
  • i would like to stick with javascript-only solution.

Returning Current URL from nested function – Chrome Extension

I’m trying to make a chrome extension that gets the URL of the currently selected tab, but I’m running into a problem where I can’t get the variable that the URL is stored in to return.

I’ve tried several things already but this is the code that I’m expecting to work:

function getStream() {

function getLinking() {
            'active': true,
            'windowId': chrome.windows.WINDOW_ID_CURRENT
        function getURL(tabs) {
            var URL = tabs[0].url;
            return URL;
    return getURL();

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('start').addEventListener('click', getStream);

However it doesn’t actually return anything so the alert doesn’t show. Please note that it’s very important for me to return the variable URL so I can use it back in the getStream function, just adding in the alert in the getURL function isn’t what I’m looking for.

If anyone could help me that would be amazing, I’m not too familiar with javascript yet and I’m especially unfamiliar with chrome extension so every bit of advice could help.

Implement function into Chrome extension

Im a beginner programmer and decided to try making a java program I made into a Chrome extension. My trouble is that I haven’t found alot of information regarding implementing functions into the extension along with texct inputs.

So far I have made a small manifest along with a popup describing what the player has to do, along with the function needed (used an online java to javascript converter).

My question is, is it even possible to run a function inside a Chrome extension, and in that case how?

My extension parts so far:

"name": "Impulse",
"version": "1.0",
"manifest_version": 2,
"description": "Calculate your level based on your exp in the pott.",
"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "mypopup.html"

And the popup part


Please insert level and EXP

The popup is working, and the description is there. I just have no idea how to make a function work within an extension as I need it for the sheet to the calculation to work.

The function I have made into javascript is here
How do I make the extension use it for the calculations?

Why doesn’t alert work as expected?

my javascript:

function getCurrentTabId(){
    chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(d){
      alert( d[0].id ); // returns expected result '2'
      return d[0].id;

  alert( getCurrentTabId() ); // returns 'undefined'

How do I get the last line “alert( getCurrentTabId() )” to display 2 instead of undefined?

Javascript – Detecting Opinionated Text

I’m trying write a script that detects, and highlights, opinionated strings in web pages. I can highlight the text just fine, but I’m having trouble detecting the opinionated text. As an alpha version, maybe simply putting in a list of strings (ei: I think, This is, etc) and if it detects those strings, highlighting the entire sentence they’re in. I’m not sure how complicated the code will have to be to make this, but currently I’m not sure how to even test this current function because I don’t know how to execute it, to make it running every time you open a new web page. Would I put it in background.js? This code is currently in a file named algorithm.js. Thanks!

 var result;
 var scanned;
 function opinion(); {
     if (
    document.documentElement.textContent || document.documentElement.innerText
  ).indexOf('I think', 'This is', 'He is', 'She is') > -1
) {
  var result = 1;
  console.log('Detected Opinion');