var text = "Hue, bla, hue, rhr, aek kmggg mlsd k Bjarni sdkrals fn lol Bjarni\
lelelele Bjarni";
var myName = "Bjarni";
var hits = [];
for(var i = 0; i < text.length; i++); {
if (text[i] === "B"); {
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
}
}
console.log(hits);
This is supposed to return my name, but it just returns an array containing a few undefined values.
The problem is here:
if (text[i] === "B"); {
Get rid of the semicolon. The semicolon in that point of the code makes it like:
if (text[i] === "B")
; // DO NOTHING AT ALL
{ // START A BLOCK
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
}
edit — oh and there's another one (with similar effects) after the for loop header. It should be:
for(var i = 0; i < text.length; i++) {
You've just got a few syntactical errors and a missing break statement to fix. This should work:
var text = "Hue, bla, hue, rhr, aek kmggg mlsd k Bjarni sdkrals fn lol Bjarni\ lelelele Bjarni";
var myName = "Bjarni";
var hits = [];
for(var i = 0; i < text.length; i++) {
if (text[i] === "B") {
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
break;
}
}
console.log(hits);
To clarify; you had a semicolon after the line if( text[i] === "B");, another semicolon after your first for loop and you were missing a break statement, so it would've looped around throughout the entire "text" string, which may or may not have been unintentional behaviour.
Related
I got the code below working, but when I try to add a Browser.msgBox() once there is a duplicate in the comparison, the code keeps running until it exceeds its time limit.
The idea is to notify the user that the item he/she is trying to add is duplicated and have the script stop running.
var duplicate = false;
for(var x = 0; x < data.length; x++) {
for(var j = 0; j < dataArquivoItens.length; j++){
if(data[x].join() == dataArquivoItens[j].join()){
duplicate = true;
break;
}
}
}
Thanks a lot!
You are only breaking out from the if statement, this is why your code keeps iterating
If you want to break from all nested loops/ statements - give them a name
Sample:
var duplicate = false;
loop1:
for(var x = 0; x < data.length; x++) {
loop2:
for(var j = 0; j < dataArquivoItens.length; j++){
if(data[x].join() == dataArquivoItens[j].join()){
duplicate = true;
Browser.msgBox("That's a duplicate");
break loop1;
}
}
}
I have a simple question although i cannot manage to resolve this problem. Hope you can help. I need to make triangle using for loop and from this 4 exercises I don't know what to do with the third one. I haven't used Javascript before, so any help would be appreciated.
# # # # #
# # # #
# # # <----- here is triangle i need to make. Just in case
# #
#
var i;
var j;
for (i = 0; i <= 5; i++ )
{
document.write("</br>");
for ( j = 0; j < 6-i; j++ )
{
document.write( "  " );
}
for ( j = 6-i; j <= 5; j++ )
{
document.write( "*" );
}
}
This is code I wrote for D in photo.
And I'm sorry i did not add it at first.
for (let line = "*"; line.length < 8; line += "*")
console.log(line);
this question came in this book: http://eloquentjavascript.net
I don't know why there are so bad answers on google for this one.
function leftTriangle(rows){
let result = '';
for(let i=rows;i>0;i--){
if(i===rows) {
result += '*'.repeat(i) + '\n';
}else{
let empty = rows-i
result+= ' '.repeat(empty) + '*'.repeat(i)+ '\n'
}
}
return result;
}
console.log(leftTriangle(5))
I'm sure there are better solutions (simply left-padding with spaces comes to mind), but here's the quick and dirty one I created from your own solution.
for (var i = 0; i < 5; i++) {
for (var j = 0; j < i; j++) {
document.write(" ");
}
for (var j = 5; j > i; j--) {
document.write("#");
if (j > i + 1) document.write(" ");
}
document.write('<br/>')
}
https://js.do/code/diamondsinthesky
Something like this?
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
Thank you for your help. I did it. It was too obvious but somehow I couldn't find it. Thank you one more time. Here is how i did it.
for (i = 5; i > 0; i--) {
document.write("</br>");
for (j = 0; j < 6 - i; j++) {
document.write("  ");
}
for (j = 6 - i; j <= 5; j++) {
document.write("*");
}
}
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
You can also do this if you are looking for something different.
This code is for a triangle of 7 lines.
let size = 8;
let y = "#";
for (let x = 0; x < size; x++)
{
console.log(y);
y += "#";
}
// Second method
for (let i = 1; i < size;i++)
{
let me ="#".repeat(`${i}`)
console.log(me);
}
var size = 5;
for (var i = 0; i < size; i++) {
for (var j = 0; j <= i; j++) {
document.write("*");
}
document.write("<br />\n");
}
This is from Codecademy's Javascript lesson "Search Text For Your Name". The following works:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j])
}
}
}
However, when I replace i + myName.length with j + myName.length, it's crashing. In full:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < j + myName.length; j++) {
hits.push(text[j])
}
}
}
I'm not getting any errors when I run this, which led me to believe that it's just stuck in an infinite loop, except that when I place a console.log marker within the For loop in question, it doesn't print anything.
What's the reason for it crashing?
j < j + myName.length; j++
j never reaches the end. You're incrementing it, but you compare it against number that is always larger than itself (assuming myName.length is > 0). The conditions for the loop is always satisfied, causing it to run forever.
It crashes because it's an infinite loop.
Here is your second example, with the static variables converted to their values:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for (i = 0; i < 42; i++) {
if (text[i] === 'Z') {
for (var j = i; j < j + 5; j++) {
hits.push(text[j]);
}
}
}
Specifically, your inner for condition is causing the infinite loop:
for (var j = i; j < j + myName.length; j++) {
or with myName.length replaced by its value, 5:
for (var j = i; j < j + 5; j++) {
j will always be less than j + 5 so the loop continues without end, consuming memory until crash.
I am unable to push any elements into an array using .push(). The code here is my attempt:
text = "fihdfhdkfhkdsfkjd";
var myName = "Anthony Pham";
var hits = [];
for(var i = 0; i < text.length; i+++) {
if (text[i] === myName[i]) {
for(var j = i; j <= (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
I am using CodeAcedmy and is givng me an error that my second for loop is unable to push any values into hits. I have tried switching between myName[j] and text[j] in the hits.push() but still cannot make the program work right. What is wrong with my second for loop here?
Try this ..
for(var i = 0; i < text.length; i++) {
if (text[i] === myName[i]) {
for(var j = i; j <= (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
Aside from the extra + in i+++, you have a logic problem. The only time text[i] === myName[i] would be true is when i equals 9 (the h in Pham). Then you have this loop:
for (var j = i; j <= (myName.length + i); j++) {
// j = i = 9
// j <= 12 + 9 = 21
hits.push(text[j]);
// hits = [h, a, m, undefined, undefined, undefined, ... , undefined ]
}
use charAt(), not array brackets.
Also, you're never checking the bounds on myName, and are reading past the end in both places it's referenced.
When I call this function, sending for example: abc as the parameter,
the function returns: undefinedcba. I can't figure out why it's adding
'undefined' to my returned value. I'm probably overlooking something obvious
but I can't spot it. Thank you.
function FirstReverse(str) {
var str_arr1 = new Array();
var ans = '';
for(i=0; i < str.length; i++) {
str_arr1.push(str.charAt(i));
}
for(j=str.length; j >= 0; j--) {
ans += str_arr1[j];
}
return ans;
}
Strings are 0-indexed. str[str.length] does not exist.
j needs to start at str.length - 1.
Or, just return str_arr1.join();
The index of the string starts at 0, so string.length is always one number bigger than index of the last character in the string.
In the second for loop, use
for(var j=str.length -1; j >= 0; j--) {
The error is in the second for statement. See the solution:
function FirstReverse(str) {
var str_arr1 = new Array();
var ans = '';
for(i=0; i < str.length; i++) {
str_arr1.push(str.charAt(i));
}
for(j=str.length-1; j >= 0; j--) {
ans += str_arr1[j];
}
return ans;
}
Because when you pass 'abc' there are only 3 characters in it.
So arrray str_arr have elements at index 0, 1 and 2.
But you are looping for str.length i.e. for 3 times and str_arr[3] is not defined.
You should do this,
function FirstReverse(str) {
var str_arr1 = new Array();
var ans = '';
for(i=0; i < str.length; i++) {
str_arr1.push(str.charAt(i));
}
for(j=str.length-1; j >= 0; j--) {
ans += str_arr1[j];
}
return ans;
}
Looks like you want to reverse a string, which you can do in this javascript one liner
function reverse(s){
return s.split("").reverse().join("");
}
The reason you are getting an undefined is because your j starts with str.length, whereas it should be str.length-1. str_arr1[str.length] is out of bounds and therefore will be undefined.