Alert a line of code from a JavaScript file [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a JavaScript error log and one of the columns is the line number where the error occurred. I think it would save me time from having to go through the source from the browser's perspective if I could just click the link and get a JavaScript alert with that line (or lines) of code.
So in short: how can I retrieve a specific line or lines of code from a JavaScript file that is included via a script element in the head of the code?
I don't use frameworks.

Modern day browsers ship with a debugging console. They log the error message and a stack trace whenever errors happen. Line numbers for the error and involved functions in the stack are usually provided and are clickable to take you the the line where it happened.

This solution isn't perfect as it creates an unnecessary request to the server however it does exactly what I wanted to do...
function getLines(haystack,from,toIncluding)
{
var i = 0
var j = 0;
var result = '';
--from;
while (from-- >0 && i !== -1)
{
--toIncluding, i = haystack.indexOf('\n', i + 1);
}
if (i===-1) {result = '';}
j = i;
while (toIncluding-->0 && j !== -1)
{
j = haystack.indexOf('\n', j + 1);
}
if (j === -1) {j = haystack.length;}
result = haystack.slice(i + 1, j);
return result;
}
...and then simply use...
if (window.XMLHttpRequest)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET',document.getElementsByTagName('script')[0].src,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState=='4')
{
alert(getLines(xmlhttp.responseText,2,7));
}
}
}

Related

Why some parts of this code are red in VS Code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I just started to learn JavaScript. I have this script which should generate composite numbers in my separate HTML file, but I don't really understand why some parts are highlighted red when I moved script to .js file.
So my question is why there are some parts that are highlighted and how I'm suppose to write it correctly? I need to use strict in this script. I know that when using strict mode I need to declare variables, objects etc.
since you are working in a javascript file.
remove the script tags
you only need script tags in other file types like html etc
function checkComposite(num) {
var arr = [];
if( var num == 1 ) {
return false;
}
else if ( var num == 2) {
return false;
}
for (var x = 2; x < num; x++) {
if(num % x == 0) {
return true;
}
}
return false;
}
function compositeNumbers() {
num = Number(document.getElementsById('number').value);
for(var j = 1; j < num; j++) {
if(checkComposite(j)) {
arr.push(j);
}
}
document.getElementsById('result').innerHTML = arr;
}

How to calculate the correct characters the user typed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
[Ask]
anyone know the reference for a case like this?
the user must type the word: "learn" -> total character 5
while the user types instead: "l3arn"
well I want to count the right word only, so the result 4
from google not yet find a solution, maybe someone knows the keyword/reference for the problem.
I want to implement it in javascript
You want to calculate the number of characters in the correct position?
In that case, it's a simple solution.
Javascript example:
function countCorrectCharacters(expectedString, string) {
var count = 0;
var l = Math.min(expectedString.length, string.length);
for (var i = 0; i < l; ++i) {
if (expectedString[i] === string[i]) {
++count;
}
}
return count;
}
var str = "learn";
var userInput = "l3arn";
var userInputArray = userInput.split('');
var counter = 0;
for(var i = 0; i< "l3arn".lenth(); i++){
if(str.indexOf(userInputArray[i]) !== -1) counter++;
}
If I understand correctly you want to count the number of letters in a string? Use this:
function getNumberOfLetters(string) {
let match = string.match(/[A-Za-z]/g);
return (match && match.length) || 0;
}
console.log(getNumberOfLetters("learn")); // 5
console.log(getNumberOfLetters("l3arn")); // 4
console.log(getNumberOfLetters("12345")); // 0

JSON data types - numbers are converting to strings when it gets to the server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am loading a CSV file, parsing it into a JSON object, then I am converting those strings into numbers. They show as numbers in the browser console, then using AJAX I am sending the data to the server. When I console.log the data everything is strings? I though JSON could accept a variety of data types, so why is it going down the line as strings?
Here is the code.
for(var i = 0; i < input.files.length; i++){
var files = input.files[i];
Papa.parse(files, {
header:false,
dynamictyping:true,
complete: function(results){
var input = results.data;
if(input[0][0] === 'Symbol' || input[0][0] === 'symbol'){
input.shift();
}
input.forEach(function(input){
jsonData.theData = theData;
var singleEntry = {
"symbol" : input[0],
"date" : input[1],
"open" : Number(input[2]),
"high" : Number(input[3]),
"low" : Number(input[4]),
"close" : Number(input[5]),
"volume" : Number(input[6])
};
// Here we will try to do the daily computations of what is needed for data
// such as percentage closed in the day and what not.
var open = singleEntry.open;
var high = singleEntry.high;
var low = singleEntry.low;
var close = singleEntry.close;
/*
console.log(open);
console.log(high);
console.log(low);
console.log(close); */
//Get the Math variables for close percentage
var spread = high - low;
var closeDiff = close - low;
var answer = closeDiff / spread;
console.log(answer);
//Adding day closes to object
if (singleEntry.volume === 0){
singleEntry["supportDay"] = false;
} else {
if(answer <= .3999){
singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = false;
console.log("answer <= .39999");
} else if (answer > .95) {
singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = true;
singleEntry["peakClose"] = true;
console.log("answer > .95");
} else {
singleEntry["percentClose"] = answer;
singleEntry["supportDay"] = true;
}
}
jsonData.theData.push(singleEntry);
console.log(singleEntry.supportDay);
return jsonData;
}); // End forEach loop
document.getElementById("editor").innerHTML = JSON.stringify(jsonData.theData[0]);
} // End Callback Complete
}); // End PapaParse
} // End for loop
});
So as you can see I can work with the objects and here is the console.log output on the browser, all that code is in the browser.
Now here is the console.log to the server:
This data gets imported straight into the database there is no working with the data in Node. In my database it imports as strings as well.
Thoughts? What am I missing?
With JSON, you're inputs can be of various data types.However, it gets serialized as one big string and sent down the wire to your server as such. On the server side, the string needs to be parsed into an object again. That step will vary depending on what sort of language your server operates in.

Parse.com Overwrites my results when Javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm not sure what am I doing wrong.
When running the attached code I would expect to see 100 rows inside Parse.com.
Instead, I only see only one.
For some reason, Parse.com is overwriting the same line 100 times.
And ideas? :/
Me.
function simplewrite() {
Parse.initialize("********************, ***********************");
var Numbers = Parse.Object.extend("Numbers");
var numbers = new Numbers();
for (var i=0; i<100; i++) {
numbers.set("number", i);
numbers.save(null, {
success: function(numbers) {
console.log("YES " + i);
},
error: function(numbers, error) {
console.log("no! " + i);
console.log(error);
}
}); // End
};
};
Your for loop is updating the same numbers variable over and over. Try moving the initialization of numbers to inside your for loop:
// ...
for (var i=0; i<100; i++) {
var numbers = new Numbers();
// ...

Undefined var error in an array iteration on a page using mootools [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
Why is this firing back an error of Microsoft JScript runtime error: 'selectedMakeModelYearItem' is undefined?
Mootools won't let me use a simple for...in for iterations.
I've looked at it 6 ways to Sunday so what the heck am I missing?
Because selectedMakeModelYearItem is undefined.
selectedMakeModelYearItems isn't, though.
Maybe you try to call this code berofe page is loaded. In this case select tag that you try to access don't rendered and cannot be accessed from JavaScript. You can try something like
window.addEventListener("load",
(function() {
return function setMakeModelYearFilter() {
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
window.removeEventListener('load', setMakeModelYearFilter, false);
}})()
, false);

Categories

Resources