hopefully this is a silly mistake but I am trying to write text within a rect in p5.js. My method was to convert the text to a character array and count the number of characters I am printing before increasing a y-offset to step down a line. Here is the example code:
let splitQuote = tweetsTable.getRow(row).get("Quote").split('');
let size = splitQuote.length;
let rtnQuoteLines;
let yOff =0;
for (i=0;i<size;i++) {
rtnQuoteLines += splitQuote.shift();
if (i % 44 == 0 && i > 0) {
let rtn = rtnQuoteLines.toString();
text(rtn, 1100,yOff);
console.log("rtn: "+rtn);
yOff += 250;
rtnQuoteLines = "";
} else if (splitQuote.length < 44) {
let rtn = splitQuote.toString();
console.log("rtn2: "+rtn);
text(rtn, 1100,yOff);
break;
}
}
The console output makes me think I'm making some mistake too because the end of the quote remains an array and the start of the quote has 'undefined' before the first character.
Can anyone spot my mistake or suggest a neater way of doing this?
Thanks in advance for your help
First off, to stop the undefined at the begin, set let rtnQuoteLines = ""; When it is initally printed it will have a value to print.
You are getting all of the commas seen in the last part of your console log because when convert an array to a string it will automatically seperate eact element with commas. To make avoid this use let rtn = splitQuote.join(""); instead.
Hope this helps! :D
I am making a reservation system. I want to program this so that if the user clicks on button with class A, their total price gets increased by 7.50. Same for button with class B, class C etc.
I tried coding it like this:
var price = 0;
if (button.className === "A")
{
price + 7.50;
}
if (button.className === "B")
{
price + 11;
}
if (button.className === "C")
{
price + 13;
}
But the only output I get is 0!
I'm probably making a rookie mistake, but can anyone help me out?
You want +=
price += 13;
This is equiv to:
price = price + 13;
Calling price + 13 will do nothing, since your never re-assigning the result back to price.
There are a couple things I'm hoping you would learn here:
You can write an alert(button.className); at the top of your code to tell you the class name of the button.
Then, as one of the guys pointed out, use else if. For example, if it passes the first condition, it won't have to go through the other two. Here's an example:
var dayToday = 'Monday';
if (dayToday === 'Monday') {
doThis();
}
else if (dayToday === 'Tuesday') {
doThat();
}
// Since today is Monday, it will pass the first "if condition"
// It won't have to check the 2nd condition
And the price + 7.50 just calculates and returns the resulting value, but it doesn't store that value anywhere. What you want to do is store it back to price by using price = price + 7.50 (or price += 7.50)
I'm trying to generate a random number between 1-75, then compare it to an array so that I can later use the result as an argument for an if / else statement; (i.e., if the number is in the array, do this, otherwise do that.)
Here is the relevant code:
var pickable = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75];
var grid = [7,14,8,10,4,25,18,20,26,27,37,33,40,45,32,48,55,49,52,53,61,63,74,73,67];
function pick()
{
var temp1 = Math.floor(Math.random() * (pickable.length));
var temp2 = temp1.toString();
var check = grid.indexOf(temp2);
alert (check + "\n" + grid);
}
But this code isn't giving me index numbers that make sense; It returns -1 when the number is clearly in the array, returns other numbers for numbers that are not in the array, or returns a number that has no obvious relationship to the position of the number in the array. Why is this not working, and what would work better? Please keep in mind, I'm just learning to code, so detailed explanations are appreciated.
After a bit of experimenting, I've found that what it is doing is picking the random number, writing it to the appropriate places on the screen, then subtracting the number of iterations of the loop before using that number to check the index. (I think.) In other words, the first time I cick the button, it displays 48 on the screen, but searches the index for 47. The next time I click the button it displays 56, but searches for 54, etc. Here is the entire function:
function pick()
{
if (pickable.length > 0)
{
var temp1 = Math.floor(Math.random() * (pickable.length));
var temp2 = temp1.toString();
var check = grid.indexOf(temp2);
alert ("index: " + check + "\nball: " + temp1 + "\n");
document.getElementById("ballNum").innerHTML = pickable[temp1];
pickable[temp1] = pickable[0];
pickable.shift();
picked.push(document.getElementById("ballNum").innerHTML);
document.getElementById("pickedNums").innerHTML = picked.join("| ");
}
else
{
alert("You are out of Bingo balls! \nPlease start over by clicking the " +
" \n\"Populate Board\" button.");
}
}
I am learning Javascript and have run into an issue with the push method. When I use it within a loop it is making my array 33 items instead of just adding 3 to the list. Initial list is 1-10 items long, user defined. I initiated all the variables in the beginning of the script, and the variable items is only manipulated when the user initially tells me how long the array will be. From there it is basic exercises in array methods, and this is the one that is giving me problems. Following is the push part of the code. I appreciate any feedback and will put more code up if anyone feels it is necessary.
for (i = 0 ; i < 3 ; i++){
newfood = prompt("Please enter food " + (i + 1) + ".");
foods.push(newfood);
}
document.write("<ol>");
i = 0; //resetting variable i to 0
for (i = 0 ; i < items + 3 ; i++){
document.write("<li>" + foods[i] + "</li><br>");
}
document.write("</ol>");
Looks like you're running into string concatenation that's then treating the string as a numeric type. Convert what I assume is a string to an int:
for (i = 0 ; i < parseInt(items) + 3 ; i++) {
document.write("<li>" + foods[i] + "</li><br>");
}
I am learning JavaScript through Codecademy, but I have an issue. The code below is supposed to search through the text variable for my name in the myName variable and then push all of the individual letters to the hits array. The code that I have written is not correct but Codecademy says that it is correct and is going to let me move on in the lesson.
I have been trying to solve the issue that I am having with no luck. The problem is that when I run the hits.push(text); line it will output the entire variable but I have tried hits.push(text[i]); and get undefined for the result. Can someone please help me understand where I have made the mistake?
/*jshint multistr:true */
var text = "XsddfasASSFABrandonSFsdfdasBrandonsddfadfaBrandon";
var myName = "Brandon";
var hits = [];
for (i=0; i<=text.length;i++){
if (text[i]===myName[i]){
for(var x=i; x<i+myName.length;x++){
hits.push(text);
}
}
}
if (hits.length===0){
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
The best way I can think to explain your mistake is simply by walking through a bit of the logic of what you have written.
for (i=0; i<=text.length;i++){
Your for loop will iterate i for as many characters as there are in your text variable, so: 49 times.
if (text[i]===myName[i]){
The first run through your for loop, where i=0, you are checking to see if text[0] is strictly equal to myName[0]. text[0] = X and myName[0] = B. The strictly equals condition is not met, so the loop proceeds to increment i repeat: text[1] = s and myName[1] = r. This continues 47 more times, and the condition is never met. myName[i] is undefined after the first 7 loops.
Normally you would do this kind of thing using indexOf, match, search, substr or substring, which are all string methods.
However for the purpose of this exercise you can do:
var text = "XsddfasASSFABrandonSFsdfdasBrandonsddfadfaBrandon";
var myName = "Brandon";
var hits = [],
namePosition = 0;
for (var i = 0; i < text.length; i++) {
if (text[i] === myName[namePosition]) {
hits.push(text[i]);
namePosition ++;
if (hits.length === myName.length) {
break;
}
}
else {
namePosition = 0;
hits = [];
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
(See it working at http://jsfiddle.net/wCWxr/1/). The problems with your original code include:
you try to compare text[i] to myName[i] but the indices of the two strings won't match up.
you try to push the entire string text into hits instead of one character at a time
your logic doesn't deal with the possibility that the beginning but not the end of myName is in text, e.g. if text was aerwerBrasdfsgars
My suggestion fixes this by recording (with namePosition) what position we are currently at within the string myName, and incrementing that when we find a character in text that matches the relevant character in myName. If the characters do not match then it's not a true hit, so we reset hits = [] and namePosition = 0. If the characters all match then hits eventually reaches the length of myName and so we break out of the loop.
If you are trying to find if myName is in text here is what you do:
RegExp:
var pattern = new RegExp(myName);
if (pattern.test(text)){
console.log(myName);
}else {
console.log("Your name wasn't found!");
}
indexOf:
if (text.indexOf(myName) != -1){
console.log(myName);
}else {
console.log("Your name wasn't found!");
}
if (text[i]===myName[i]){
this line should create an error, because myName[i] is not the first letter of myName.
if (text[i]===myName[0]){
Change to this line should work.