I was wondering if someone could help me understand what I am doing wrong? Below I have posted the assignment question, my code, and then the result I am getting.
Assignment
Here is my code:
function parseScores(scoresString) {
// TODO: Compete the function
let inString = scoresString.split();
return inString;
}
function buildDistributionArray(scoresArray) {
// TODO: Compete the function
let x = 0;
let distributeArray = new Array(5);
distributeArray[0] = 0;
distributeArray1 = 0;
distributeArray2 = 0;
distributeArray3 = 0;
distributeArray[4] = 0;
for(i = 0; i < scoresArray.length; i++){
if(scoresArray[i] >= 90){
distributeArray[0]++;
}
else if(scoresArray[i] >= 80 && scoresArray[i] <= 89){
distributeArray[1]++;
}
else if (scoresArray[i] >= 70 && scoresArray[i] <= 79) {
distributeArray[2]++;
}
else if(scoresArray[i] >= 60 && scoresArray[i] <= 69){
distributeArray[3]++;
}
else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
distributeArray[4]++;
}
return distributeArray;
}
}
function setTableContent(userInput) {
// TODO: Compete the function
let myTable = document.getElementById("distributionTable");
if(userInput.length > 0) {
let parsedScores = parseScores(userInput);
let buildArray = buildDistributionArray(parsedScores);
let row = myTable.insertRow(0);
let row2 = myTable.insertRow(1);
let row3 = myTable.insertRow(2);
let cell1 = row2.insertCell(0);
let cell2 = row2.insertCell(1);
let cell3 = row2.insertCell(2);
let cell4 = row2.insertCell(3);
let cell5 = row2.insertCell(4);
cell1.innerHTML = "A";
cell2.innerHTML = "B";
cell3.innerHTML = "C";
cell4.innerHTML = "D";
cell5.innerHTML = "F";
let graphValueArray = [];
let occuranceArray = [];
for(index = 0; index < 5; index++){
occuranceArray[index] = row3.insertCell(index);
occuranceArray[index].innerHTML = buildArray[index];
graphValueArray[index] = row.insertCell(index);
let styleClass = "bar" + index;
let heightValue = (buildArray[index] * 10) + "px";
graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
}
}
else {
let emptyRow = myTable.insertRow(0);
let emptyCell = emptyRow.insertCell(0);
emptyCell.innerHTML = "No graph to display";
}
}
// The argument can be changed for testing purposes
setTableContent("45 78 98 83 86 99 90 59");
my results:
Result
Here is my HTML
HTML
The problem is where you do .split(). You have to split it on space, so .split(' ')
Another tip, you don't need to include the upper limit in the if statements. The previous if statement takes care of it.
See if this works:
function parseScores(scoresString) {
// TODO: Complete the function
let inString = scoresString.split(' ');
return inString;
}
function buildDistributionArray(scoresArray) {
// TODO: Compete the function
let x = 0;
let distributeArray = new Array();
distributeArray[0] = 0;
distributeArray[1] = 0;
distributeArray[2] = 0;
distributeArray[3] = 0;
distributeArray[4] = 0;
for(i = 0; i < scoresArray.length; i++){
if (scoresArray[i] >= 90){
distributeArray[0]++;
}
else if (scoresArray[i] >= 80){
distributeArray[1]++;
}
else if (scoresArray[i] >= 70) {
distributeArray[2]++;
}
else if(scoresArray[i] >= 60){
distributeArray[3]++;
}
else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
distributeArray[4]++;
}
return distributeArray;
}
}
function setTableContent(userInput) {
// TODO: Compete the function
let myTable = document.getElementById("distributionTable");
if(userInput.length > 0) {
let parsedScores = parseScores(userInput);
let buildArray = buildDistributionArray(parsedScores);
let row = myTable.insertRow(0);
let row2 = myTable.insertRow(1);
let row3 = myTable.insertRow(2);
let cell1 = row2.insertCell(0);
let cell2 = row2.insertCell(1);
let cell3 = row2.insertCell(2);
let cell4 = row2.insertCell(3);
let cell5 = row2.insertCell(4);
cell1.innerHTML = "A";
cell2.innerHTML = "B";
cell3.innerHTML = "C";
cell4.innerHTML = "D";
cell5.innerHTML = "F";
let graphValueArray = [];
let occuranceArray = [];
for(index = 0; index < 5; index++){
occuranceArray[index] = row3.insertCell(index);
occuranceArray[index].innerHTML = buildArray[index];
graphValueArray[index] = row.insertCell(index);
let styleClass = "bar" + index;
let heightValue = (buildArray[index] * 10) + "px";
graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
}
}
else {
let emptyRow = myTable.insertRow(0);
let emptyCell = emptyRow.insertCell(0);
emptyCell.innerHTML = "No graph to display";
}
}
// The argument can be changed for testing purposes
setTableContent("45 78 98 83 86 99 90 59");
Related
I am trying to make a real time chat system. I am sending messages and saving them to the database. But before saving the message into database i need to encrypt it with using NTRU for Integers algorithm. In order to use this algorithm i have to convert text into numbers. I already tried to convert to ASCII code but its creating too big number for algorithm. Is there any way to convert text into small numbers? Saving into database with parent.send_message(chat_input.value)
create_chat(){
var parent = this;
var title_container = document.getElementById('title_container')
var title = document.getElementById('title')
title_container.classList.add('chat_title_container')
title.classList.add('chat_title')
var chat_container = document.createElement('div')
chat_container.setAttribute('id', 'chat_container')
var chat_inner_container = document.createElement('div')
chat_inner_container.setAttribute('id', 'chat_inner_container')
var chat_content_container = document.createElement('div')
chat_content_container.setAttribute('id', 'chat_content_container')
var chat_input_container = document.createElement('div')
chat_input_container.setAttribute('id', 'chat_input_container')
var chat_input_send = document.createElement('button')
chat_input_send.setAttribute('id', 'chat_input_send')
chat_input_send.setAttribute('disabled', true)
chat_input_send.innerHTML = `<i class="far fa-paper-plane"></i>`
var chat_input = document.createElement('input')
chat_input.setAttribute('id', 'chat_input')
chat_input.setAttribute('maxlength', 11)
chat_input.placeholder = `${parent.get_name()}. Say hello..`
chat_input.onkeyup = function(){
if(chat_input.value.length > 0){
chat_input_send.removeAttribute('disabled')
chat_input_send.classList.add('enabled')
chat_input_send.onclick = function(){
chat_input_send.setAttribute('disabled', true)
chat_input_send.classList.remove('enabled')
if(chat_input.value.length <= 0){
return
}
parent.create_load('chat_content_container')
chat_input.value = chat_input.value.toLocaleUpperCase()
parent.send_message(chat_input.value)
chat_input.value = ''
chat_input.focus()
}
I was able to solve my problem with this code
// Vocabulary
let vocabulary = new Array();{
vocabulary[10] = "A";
vocabulary[11] = "B";
vocabulary[12] = "C";
vocabulary[13] = "D";
vocabulary[14] = "E";
vocabulary[15] = "F";
vocabulary[16] = "G";
vocabulary[17] = "H";
vocabulary[18] = "I";
vocabulary[19] = "J";
vocabulary[20] = "K";
vocabulary[21] = "L";
vocabulary[22] = "M";
vocabulary[23] = "N";
vocabulary[24] = "O";
vocabulary[25] = "P";
vocabulary[26] = "Q";
vocabulary[27] = "R";
vocabulary[28] = "S";
vocabulary[29] = "T";
vocabulary[30] = "U";
vocabulary[31] = "V";
vocabulary[32] = "W";
vocabulary[33] = "X";
vocabulary[34] = "Y";
vocabulary[35] = "Z";
vocabulary[36] = " ";
vocabulary[37] = "a";
vocabulary[38] = "b";
vocabulary[39] = "c";
vocabulary[40] = "d";
vocabulary[41] = "e";
vocabulary[42] = "f";
vocabulary[43] = "g";
vocabulary[44] = "h";
vocabulary[45] = "i";
vocabulary[46] = "j";
vocabulary[47] = "k";
vocabulary[48] = "l";
vocabulary[49] = "m";
vocabulary[50] = "n";
vocabulary[51] = "o";
vocabulary[52] = "p";
vocabulary[53] = "q";
vocabulary[54] = "r";
vocabulary[55] = "s";
vocabulary[56] = "t";
vocabulary[57] = "u";
vocabulary[58] = "v";
vocabulary[59] = "w";
vocabulary[60] = "x";
vocabulary[61] = "y";
vocabulary[62] = "z";
vocabulary[63] = "!";
}
// Converting Text into numbers
var number = "";
for(var z = 0 ; z < chat_input.value.length ; z++)
{
for(var i = 10 ; i < 64 ; i++) {
if(chat_input.value.charAt(z) == vocabulary[i]) {
var test = i;
test = test.toString();
number = number + test;
}
}
}
// Seperating numbers into array
var numbers = new Array() ;
var index = 0 ;
while(number.length !== 0) {
if (number.length == 3) {
numbers[index] = number.substring(0, 3);
number = number.substring(3,number.length);
} else if (number.length == 2) {
numbers[index] = number.substring(0, 2);
number = number.substring(2,number.length);
} else if (number.length == 1) {
numbers[index] = number.substring(0, 1);
number = number.substring(1,number.length);
} else
numbers[index] = number.substring(0, 4);
number = number.substring(4,number.length);
index++;
}
//Encrpyting numbers and getting cipher text
var indexofe = 0 ;
var ciphertextz = new Array();
while(indexofe !== numbers.length) {
ciphertextz[indexofe] = parent.Encrypt(numbers[indexofe]);
indexofe++;
}
I am having an issue with nested if else statements. In this issue, it only executes the first if statement and skips all of the rest, when I want every statement to be executed. The else if statement works fine when there is nothing nested inside it, but when I nest additional if else statements only the first one seems to work. Here is my javascript code:
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json()).then((jsonObject) => {
const list = jsonObject['list'];
console.log(jsonObject);
for ( let i = 0; i < 5; i++){
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = list[i].main.temp;
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 1){
tempday.textContent = "Sun";
var far = list[i].main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = list[i].main.temp;
}
else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = list[i].main.temp;
}
else{
tempday.textContent = "Thu";
temperature.textContent = list[i].main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
any suggestions?
Comparaison are coded with == or ===...
So this is not good.
if (list[i].weather[i].main = "Clear")
But list[i].weather[i] something doesn't exists, I know because it's there is a big red message that appears on the console just by running the code. You maybe wanted to use list[i].weather[0].
Here is a corrected code snippet
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json())
.then((jsonObject) => {
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
console.log(jsonObject);
const list = jsonObject['list'];
for ( let i = 0; i < 5; i++){
const item = list[i];
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = item.main.temp;
if (item.weather[i].main = "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 1){
tempday.textContent = "Sun";
var far = item.main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (item.weather[0].main === "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = item.main.temp;
} else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = item.main.temp;
} else{
tempday.textContent = "Thu";
temperature.textContent = item.main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
<div class='weather-forecast-wrapper'></div>
I'm making a gem-puzzle. When I just use html and js I generate elements dynamically and put onclick on each one and when I click on it, everything works well, but when I connected the webpack it doesn't work. I rewrite the function showTable with createElement instead concatenation. And now I have an error Maximum call stack size exceeded at moveThisTile. I don't understand why? How can I fixed that? Please, help me to make this code work. There is my full code https://codepen.io/tomas777/pen/abZjZav?editors=1111
function startNewGame() {
let arrayOfNumbers = new Array();
let arrayHasNumberBeenUsed;
let randomNumber = 0;
let count = 0;
moves = 0;
rows = document.getElementById("rows").value;
columns = document.getElementById("columns").value;
textMoves.innerHTML = moves;
arrayForBoard = new Array(rows);
for (let i = 0; i < rows; i++){
arrayForBoard[i] = new Array(columns);
}
arrayHasNumberBeenUsed = new Array( rows * columns );
for (let i = 0; i < rows * columns; i++){
arrayHasNumberBeenUsed[i] = 0;
}
for (let i = 0; i < rows * columns; i++){
randomNumber = Math.floor(Math.random()*rows * columns);
if (arrayHasNumberBeenUsed[randomNumber] == 0) {
arrayHasNumberBeenUsed[randomNumber] = 1;
arrayOfNumbers.push(randomNumber);
}else
{
i--;
}
}
count = 0;
for (let i = 0; i < rows; i++){
for (let j = 0; j < columns; j++){
arrayForBoard[i][j] = arrayOfNumbers[count];
count++;
}
}
showTable();
}
function showTable() {
let tbody = document.createElement("tbody");
document.querySelector('#table').appendChild(tbody);
for (let i = 0; i < rows; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < columns; j++) {
let cell = document.createElement('td');
if (arrayForBoard[i][j] == 0) {
cell.className = 'blank';
} else {
cell.className = 'tile';
cell.onclick = moveThisTile(i, j);
cell.innerHTML =arrayForBoard[i][j];
}
tr.appendChild(cell);
}
tbody.appendChild(tr);
}
};
function moveThisTile(tableRow, tableColumn) {
if (checkIfMoveable(tableRow, tableColumn, "up") ||
checkIfMoveable(tableRow, tableColumn, "down") ||
checkIfMoveable(tableRow, tableColumn, "left") ||
checkIfMoveable(tableRow, tableColumn, "right") ) {
incrementMoves();
} else {
alert("ERROR: Cannot move tile!\nTile must be next to a blank space.");
}
if (checkIfWinner()) {
alert("Congratulations! You solved the puzzle in " + moves + " moves.");
startNewGame();
}
}
function checkIfMoveable(rowCoordinate, columnCoordinate, direction) {
let rowOffset = 0;
let columnOffset = 0;
if (direction == "up"){rowOffset = -1;}
else if (direction == "down"){rowOffset = 1;}
else if (direction == "left"){columnOffset = -1;}
else if (direction == "right"){columnOffset = 1;}
if (rowCoordinate + rowOffset >= 0 && columnCoordinate + columnOffset >= 0 &&
rowCoordinate + rowOffset < rows && columnCoordinate + columnOffset < columns
){
if ( arrayForBoard[rowCoordinate + rowOffset][columnCoordinate + columnOffset] == 0){
arrayForBoard[rowCoordinate + rowOffset][columnCoordinate + columnOffset] = arrayForBoard[rowCoordinate][columnCoordinate];
arrayForBoard[rowCoordinate][columnCoordinate] = 0;
showTable();
return true;
}
}
return false;
}
function checkIfWinner(){
let count = 1;
for (let i = 0; i < rows; i++){
for (let j = 0; j < columns; j++){
if (arrayForBoard[i][j] != count){
if ( !(count === rows * columns && arrayForBoard[i][j] === 0 )){
return false;
}
}
count++;
}
}
return true;
}
from the below code I'm trying to get common words(from two strings given) without converting the strings into array. The below code is getting and displaying the common words but the problem is, this code is not removing all the duplicates as it shows all the common words without removing duplicates. I tried searching, but the solution is to use split() and filter(). Is there any other way to remove duplicates.
Thanks so much in advance.
function common() {
var str1 = "is hello and he is the only hello is"
var str2 = "is hello you and is and he and is the only";
var min = 0;
var max = 0;
var count = 0;
var count1 = 0;
var count2 = 0;
var out = '';
var out2 = '';
var out3 = '';
var len1 = str1.length;
var len2 = str2.length;
var output = '';
var temp = 0;
var temp1 = 0;
for (m = 0; m < str1.length; m++) {
temp1 = 0;
if (str1.charAt(m) == " " || m == str1.length - 1) {
count1++;
if (m == str1.length - 1) {
out1 = str1.slice(temp, m + 1);
} else {
out1 = str1.slice(temp, m);
}
for (i = temp1; i < str2.length; i++) {
if (str2.charAt(i) == " " || i == str2.length - 1) {
if (i == str2.length - 1) {
out2 = str2.slice(temp1, i + 1);
} else {
out2 = str2.slice(temp1, i);
}
temp1 = i + 1;
if (out1 == out2) {
if (out3.indexOf(out1) == -1) {
out3 += out1 + ' ';
} else if (out3.indexOf(out1) >= 0) {
var r = out3.indexOf(out1);
while (out3.charAt(r) != " ") {
r++;
}
if (r != out1.length) {
out3 += out1 + ' ';
}
}
}
}
}
temp = m + 1;
}
}
console.log(out3);
out = document.getElementById("tarea3");
out.value = out3;
}
<textarea id="tarea"></textarea>
<textarea id="tarea2"></textarea>
<textarea id="tarea3"></textarea>
<button type="button" onclick="common()">Run</button>
No arrays no regex you can get the common words like this. The rest is up to your processing.
var str1 = "is hello and he is the only hello is",
str2 = "is hello you and is and he and is the only",
lut = {},
stc = "",
i = 0;
while (i <= str1.length) {
if (str1[i] !== " " && i < str1.length) {
stc+=str1[i++];
} else {
lut[stc] = "unmatch";
stc = "";
++i;
}
}
i = 0;
stc = "";
while (i <= str2.length) {
if (str2[i] !== " " && i < str2.length) {
stc+=str2[i++];
} else {
lut[stc] = lut[stc] ? "common" : "unmatch";
stc = "";
++i;
}
}
console.log(lut);
I am creating a website application that allows users to select a seat, if it is not already reserved, and reserve it.
I have created a very round about way of getting the seats that are previously reserved using iFrames, however that was temporarily, now I need to make it secure and "proper javascript code" using proper practices. I have no clue what AJAX (or JSON) is, nor how to add it to this code, but it needs to get the file "seatsReserved"+this.id(that is the date)+"Que.html" and compare the string of previously reserved seats to see which class to make the element. If this is horrible, or if any of the other things could work better, I am open to criticism to everything. Thank you all!
Here is the javascript code:
A little side note, all of the if statements are due to different amount of seats in each row
<script>
var i = " 0 ";
var counter = 0;
var leng=0;
document.getElementById("Show1").addEventListener("click", changeDay);
document.getElementById("Show2").addEventListener("click", changeDay);
document.getElementById("Show3").addEventListener("click", changeDay);
function changeDay() {
var iFrame = document.getElementById("seatList");
iFrame.src = "seatsReserved" + this.id + "Que.html";
document.getElementById('date').innerHTML = this.id;
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.removeEventListener("click", selectedSeat);
}
else {
document.getElementById(let +k).className = "openseat";
document.getElementById(let +k).removeEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
}
function loadChanges() {
var iFrame = document.getElementById("seatList");
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
var leng = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.addEventListener("click", selectedSeat);
seat.className = "openseat";
}
else {
document.getElementById(let +k).className = "notAvailible";
document.getElementById(let +k).addEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
i = " 0 ";
counter = 0;
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
document.getElementById("seatnums").innerHTML = counter;
}
i = document.getElementById("seatString").innerHTML;
counter = document.getElementById("seatnums").innerHTML;
function selectedSeat() {
var w = this.id;
var l = (" " + w);
var b = (" " + w + " ");
if (counter < 5) {
if (i.indexOf(b) <= 0) {
this.className = "closedseat";
i = i + b;
i = i.replace(" 0 ", " ");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter + 1;
document.getElementById("seatnums").innerHTML = counter;
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
function doNothing() {
}
var rannum = Math.random() * 1000;
document.getElementById('getConfirmation').value = rannum;
</script>