I am trying to the splice method but it is not working properly want to delete one user but it is deleting 2.
Also want to add the binary search button
this is the requirement for binary search
Replace the sequential access algorithm to operate on the arrays with a binary search algorithm.
To implement this, the array’s data must be sorted. You may refer to an online example available at Array.prototype.sort() to learn about sorting data in arrays.
function Display_Athlete() {
var text = "<hr/>";
for (let i = 0; i < athlete_name.length; i++) {
text += "Athlete No - " + (i + 1) + ", Athlete Name is " + athlete_name[i]
+ " and height is " + athlete_height[i] + "<br>";
}
document.getElementById("message").innerHTML = text;
}
//Created function to remove the user
function Remove_Athlete() {
var person = prompt("Enter the name to remove the Athlete");
for (let i = 0; i < athlete_name.length; i++) {
athlete_name.splice(person - 1, 1);
athlete_height.splice(person - 1, 1);
alert(" Athlete_name " + athlete_name + " Athlete_height " + athlete_height + " is removed ");
}
}
//Create the function to find the user
function Find_Athlete() {
var person = prompt("Enter the name to Find the Athlete");
var text = "";
for (let i = 0; i < athlete_name.length; i++) {
if (athlete_name[i] == person) {
text += "Athlete No - " + (i + 1) + ", Athlete Name is " + athlete_name[i]
+ " and height is " + athlete_height[i] + "<br>";
}
}
document.getElementById("message").innerHTML = text;
if (text == "")
alert(`${person} Invalid Athlete name`);
return "";
}
function Binary_Search(){
}
I'd like to write a JavaScript program that displays a symmetric histogram like this image:
The program should ask the user to enter the number of bars to print and the character to use to draw the bars. The count of characters in the bar needs to be displayed on the right-side of each bar.
The example showed is when I entered # as the character and 13 as the number.
Here's my code:
var symbol = prompt("Enter the symbol");
var number = prompt("Enter the number");
var currentNum = 1;
let text = "";
let symbolNum = symbol;
while (currentNum <= number) {
text += "<br>" + symbolNum + " " + currentNum;
symbolNum += symbol;
currentNum++;
}
document.write(text + "<br>");
And at last, I only can output the following:
I'd like to know what I can do in order to reverse the loop?
Try this
function SymmetricHistogram(){
const size = 10;
let numberX = 0;
let numberY = 0;
for(let i = size; i>=-size; i--) {
for(let j=size; j>=Math.abs(i); j--){
process.stdout.write("#");
}
numberX <=size ? console.log(numberX++) : console.log(--numberY);
}
}
SymmetricHistogram();
Or try the below
https://onecompiler.com/javascript/3x58bqr3h
Two different way for the same result. Not realy clean, but work.
var symbol = prompt("Enter the symbol");
var number = prompt("Enter the number");
var currentNum = 1;
let textTOP = "";
let textBOTTOM = "";
let symbolNum = symbol;
while (currentNum <= number) {
textTOP += "<br>" + symbolNum + " " + currentNum;
if (currentNum < number)
textBOTTOM = "<br>" + symbolNum + " " + currentNum + textBOTTOM;
symbolNum += symbol;
currentNum++;
}
document.write(textTOP + textBOTTOM + "<br>");
var symbol = '#';
var number = 13;
var currentNum = 1;
let text = "";
while (currentNum < number * 2) {
if (currentNum <= number) {
let num = currentNum;
text += "<br>" + symbol.repeat(num) + " " + num;
} else {
let num = Math.abs(number * 2 - currentNum);
text += "<br>" + symbol.repeat(num) + " " + num;
}
currentNum++;
}
document.write(text + "<br>");
ok so I understand this is a very basic JS logic but I am trying to replace any document.write() with .innerHTML and I tried it with the code below
function writeTimesTable(num) {
for(let i = 1; i < 10; i++ ) {
let writeString = i + " * " + num + " = ";
writeString = writeString + (i * num);
writeString = writeString + "<br />";
document.write(writeString);
}
}
function newTable() {
for(let i = 1; i <= 10; i++ ) {
let para = document.getElementById("paragraph");
para.innerHTML = writeTimesTable(i)
}
}
I have a div element with the ID of paragraph already. I keep getting undefined when I look at the div#paragraph and the rest of my code outputs under my script tag but not in the div element. What am I doing wrong?
Your function writeTimesTable() needs to return a value. Your writeString string, needs to be concatenated as well, you can do that with += like seen below:
function writeTimesTable(num) {
let writeString = "";
for(let i = 1; i < 10; i++ ) {
writeString += i + " * " + num + " = ";
writeString += writeString + (i * num);
writeString += writeString + "<br />";
}
return writeString
}
Using para.innerHTML = writeTimesTable(i) probably isn't intended, as it will just display the last loop, so you might also want to use += here as well:
para.innerHTML += writeTimesTable(i)
You normally want to avoid document.write because it literally writes out to the document.
I have also taken the liberty of doing the DOM creation off-page during the loop, and just add it to the actual DOM at the end. This means you aren't constantly re-drawing the page while you loop.
This is better than changing your innerHTML = to innerHTML +=, which you would need to do if you wanted to avoid overwriting each previous iteration of the loop.
function writeTimesTable(num) {
for(let i = 1; i < 10; i++ ) {
let writeString = i + " * " + num + " = ";
writeString = writeString + (i * num);
writeString = writeString + "<br />";
return writeString;
}
}
function newTable() {
const inner = document.createElement('div');
for(let i = 1; i <= 10; i++ ) {
const item = document.createElement('div');
item.innerHTML = writeTimesTable(i);
inner.appendChild(item);
}
let para = document.getElementById("paragraph");
para.appendChild(inner);
}
newTable();
<div id="paragraph"></div>
Your newTable() function with the 10 loops is useless. You're doing 10 times the same stuff over a single DOM element.
Don't use document.write...
I'd do it like:
function newTable( num ) {
var HTML = "";
for (var i=1; i <= num; i++) {
HTML += i +" * "+ num +" = "+ (i*num) +"<br>";
}
return HTML; // Return the concatenated HTML
}
document.getElementById("paragraph").innerHTML = newTable(10);
<p id="paragraph">asdasdasd</p>
Or in a super uselessly cryptic ES6 way:
const newTable = n =>
Array(n).fill().map((_,i) =>
`${i+=1} * ${n} = ${i*n}<br>`
).join('');
document.getElementById("paragraph").innerHTML = newTable(10);
<p id="paragraph">asdasdasd</p>
I am new to jQuery and I cant seem to get the following code working..
for ( var i = 0; i < 2; i++ ) {
$status[i] = $('select[name="status'+ i +'"] option:selected').val();
$odd_a[i] = $("input:text[name='odd_a"+ 1 +"']").val();
$odd_b[i] = $("input:text[name='odd_b"+ 1 +"']").val();
$term[i] = $("select[name='term"+ 1 +"'] option:selected").val();
$dh_place[i] = $("input:text[name='dh_place"+ 1 +"']").val();
$dh_total[i] = $("input:text[name='dh_total"+ 1 +"']").val();
}
I have several text boxes "status1, status2, status3 etc. I need to call their name by the for loop. If I replace the "i" with the "1" it works. I cant seem to call the variable "i" at that position.
Try with
$status[i] = $('select[name="status'+ i +'"]').val();
and You need to start i value from 1 like
for ( var i = 1; i < 2; i++ ) {
One problem I can see is the i starts with 0 where as your input starts with 1, so the first loop will not return any elements.
for (var i = 0; i < 2; i++) {
$status[i] = $('select[name="status' + (i + 1) + '"]').val();
$odd_a[i] = $("input:text[name='odd_a" + (i + 1) + "']").val();
$odd_b[i] = $("input:text[name='odd_b" + (i + 1) + "']").val();
$term[i] = $("select[name='term" + (i + 1) + "']").val();
$dh_place[i] = $("input:text[name='dh_place" + (i + 1) + "']").val();
$dh_total[i] = $("input:text[name='dh_total" + (i + 1) + "']").val();
}
I have a string that I have created where I would like to remove the last comma and replace it with a period. I want to keep all of the other commas. Here I was trying to use a conditional statement, it works to add commas, but it doesn't work to replace the last one with a period. I am new at this, I would really appreciate any help.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
displayResult();
}
You seem to be doing it a hard way! Use the Arry's join() method to build the list.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " is a " + petObj.type + " she likes " +
petObj.likes.join(", ") + ".";
displayResult();
}
This will do it:
str.replace(/,([^,]*)$/,".$1")
The regular expression matches on a comma followed by any number of non-commas all the way to the end of the string... by definition, this is the last comma. It works if there are no commas, one comma or any number of commas.
Notice that the 'if' part of this has to always be true so the 'else' part is never executed:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
The for loop says to only continue if that condition is true and your code then tests the same condition.
You might just do something like this:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length-1) {
likesString = likesString + petObj.likes[j] + ", ";
}
else {
likesString = likesString + petObj.likes[j] + ". ";
}
}
There are better ways to do the condition that don't duplicate so much but that might do what you want. (Also I fixed the extra 'var' part.)
Here is a great place to learn and test regular expressions: RegExr
Essentially, you want to replace the following:
RegExp Pattern: /,([^,]+)$/
Replace Pattern: .$1
So your code should look like:
s.replace(/,([^,]+)$/, '.$1');
Where s is the string you're trying to replace the last comma in.
Don't forget to set that line to a variable to save it.
To replace the last occurrence of a comma in a string with a period, you can use:
var index = str.lastIndexOf(",");
var newstr = str.substring(0, index) + "." + str.substring(index + 1);
After looking at your code, it seems epascarello's approach is the best. In addition to what he has pointed out, if there are no elements in the array, your string will somewhat abruptly end with: "she likes". To fix this, you could use:
likesString = petObj.name + " is a " + petObj.type + (petObj.likes.length ? ", she likes " + petObj.likes.join(", ") : "") + ".";
For start you can avoid to put the last comma with a code like this:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type;
if (petObj.likes.length) {
likesString = likesString + " she " + " likes " petObj.likes.join(", ");
}
likesString = likesString + '.'
displayResult();
}
If you still want to use your code you have to test versus lenght-1 as the likes array indexes go from 0 to length-1:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
likesString = likesString + petObj.likes[j] + ((j == petObj.likes.length-1)?'.':', ';
}
displayResult();
}
If you still want to change the string after its construction you can just remove the last two characters
likesString=likesString.replace(/, $/,'.')
That is because replace() returns a new string with the new value instead of changing the string itself, you could do this instead:
likesString = likesString.replace(/,\s$/, ".");