This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
Here is what we're trying to do:
$(document).ready(function()
{
for (var i = 0; i < 10; i++)
{
for (var j = 0; j < 15; j++)
{
$('#Row'+(i+1)+'Sub' + (j+1) + '_ClientDropDown').ready(function()
{ TrimServices_Function( "Row" + (i+1) + "_Sub" + (j+1).ToString() + "_ClientDropDown"); })
}
}
})
Unfortunately, Javascript doesn't really pass the i and j into the second function correctly. When doing alerts in other tests it creates a line with the right initial call, but then the other part just returns 10 and 15. So lets say line 5 sub 7 would be:
$('#Row5Sub7_ClientDropDown').ready(function()
{ TrimServices_Function( "Row11_Sub16_ClientDropDown"); })
How would we go about making sure that we loop through lines where the row and sub match up?
Set both strings outside of the function inside the second for loop like this (I would also clean up your string concatenation, but I left it as you had it here):
for (var i = 0; i < 10; i++)
{
for (var j = 0; j < 15; j++)
{
var selectorA = '#Row'+(i+1)+'Sub' + (j+1) + '_ClientDropDown';
var selectorB = "Row" + (i+1) + "_Sub" + (j+1).ToString() + "_ClientDropDown";
$(selectorA).ready(function()
{ TrimServices_Function(selectorB); })
}
}
Related
I am trying to write a JavaScript to remove string in OBX 5.1 after "\."
Here is the inbound OBX segment:
OBX|2|NM|WBC^White Blood Cell Count^WinPath||3.2\\.br\\This result could indicate your patient might have\\.br\\sepsis. Take into consideration the absolute\\.br\\neutrophil and lymphocyte counts when making your|x 10^9/l|4 - 10|L|||F|||||
Here is the expected outbound OBX segment:
OBX|2|NM|WBC^White Blood Cell Count^WinPath||3.2|x 10^9/l|4 - 10|L|||F|||||
I have written this Javascript code. It is compiling but not removing the unwanted text.
Here is what I have written:
var RegExp_pattern = "\\.";
function indexOf(stringToTrim) {
return stringToTrim.indexOf(RegExp_pattern);
}
function substring(ssstringToTrim) {
return ssstringToTrim.substring(indexOf(OBX_TestValue), -1);
}
/* Single input message case */
var next = output.append(input[0]);
// loop through Order Group (OBR) & Result Group (OBX)
//
var cntObs = next.getRepeatCount("ObservationMessage");
for (var i = 0; i < cntObs; i++) {
var cntOrders = next.getRepeatCount("ObservationMessage[" + i + "]/Order");
for (var j = 0; j < cntOrders; j++) {
var cntResults = next.getRepeatCount("ObservationMessage[" + i + "]/Order[" + j + "]/Results");
for (var k = 0; k < cntResults; k++) {
var OBX_TestValue = next.getField("ObservationMessage[" + i + "]/Order[" + j + "]/Results[" + k + "]/OBX/ObservationValue");
if (OBX_TestValue.indexOf(OBX_TestValue) > 0) {
OBX_TestValue = substring(OBX_TestValue);
}
}
}
}
To remove everything from the first occurance of "\." until the end of the string you should use a regular expression.
var str = "OBX|2|NM|WBC^White Blood Cell Count^WinPath||3.2\\.br\\This result could indicate your patient might have\\.br\\sepsis. Take into consideration the absolute\\.br\\neutrophil and lymphocyte counts when making your|x 10^9/l|4 - 10|L|||F|||||";
var mtch = str.replace(/\\\..*/, '');
console.log(mtch);
This question already has answers here:
How to print a half pyramid in javascript
(9 answers)
How to print star pattern in JavaScript in a very simple manner? [closed]
(22 answers)
Closed 1 year ago.
im trying to write a code that outputs lines of '#' signs that increase by one for each value until the input number is reached.
for example, when triangles(3) is called my desired output is:
'#'
'##'
'###'
ive learned how to create this sequence with number values instead of the '#' signs but not sure how to achieve this output displayed above. thanks!
function triangles(num) {
let number = '';
for (let i = 1; i <= num; i++) {
console.log(number += i);
}
}
triangles(5)
this outputs:
'1'
'12'
'123'
'1234'
'12345'
You can use String.prototype.repeat() to repeat the pound (#) character as specified by the current value of i, without needing to declare a new variable to store it:
function triangles(num) {
for (let i = 1; i <= num; i++) {
console.log('#'.repeat(i));
}
}
triangles(5);
function triangles(num) {
let symbol = "#";
for (let i = 1; i <= num; i++) {
console.log(Array(i).fill(symbol).join(""));
}
}
triangles(5);
function triangles(num) {
const c = "#";
for (let i = 1; i <= num; i++) {
console.log(c.repeat(i));
}
}
triangles(5)
You don't have to use console.log to print number += i specifically. If you want to print '#' multiple times, you can use #Terry's answer (which is shorter) or you can use another for loop in your code (this approach could be simpler for beginners).
function triangles(num) {
let number = '';
for (let i = 1; i <= num; i++) {
number++;
let poundkeys = '';
// dont use i again, name it something else (like j or k)
for (let j = 1; j <= number; j++) {
// just like you do number += i, add a pound symbol to poundkeys
poundkeys += '#';
}
// now, log poundkeys
console.log(poundkeys);
}
}
triangles(5);
Also, a quick tip. Generally, it is better not to log number += i. Instead of:
console.log(number += i);
It is encouraged and more readable if you separate them:
number += i;
console.log(number);
This question already has answers here:
How to display pyramid using JavaScript?
(34 answers)
Closed 4 years ago.
I wrote this method that draw a Triangle. For example if you call drawTriangle(5) you get the following output:
this is my code :-
function drawTriangle(t){
for (let i=1; i <= t; i++)
{
for (let j=1; j<=i; j++)
{
console.log(j+" ");
}
console.log("\n");
}
}
let t = 5 ;
drawTriangle(t);
and i get this output
I can not make them in a line I don't Know where is the problem .
console.log() will print a new line every time, store each line in a variable and print it at the end of innerLoop
function drawTriangle(t){
for (let i=1; i <= t; i++)
{
let eachLine = ''
for (let j=1; j<=i; j++)
{
eachLine += j + " "
}
eachLine = eachLine.trim();
console.log(eachLine);
}
}
let t = 5 ;
drawTriangle(t);
Refer to this. You can use string concatenation in your inner for loop.
Well other answer has already specified what you missed in your code.
I am adding it in case you want to see a alternate solution
let n = 1;
let op = ''
while(n<=5){
op+=new Array(n).fill(0).map( (e,i) => i+1 ).join(' ') + '\n'
n++
}
console.log(op)
Here's a solution with linear time complexity--just console.log(draw(n)), where n is an integer, to see the result:
function draw(n) {
let triangle = '';
let prev;
for (let i = 1; i <= n; i++) {
if (prev) {
triangle += '\n';
prev = prev + ' ' + i;
} else {
prev = i;
}
triangle += prev;
}
return triangle;
}
I have this array of objects here that I am traversing and want to display a match if the person at the current index has an age within +/- 10 years of anyone else in the array. However, when I run it, it says "Cannot read property 'age' of undefined." Where did I go wrong?
function findmatches() {
var n = USERS.length;
for (var i = 0; i < n; i++) {
var currName = USERS[i].firstName;
var currAge = USERS[i].age;
var currGender = USERS[i].gender;
for (var c = 0; c < 10; c++) {
if (((USERS[c].age) + 10) <= currAge) {
document.getElementById("showmatches").innerHTML += currName + " matched to >> " + USERS[i].firstName + " " + USERS[i].lastName + " \n";
break;
}
}
}
}
What exactly is your second for loop supposed to do?
In the code you posted, it iterates through first 10 users in the USERS array. I assume it has less users than that, so at some point USERS[c] is undefined, and you're trying to access USERS[c].age.
I have this problem, to repeat 30 asterisks for 3 lines. I made this example code but it repeats 30 numbers (1..30) from 1 number for first line, up to 30 numbers for the last line. So, I'd need the code to repeat 30 asterisks, for 3 lines each but not quite like within this code.
Sorry for bad elaboration.
var text = "";
var max = 30;
for(i = 0; i < max; i++)
{
for(j = 0; j <= i; j++)
{
text += (j+1)+" ";
}
text += "<br />";
}
A more re-usable solution will be to make a generic repeatString function that simply makes multiple copies of any string.
function repeatString(s, times) {
for (var i = 0, r = ''; i < times; i++) {
r += s;
}
return r;
}
var line = repeatString('*', 30) + '<br />',
content = repeatString(line, 3);
http://jsfiddle.net/611y2vmz/1/
Repeat the loop three times, like this:
for ( var i = 0; i < 3; i++ ) { // this is the line loop
for ( var j = 0; j < 30; j++ ) { //this is the asterix loop
document.write('*');
}
document.write('<br>');
}
Here's a simple demo
If you are using ES2015 (ES6) syntax you can leverage repeat function and string templating. Using those features your code will look like this
let text = (`${'*'.repeat(30)}<br/>`).repeat(3);
Here is an example of ES2015 (ES6) code
if you are using ES5 then you can do this way:
String.prototype.repeat = function(count) {
return count < 1 ? '' : new Array(count + 1).join(this);
};
var text = ('*'.repeat(30) + '<br/>').repeat(3);
Here is an example of ES5 code
You need your outer loop to iterate 3 times, and your inner loop to iterate 30 times. Each iteration of your inner loop should add an asterisk (instead of adding j+1 like you are doing now). This will produce 3 rows of 30 asterisks.
var TEXT = "*";
var LINE_SEPARATOR = "<br/>";
var TEXT_COUNT = 30;
var LINE_COUNT = 3;
var output = "";
for (line = 1; line <= LINE_COUNT; ++line) {
for (text = 1; text <= TEXT_COUNT; ++text) {
output += TEXT;
}
output += LINE_SEPARATOR;
}
document.write(output);
An alternative would be to use recursion:
function stars(num) {
return num > 0 ? stars(num - 1) + '*' : '';
}
var content = stars(30) + '<br/>' + stars(30) + '<br/>' + stars(30);
DEMO