Why does this for loop log letters instead of numbers? [closed]Why does Google prepend while(1); to their JSON responses?How to initialize an array's length in javascript?Inspecting javascript on jsfiddle.net in Google ChromeWhy does ++[[]][+[]]+[+[]] return the string “10”?Why does JavaScript only work after opening developer tools in IE once?Why does parseInt(1/0, 19) return 18?Detect iPad Mini in HTML5AngularJS : Initialize service with asynchronous dataWhat is the role of variables (var) in JavascriptWhy does string.replace(/W*/g,'_') prepend all characters?
Debian 9 server no sshd in auth.log
TIP120 Transistor + Solenoid Failing Randomly
Huffman Code in C++
Class Not Passing SObject By Reference
What is a precise issue with allowing getters?
Can a player choose to add detail and flavor to their character's spells and abilities?
What are the requirements for a river delta to form?
Which version of the Squat Nimbleness feat is correct?
Can I combine SELECT TOP() with the IN operator?
Sci-fi/fantasy book - ships on steel runners skating across ice sheets
Why would a military not separate its forces into different branches?
Why does blending blueberries, milk, banana and vanilla extract cause the mixture to have a yogurty consistency?
In "Avengers: Endgame", what does this name refer to?
Gerrymandering Puzzle - Rig the Election
How did the Force make Luke hard to hit in the Battle of Yavin?
Subnumcases as a part of align
What is monoid homomorphism exactly?
How to deal with employer who keeps me at work after working hours
What does the copyright in a dissertation protect exactly?
Collision domain question
Why increasing of the temperature of the objects like wood, paper etc. doesn't fire them?
Problem with estimating a sequence with intuition
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
Hostile Divisor Numbers
Why does this for loop log letters instead of numbers? [closed]
Why does Google prepend while(1); to their JSON responses?How to initialize an array's length in javascript?Inspecting javascript on jsfiddle.net in Google ChromeWhy does ++[[]][+[]]+[+[]] return the string “10”?Why does JavaScript only work after opening developer tools in IE once?Why does parseInt(1/0, 19) return 18?Detect iPad Mini in HTML5AngularJS : Initialize service with asynchronous dataWhat is the role of variables (var) in JavascriptWhy does string.replace(/W*/g,'_') prepend all characters?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
The var is set to an integer and as far as I know i++ means incrementing by 1. I do not understand why it prints all the characters of the variable sentence instead of all the integer that would correspond to the length.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
javascript
closed as unclear what you're asking by Pointy, Gerardo Furtado, SherylHohman, Code Maniac, Alexander Kogtenkov Mar 23 at 9:32
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
The var is set to an integer and as far as I know i++ means incrementing by 1. I do not understand why it prints all the characters of the variable sentence instead of all the integer that would correspond to the length.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
javascript
closed as unclear what you're asking by Pointy, Gerardo Furtado, SherylHohman, Code Maniac, Alexander Kogtenkov Mar 23 at 9:32
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
3
sentence[i]means "extract the character at positioniin the string and create a new string containing only that character"
– Pointy
Mar 23 at 4:31
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:[0, 1, 2, 3...]?
– zer00ne
Mar 23 at 4:50
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40
add a comment |
The var is set to an integer and as far as I know i++ means incrementing by 1. I do not understand why it prints all the characters of the variable sentence instead of all the integer that would correspond to the length.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
javascript
The var is set to an integer and as far as I know i++ means incrementing by 1. I do not understand why it prints all the characters of the variable sentence instead of all the integer that would correspond to the length.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence[i]);
javascript
javascript
edited Mar 23 at 5:20
TuCraiN
asked Mar 23 at 4:30
TuCraiNTuCraiN
235
235
closed as unclear what you're asking by Pointy, Gerardo Furtado, SherylHohman, Code Maniac, Alexander Kogtenkov Mar 23 at 9:32
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Pointy, Gerardo Furtado, SherylHohman, Code Maniac, Alexander Kogtenkov Mar 23 at 9:32
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
3
sentence[i]means "extract the character at positioniin the string and create a new string containing only that character"
– Pointy
Mar 23 at 4:31
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:[0, 1, 2, 3...]?
– zer00ne
Mar 23 at 4:50
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40
add a comment |
3
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
3
sentence[i]means "extract the character at positioniin the string and create a new string containing only that character"
– Pointy
Mar 23 at 4:31
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:[0, 1, 2, 3...]?
– zer00ne
Mar 23 at 4:50
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40
3
3
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
3
3
sentence[i] means "extract the character at position i in the string and create a new string containing only that character"– Pointy
Mar 23 at 4:31
sentence[i] means "extract the character at position i in the string and create a new string containing only that character"– Pointy
Mar 23 at 4:31
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:
[0, 1, 2, 3...]?– zer00ne
Mar 23 at 4:50
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:
[0, 1, 2, 3...]?– zer00ne
Mar 23 at 4:50
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40
add a comment |
4 Answers
4
active
oldest
votes
String Character access
There are two ways to access an individual character in a string. The first is the
charAt()method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
add a comment |
As others have stated, var sentence = "..." creates a collection or array of characters that you are then looping over.
A minor change in your code to help explain.
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
add a comment |
Your question is not entirely clear.
Do you want to print the index of every character that does not correspond to as space ("")?
If so, below would do that.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
In the above code, I am printing the index, i, as long as the value at that index (i) is not a space character. Your code sentence[i] tells it to print the value at the index (i), which would be the character at that location in the string.
On the other hand if you want the Ascii value at index i, you need to use the charAt function: sentence.charAt[i].
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
As another comment suggested, "...instead of all the integer that would correspond to the length" is unclear.
There is only 1 length for any given string. This is also true for any array at in any particular point in time.
Perhaps you mean index?
Each character has a unique index in any particular string.
To answer your question from the comments below:
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
sentence: I (space) l o v e (space) l e a r n i n g software development
There are a total of 36 characters in the sentence. (If I counted correctly)
index values start at 0 and end at 35
length is 36. But remember the last index number is 35, and the first is 0.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, theindexis the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.
– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (iin your code) is a location, kind of like counting. But unlike "normal life", it starts at0instead of1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.
– SherylHohman
Mar 23 at 6:22
add a comment |
Perhaps you are experienced in a different language where indexing a character of a string would return a integer.
In JavaScript, you get a human representation of the string. The string is initially encoded in UTF-16, converted into binary, and then stored in memory. However, when you access it, you get the actual string instead of UTF-16 code due to an language-specific automatic conversion.
However, if you are using Node.js you can use a buffer to store the string and deal with binary data directly.
In JavaScript, if you want to get the code you can use codePointAt(index) method:
alert( "z".codePointAt(0) ); // 122
alert( "Z".codePointAt(0) ); // 90
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
String Character access
There are two ways to access an individual character in a string. The first is the
charAt()method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
add a comment |
String Character access
There are two ways to access an individual character in a string. The first is the
charAt()method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
add a comment |
String Character access
There are two ways to access an individual character in a string. The first is the
charAt()method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
String Character access
There are two ways to access an individual character in a string. The first is the
charAt()method:
return 'cat'.charAt(1); // returns "a"
The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:
return 'cat'[1]; // returns "a"
answered Mar 23 at 4:33
MamunMamun
31.6k71933
31.6k71933
add a comment |
add a comment |
As others have stated, var sentence = "..." creates a collection or array of characters that you are then looping over.
A minor change in your code to help explain.
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
add a comment |
As others have stated, var sentence = "..." creates a collection or array of characters that you are then looping over.
A minor change in your code to help explain.
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
add a comment |
As others have stated, var sentence = "..." creates a collection or array of characters that you are then looping over.
A minor change in your code to help explain.
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
As others have stated, var sentence = "..." creates a collection or array of characters that you are then looping over.
A minor change in your code to help explain.
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
// Create a string (or an array of characters)
var sentence = "I love learning software development";
// Loop over the length of `sentence`.
// `i` is the index in the array from 0 to `sentence.length`
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log("char:["+ sentence[i] +"] i:["+ i +"]");
answered Mar 23 at 5:05
TiggerTigger
6,59432835
6,59432835
add a comment |
add a comment |
Your question is not entirely clear.
Do you want to print the index of every character that does not correspond to as space ("")?
If so, below would do that.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
In the above code, I am printing the index, i, as long as the value at that index (i) is not a space character. Your code sentence[i] tells it to print the value at the index (i), which would be the character at that location in the string.
On the other hand if you want the Ascii value at index i, you need to use the charAt function: sentence.charAt[i].
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
As another comment suggested, "...instead of all the integer that would correspond to the length" is unclear.
There is only 1 length for any given string. This is also true for any array at in any particular point in time.
Perhaps you mean index?
Each character has a unique index in any particular string.
To answer your question from the comments below:
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
sentence: I (space) l o v e (space) l e a r n i n g software development
There are a total of 36 characters in the sentence. (If I counted correctly)
index values start at 0 and end at 35
length is 36. But remember the last index number is 35, and the first is 0.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, theindexis the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.
– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (iin your code) is a location, kind of like counting. But unlike "normal life", it starts at0instead of1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.
– SherylHohman
Mar 23 at 6:22
add a comment |
Your question is not entirely clear.
Do you want to print the index of every character that does not correspond to as space ("")?
If so, below would do that.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
In the above code, I am printing the index, i, as long as the value at that index (i) is not a space character. Your code sentence[i] tells it to print the value at the index (i), which would be the character at that location in the string.
On the other hand if you want the Ascii value at index i, you need to use the charAt function: sentence.charAt[i].
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
As another comment suggested, "...instead of all the integer that would correspond to the length" is unclear.
There is only 1 length for any given string. This is also true for any array at in any particular point in time.
Perhaps you mean index?
Each character has a unique index in any particular string.
To answer your question from the comments below:
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
sentence: I (space) l o v e (space) l e a r n i n g software development
There are a total of 36 characters in the sentence. (If I counted correctly)
index values start at 0 and end at 35
length is 36. But remember the last index number is 35, and the first is 0.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, theindexis the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.
– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (iin your code) is a location, kind of like counting. But unlike "normal life", it starts at0instead of1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.
– SherylHohman
Mar 23 at 6:22
add a comment |
Your question is not entirely clear.
Do you want to print the index of every character that does not correspond to as space ("")?
If so, below would do that.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
In the above code, I am printing the index, i, as long as the value at that index (i) is not a space character. Your code sentence[i] tells it to print the value at the index (i), which would be the character at that location in the string.
On the other hand if you want the Ascii value at index i, you need to use the charAt function: sentence.charAt[i].
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
As another comment suggested, "...instead of all the integer that would correspond to the length" is unclear.
There is only 1 length for any given string. This is also true for any array at in any particular point in time.
Perhaps you mean index?
Each character has a unique index in any particular string.
To answer your question from the comments below:
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
sentence: I (space) l o v e (space) l e a r n i n g software development
There are a total of 36 characters in the sentence. (If I counted correctly)
index values start at 0 and end at 35
length is 36. But remember the last index number is 35, and the first is 0.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);Your question is not entirely clear.
Do you want to print the index of every character that does not correspond to as space ("")?
If so, below would do that.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
In the above code, I am printing the index, i, as long as the value at that index (i) is not a space character. Your code sentence[i] tells it to print the value at the index (i), which would be the character at that location in the string.
On the other hand if you want the Ascii value at index i, you need to use the charAt function: sentence.charAt[i].
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
As another comment suggested, "...instead of all the integer that would correspond to the length" is unclear.
There is only 1 length for any given string. This is also true for any array at in any particular point in time.
Perhaps you mean index?
Each character has a unique index in any particular string.
To answer your question from the comments below:
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
sentence: I (space) l o v e (space) l e a r n i n g software development
There are a total of 36 characters in the sentence. (If I counted correctly)
index values start at 0 and end at 35
length is 36. But remember the last index number is 35, and the first is 0.
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(i);
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
if (sentence[i] !== " ")
console.log(sentence.charCodeAt(i));
var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);var sentence = "I love learning software development";
for (var i=0; i < sentence.length; i++)
console.log("index:", i, "character:", sentence.charAt(i));
console.log ("nlength: ", sentence.length, "index 0 - ", sentence.length-1);edited Mar 23 at 6:04
answered Mar 23 at 5:10
SherylHohmanSherylHohman
5,81374052
5,81374052
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, theindexis the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.
– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (iin your code) is a location, kind of like counting. But unlike "normal life", it starts at0instead of1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.
– SherylHohman
Mar 23 at 6:22
add a comment |
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, theindexis the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.
– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (iin your code) is a location, kind of like counting. But unlike "normal life", it starts at0instead of1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.
– SherylHohman
Mar 23 at 6:22
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
1=a, 2=b, 3=c... Is this the index you are talking about? Sorry I am very new.
– TuCraiN
Mar 23 at 5:33
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, the
index is the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.– SherylHohman
Mar 23 at 5:39
@TuCraiN Index is the location in the string. If you write 0 above the first letter in the string, then the 2nd letter in the string is 2, etc, count each letter in the string, the
index is the number, or location of each letter in the string. Arrays and Strings in JS (and most languages start at 0, not 1). So in your loop, i is the index. You are looping through every "letter location" in the string, and printing out the letter at that index. Unless the "letter" is a space, because your 'if` statement tells JS to skip over spaces. The question is, what do you want it to print.– SherylHohman
Mar 23 at 5:39
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
You might need to read up on strings and arrays. We cannot give a complete tutorial and explanation of the JavaScript language on this site. Sorry, this is just not the proper format, or purpose, of this particular site. Hopefully this can point you in the right direction. When you have a little better clarity, come back to revisit this question, and perhaps we can help you more.
– SherylHohman
Mar 23 at 5:52
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
w3schools.com/jsref/jsref_charat.asp Also, I'm going to add another snippet that will print the index, and the character at that index of the string. Hopefully that clarifies things.
– SherylHohman
Mar 23 at 5:56
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (
i in your code) is a location, kind of like counting. But unlike "normal life", it starts at 0 instead of 1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.– SherylHohman
Mar 23 at 6:22
index (i) is corresponds to the sequence of letters in the sentence. i= 0th place, the 1st place, the 2nd place, the 3rd place... in the given sentence. An index (
i in your code) is a location, kind of like counting. But unlike "normal life", it starts at 0 instead of 1. There is a such thing as "a" represented by a particular number, "b" always represented by a different number, etc. But In computers, the numbers are very different. They are known as ASCII values, or utf-8 values, etc. You can look those up. If this is what you want, see my 2nd example.– SherylHohman
Mar 23 at 6:22
add a comment |
Perhaps you are experienced in a different language where indexing a character of a string would return a integer.
In JavaScript, you get a human representation of the string. The string is initially encoded in UTF-16, converted into binary, and then stored in memory. However, when you access it, you get the actual string instead of UTF-16 code due to an language-specific automatic conversion.
However, if you are using Node.js you can use a buffer to store the string and deal with binary data directly.
In JavaScript, if you want to get the code you can use codePointAt(index) method:
alert( "z".codePointAt(0) ); // 122
alert( "Z".codePointAt(0) ); // 90
add a comment |
Perhaps you are experienced in a different language where indexing a character of a string would return a integer.
In JavaScript, you get a human representation of the string. The string is initially encoded in UTF-16, converted into binary, and then stored in memory. However, when you access it, you get the actual string instead of UTF-16 code due to an language-specific automatic conversion.
However, if you are using Node.js you can use a buffer to store the string and deal with binary data directly.
In JavaScript, if you want to get the code you can use codePointAt(index) method:
alert( "z".codePointAt(0) ); // 122
alert( "Z".codePointAt(0) ); // 90
add a comment |
Perhaps you are experienced in a different language where indexing a character of a string would return a integer.
In JavaScript, you get a human representation of the string. The string is initially encoded in UTF-16, converted into binary, and then stored in memory. However, when you access it, you get the actual string instead of UTF-16 code due to an language-specific automatic conversion.
However, if you are using Node.js you can use a buffer to store the string and deal with binary data directly.
In JavaScript, if you want to get the code you can use codePointAt(index) method:
alert( "z".codePointAt(0) ); // 122
alert( "Z".codePointAt(0) ); // 90
Perhaps you are experienced in a different language where indexing a character of a string would return a integer.
In JavaScript, you get a human representation of the string. The string is initially encoded in UTF-16, converted into binary, and then stored in memory. However, when you access it, you get the actual string instead of UTF-16 code due to an language-specific automatic conversion.
However, if you are using Node.js you can use a buffer to store the string and deal with binary data directly.
In JavaScript, if you want to get the code you can use codePointAt(index) method:
alert( "z".codePointAt(0) ); // 122
alert( "Z".codePointAt(0) ); // 90
edited Mar 23 at 5:11
Andy Ng
71
71
answered Mar 23 at 4:54
santanu berasantanu bera
435312
435312
add a comment |
add a comment |
3
String is collection of characters
– Pranav C Balan
Mar 23 at 4:31
3
sentence[i]means "extract the character at positioniin the string and create a new string containing only that character"– Pointy
Mar 23 at 4:31
"...instead of all the integer that would correspond to the length" What do you mean? Do you expect:
[0, 1, 2, 3...]?– zer00ne
Mar 23 at 4:50
Please give an example of what you want the program to print. What are you expecting? How does what it does do differ from what you expect, or are trying to make it do?
– SherylHohman
Mar 23 at 5:40