//HTML code part
<section class="playersScore">
<p class="label-score">
Player's Score:
<span id="pScore"> 0 </span>
</p>
</section>
//JS code part
const min = 1;
const max = 10;
let playerPoints = 0;
let computerPoints = 0;
const choices = section.addEventListener('click', function (event){
const x = Math.trunc(Math.random() * (max - min)) + min;
const x = Math.trunc(Math.random() * (max - min)) + min;
if (event.target.id === 'rock' && x > 6) {
console.log('Player WIN!!');
playerPoints++;
document.getElementById('pScore').textContent = playerPoints;
//UPDATE
In this code you can see this part '&& x > 6'. I made the computer this way to make a choice between rock paper scissors.
Try to show you a short code part as you asked me, I hope it's enought.
And thanks for show the problem of screenshots don't make the same mistake again
So I want to make a game, if you collect 3 points you win it. It is almost done I just want to get the players current points what always changeing.This is my html code what contains the value or property, it can be the problem I don't what is it for sure.You can see the changeing when player or computer collect point
I used document.getElementById('').textContent = variable to change the value, but after changeing can't get it again.
Tried to get back it with the same way but doesn't work.
As you see in the image If print it I can see the current number
But if try again with the id selector or span selector or even class selector it shows nothing.
I think you'll be best using a variable to store the score instead of using an DOM element. Then you can create a function to update it, and every time you need to change the score, use the function instead of setting it directly.
Something like this:
let points = 0;
let playerScoreDisplay = document.getElementById('pScore');
function updatePoints(value){
points = value;
playerScoreDisplay.textContent = points;
}
I was wondering about being able to use/make a function like REPT() to display partial repetitions with decimal numbers. My formula, as it stands, works fine for integers. If I wanted to give 3 stars, I just use =REPT(CHAR(9733), 3) and that prints 3 black stars.
Let's say I wanted to give something 4.2 stars. Is there a way to do something like this? I've been trying to figure out a way to do it with App Script, but I'm not sure how to proceed. Everything I've researched online is geared towards making a clickable rating system with HTML/CSS/JavaScript. But I'd be looking more for something like an average rating on Amazon or something.
This is what I have with App Script so far:
function starRating() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('test');
var cell = sheet.getRange(2,1); // Sets a test cell
// Create a concatenated string of 5 blank stars
var blankStars = "";
for (let i = 0; i < 5; i++) {
blankStars = blankStars.concat(String.fromCharCode(9734));
}
// Create a concatenated string of 5 black stars
var blackStars = "";
for (let i = 0; i < 5; i++) {
blackStars = blackStars.concat(String.fromCharCode(9733));
}
cell.setValue(blankStars);
cell.setHorizontalAlignment("center")
cell.setFontSize(18);
var rating = sheet.getRange(1,1).getValue(); // Raw Rating (e.g. 4.3)
var amount = Math.max(0, (Math.min(5, rating))) * 20; // Gets percent out of 100, also ensures rating is from 0-5
/*
Maybe find a way to overlay the blackStar on top of blankStar?
Use amount as a way of only showing percent of blackStar?
*/
}
As I put in the comment, my thought was to overlay a percent of the blackStar string on top of the blankStar string, but 1. I don't know how to do that and 2. I don't know how that could be put into the cell.
This is fairly close and a lot easier
function stars(n = 9) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const style = SpreadsheetApp.newTextStyle().setForegroundColor("#000000").setFontSize(16).build();
let s = "";
Array.from(new Array(10).keys()).forEach(x => s = s.concat(String.fromCharCode(x < n ? 9733 : 9734)));
sh.getRange(3, 1).setValue(s).setTextStyle(style);
}
For this question, it might be a little vague, because i just dont understand it at all, its probably the wording.. from what i learn in class it seems a lot harder. So im lost as to where to begin.. if someone can help walk me through it easier i would appreciate it!
Question: Design a Program that will read the same parts inventory file described in the problem 6. the parts are: (Record code, part number, part description, and inventory balance) validate the record code and part number on each record, and print the details of all valid records whose part numbers fall within the value AA3000 and AA3999 inclusive. Also print a count of these selected records at the end of the parts listing.
Now, i hope you can understand what its asking because i sure dont. Any help or a small walk through would be awesome. This is the code i am supposed to start out from that was given to me.
var Rec_Code = new Array(11,11,11,12,11,12,11,13,11,14);
var Numer = new Array(2000,3000,3003,3008,3999,2000,1090,3678,3777,3543);
var Alpha = new Array("AA","AA","AX","AA","AA","AA","AB","AA","AN","AA");
var Desc = new Array("X","L","S","F","R","U","T","N","Q","Y");
var Inv_Bal = new Array(12,13,14,23,34,56,32,45,67,77);
also, this was given to me, which is basically what i have to do, but dont know how to completely do it.
use the vars that I provided to create 5 parallel arrays, RecCode, AlphaPart of part number, Numeric part of the part number,Description and Inventory. You need to search the first 3 arrays for:
RecCode of 11
AlphaCode of 'AA':
Numeric Code betweewn 3000 - 3999 inclusive
when you find a match increment a count and display the Description and Inventory.
Assuming that all arrays are the same length and sorted appropriately, you can loop over one and display the information you need:
var count = 0;
for(var i = 0; i < Rec_Code.length; i++)
{
if(Rec_Code[i] == 11 && Alpha[i] == 'AA' && (Numer[i] >= 3000 && Numer[i] <= 3999))
{
console.log(Desc[i]);
console.log(Inv_Bal[i]);
count++;
}
}
var Rec_Code = new Array(11,11,11,12,11,12,11,13,11,14);
var Numer = new Array(2000,3000,3003,3008,3999,2000,1090,3678,3777,3543);
var Alpha = new Array("AA","AA","AX","AA","AA","AA","AB","AA","AN","AA");
var Desc = new Array("X","L","S","F","R","U","T","N","Q","Y");
var Inv_Bal = new Array(12,13,14,23,34,56,32,45,67,77);
var count = 0;
for(var i = 0; i < Rec_Code.length; i++)
{
if(Rec_Code[i] == 11 && Alpha[i] == 'AA' && (Numer[i] >= 3000 && Numer[i] <= 3999))
{
console.log(Desc[i]);
console.log(Inv_Bal[i]);
count++;
}
}
Allright, I know what machine precision is, but this, I can't understand...
Code:
console.log("meanX",meanX);
meanX2 = meanX * meanX; //squared
console.log("meanX2",meanX2);
Console output:
meanX 300.3
meanX2 28493.4400000000002
In case you are wondering, the correct value for meanX2 would be 90180.09
And this is only one of the many examples visible in the screenshot..
.toFixed(6) seems to fix this... But I have no idea why it doesn't work without it.
Edit
Ok, I don't want to post the whole program code here because in first place I'm not the only author, and second, I also wouldn't like this to be copied without our permission. But I'll gladly explain how I get this error and will post the whole method/function code here.
This code belongs, as you may have guessed from the window title, to a lane detection algorithm. We use Three.js/webgl to run some pre processing shaders on each frame of a video and then we analyze the resulting image. The method/function you see on the screenshot is a perpendicular line fitting algorithm and is part of the whole thing.
I can see the algorithm running nicely because I have the lane being drawn on top of the video, and It is well placed. Until suddenly the lane turns into an horizontal bar. This unexpected behavior happens exactly because of the phenomenon I described here, since it's from that moment that I start to see wrong math in the console.
Also, because the video and algorithm run at slightly different fps everytime, the problem doesn't always happen in the same moment of the video, and sometimes It doesn't happen at all.
Here is the code (it has some alterations because I was trying to isolate the issue):
this.perpendicularLineFit = function (points, slopeSign) {
var count = points.length;
var sumX = 0,
sumY = 0;
var sumX2 = 0,
sumY2 = 0,
sumXY = 0;
var meanX, meanY;
var i, lowp = {}, highp = {};
var B;
var slope;
var originY;
for (i = 0; i < count; i++) {
sumX += points[i].x;
sumY += points[i].y;
sumX2 += points[i].x * points[i].x;
sumY2 += points[i].y * points[i].y;
sumXY += points[i].y * points[i].x;
}
meanX = sumX / count;
meanY = sumY / count;
//If you uncoment this, problem reappears:
//var numeratorLeft = meanY * meanY;
console.log("meanX",meanX);
var meanX2 = meanX*meanX;
console.log("meanX2",meanX2);
var numerator = (sumY2 - count * (meanY * meanY)) - (sumX2 - count * meanX2);
var denominator = (count * meanX * meanY - sumXY);
B = 0.5 * (numerator / denominator);
slope = -B + slopeSign * Math.sqrt(B * B + 1);
originY = meanY - slope * meanX;
slope = isNaN(slope) ? slopeSign : slope;
originY = isNaN(originY) ? originY : originY;
lowp.y = this.lowY;
lowp.x = (this.lowY - originY) / slope;
highp.y = this.highY;
highp.x = (this.highY - originY) / slope;
return {
low: lowp,
high: highp
};
};
Now, I was trying to understand what was causing this, and the most bizarre thing is that it seems that when I place a statement of this form
var x = ... meanY * meanY ...;
before the meanX2 attribution, the issue happens. Otherwise it doesn't.
Also, I tried to catch this anomaly in the debugger but just when I enter the debugging tab, the problem disapears. And the values turn correct again.
I certainly don't believe in black magic, and I know that you are probably skeptic to this.
I would be too. But here is a link to a video showing it happening:
The video
Edit2:
I managed to reproduce this issue in another computer.. Both having ubuntu and using firefox (versions 20 and 21).
Edit3:
I'm sorry it took so much time! Here is a zip containing the issue. Just run it in any webserver. The code mentioned is in LaneDetection.js. Search for "HERE" in the file to find it.
https://docs.google.com/file/d/0B7y9wWiGlcYnYlo1S2pBelR1cHM/edit?usp=sharing
The problem might not happen in the first attempts. If that's the case refresh the page and try again. When the lines get horizontal you know it's there. As I said, I saw this problem happening in firefox versions 20 and 21 on ubuntu. In chrome it never happened.
By the way, I noticed that changing javascript.options.typeinference flag in firefox seems to stop the problem... I don't know exactly what that flag does, but maybe this optimization is not correctly implemented in firefox?
I can't say for sure that I actually have an answer but I think that I have confirmed that basilikum was correct to suggest a memory problem. Here's what I did: I took the first ten entries from your screenshot and calculated the correct answer. I then converted the correct answer and the wrong answer into the hexidecimal representation of the double-precision float. What I ended up with was the following:
292.416^2 = 85507.506 = 40F4E0381C71C71E
changed to 27583.373 = 40DAEFEB1C71C722
293.166^2 = 85946.694 = 40F4FBAB1C71C72A
changed to 27583.373 = 40DAEFEB1C71C722
295.818^2 = 87508.396 = 40F55D4658DC0876
changed to 28041.024 = 40DB62419637021F
294.500^2 = 86730.250 = 40F52CA400000000
changed to 27583.373 = 40DAEFEB1C71C722
297.000^2 = 88290.000 = 40F58E2000000000
changed to 28041.024 = 40DB62419637021F
221.750^2 = 49173.062 = 40E802A200000000
changed to 24964.000 = 40D8610000000000
300.300^2 = 90180.090 = 40F6044170A3D70A
changed to 28493.440 = 40DBD35C28F5C290
220.200^2 = 48488.040 = 40E7AD0147AE147B
changed to 25408.360 = 40D8D0170A3D70A4
300.600^2 = 90360.360 = 40F60F85C28F5C29
changed to 28493.440 = 40DBD35C28F5C290
213.000^2 = 45369.000 = 40E6272000000000
changed to 28032.326 = 40DB6014E5E0A72E
There's no persistent pattern to the change but there are a couple instances that are very telling of a memory issue. In the first two entries you can see that bytes 1, 2 and 3 were unchanged. In the 9th entry there's something even more odd. It would appear that bytes 0 - 3 were shifted left by exactly 4 bits! Upon considering the statement that the problem doesn't arise until after some time has passed and in light of these two anomalies, I'm fairly confident that you're encountering some sort of memory issue. Could it be, dare I say, a stack overflow?
My current solution does not calculate the added ship cost. My condition isn't working, I've been able to do similar problems with functions but I would like to know where I went wrong here.
<html>
<script type="text/javascript">
var numOrdered = 0;
var priceItem = 0;
var shipCost = 0;
var amtDue = 0;
numOrdered = prompt("Enter the number ordered ",0);
priceItem = prompt("Enter the price of the item ",0);
amtDue = numOrdered * priceItem;
if (amtDue > 1000)
{
shipCost = 0;
}
else
{
shipCost = amtDue * .1;
}
amtDue = amtDue + shipCost;
document.write("You owe ", amtDue);
</script>
</html>
It works for me. I put your code here: http://jsfiddle.net/XVzKb/
No changes -- if I enter 5 and then when prompted again 10 the total displayed in the page is 55 which is 5 * 10 * 1.1 = 55. Note that if your input includes a non-numeric character like $ or , then you'll need to parse it like so:
number = parseInt(response.replace(/[^0-9]/g, ''), 10)
I would check out the developer tools in Chrome or Firefox and use console.log or put in some break points to help you understand what is working and what is not. This way you can see if its your javascript or your html document that is failing.
That being said, you are comparing strings not numbers, so you'll want to use parseInt on the values you are getting back from the prompt.
priceItem = parseInt(priceItem, 10);