What's wrong with my Code [Iteration Structure] - javascript

I am trying to have an array that contains values of each character of a key typed and I was getting errors. Therefore, I took a very tiny snippet and did lots of tries to see what was going on:
for (var j = 0; j < code.length; j++) {
var UnitPlace = codeLength - j;
if (code[j] = "0") {
value = 0;
encryption.push(0);
}
else {
if (code[j] = "1") {
value = 1;
encryption.push(1);
}
else {
if (code[j] = "2") {
value = 2;
encryption.push(2);
}
else {
if (code[j] = "3") {
value = 3;
encryption.push(3);
}
else {
if (code[j] = "4") {
value = 4;
encryption.push(4);
}
else {
if (code[j] = "5") {
value = 5;
encryption.push(5);
}
else {
if (code[j] = "6") {
value = 6;
encryption.push(6);
}
else {
if (code[j] = "7") {
value = 7;
encryption.push(7);
}
}
}
}
}
}
}
}
}
document.write(encryption);
I was able to realize that only the first if statement is read in the iteration sequence. but the source code you see above is after i realized this and tried having it read the other parts of the sequence but it still reads only the first if statement.
Could someone tell me how to solve it and/or what the problem with my code is?

Are you looking for something like this? :
let strCode = "1234567";
let encryption = strCode.split('').map(c => parseInt(c));
document.write(encryption);

Related

JavaScript - Nested FOR loop executing twice

I have a nested FOR loop that iterates over an object, and updating variables or attributes based on conditions. The last 'IF' on this loop formats a date and updates the field accordingly. For some reason the final line causes the loop to iterate twice, therefore updating the 'formattedDate' variable twice (in the object, there is only one single element with the 'childTag' = 'date'. Any help is appreciated!
for (let i = 0; i < submitFormData.tabs.length; i++) {
for (let j = 0; j < submitFormData.tabs[i].elements.length; j++) {
if (submitFormData.tabs[i].elements[j].childTag == "project_name") {
projectName = submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "location_store") {
locationStore = ' #' + submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "location_street") {
locationStreet = submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "location_city") {
locationCity = submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "location_state") {
locationState = submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "location_zip") {
locationZip = submitFormData.tabs[i].elements[j].value;
} else if (submitFormData.tabs[i].elements[j].childTag == "date") {
if (submitFormData.tabs[i].elements[j].value != "") {
console.log(submitFormData)
date = (submitFormData.tabs[i].elements[j].value.substring(0, 10));
formattedDate = (date.substring(5, 11) + '-' + date.substring(0, 4));
console.log(formattedDate, 'formatted date')
submitFormData.tabs[i].elements[j].value = formattedDate;
}
}
}
}
Sample of Object
I see that in the loop you manipulate the list which you iterate over.
submitFormData.tabs[i].elements
It would be better to make a copy of it and iterate over it.
const newSubmitFormData = [...submitFormData.tabs[i].elements]
for (let j = 0; j < newSubmitFormData .length; j++) { /* your code */ }

TypeError in JavaScript when setting the Y position

When running this code I currently getting the error "TypeError: Invalid value for y-coordinate. Make sure you are passing finite numbers to setPosition(x, y)." all of my functions do work are are declared properly. When I use a println and print out letYPos it prints out "NaN" but when I call the function again it prints out the correct value. Does anyone know how I can fix this or if there even is a way to fix this?
var count = 0;
var letYPos = 0;
var y = 3;
function testing()
{
var letXPos = 25;
letYPos += 75;
if (count == 6)
{
println("You have ran out of guesses, the correct anwser was: " + secretWord);
return;
}
var input = readLine("Enter your word: ");
if (input == null)
{
println("You have to enter a word! ");
return;
}
if (input.length != 5)
{
println("That is not a five letter word, please try again.");
return;
}
var x = 3;
for (var i = 0; i < input.length; i++)
{
if (input.includes(secretWord.charAt(i)))
{
var index = input.indexOf(secretWord.charAt(i));
}
if (index == 0) { yellow(3, y ) }
if (index == 1) { yellow(83, y ) }
if (index == 2) { yellow(163, y) }
if (index == 3) { yellow(243, y) }
if (index == 4) { yellow(323, y) } index = null;
}
for (var i = 0; i < input.length; i++)
{
if (input.charAt(i) == secretWord.charAt(i))
{
green(x, y);
}
x += 80;
}
y += 80;
for (var i = 0; i < input.length; i++)
{
for (var a = 0; a <= 5; a++)
{
var txt = new Text(input.charAt(a), font);
txt.setPosition(letXPos, letYPos);
add(txt);
letXPos += 80;
}
}
if (input == secretWord)
{
println("That's Correct, Congratulations!");
return;
}
count++;
setTimeout(testing, 100);
}

Even after I give the condition truth, the answer here always looks false

Even after I give the conditioned truth, the answer here always looks false.
var AlloverName = ['Iqbal', 'Saidul', 'Ali Akbar']
var isOkay = false
function detailsAllOfYou(activity) {
for (i = 0; i <= activity.length; i++) {
if (activity[i] === 'Saidul') {
console.log("Name is found")
isOkay = true;
break
}
if (!isOkay) {
console.log("Name is not found")
break
}
}
}
detailsAllOfYou(AlloverName)
You need to change your logic, because the array sees the first element isnt equal then breaks, so change it to
var AlloverName = ['Iqbal', 'Saidul', 'Ali Akbar']
var isOkay = false
function detailsAllOfYou(activity) {
for (let i = 0; i <= activity.length; i++) {
if (activity[i] === 'Saidul') {
console.log("Name is found")
isOkay = true;
break
}
}
if (!isOkay) {
console.log("Name is not found")
}
}
detailsAllOfYou(AlloverName)

Why am I getting an undefined alert when entering a number into my array?

When I input numbers into the array, I get two alerts: a "number" alert, and then an "undefined" alert. I don’t know how to fix this.
This is my JavaScript code:
var myStuff = [];
function myfunctionA() {
var enteredvalue = document.getElementById("numbers").value;
alert(typeof Number(document.getElementById('numbers').value));
if (enteredvalue == "") {
alert("Input is not a number");
} else if (isNaN(enteredvalue)) {
alert('You need to enter a valid number!');
}
var elementExists = false;
var x = document.getElementById('numbers').value;
for (var i = 0; i < myStuff.length; i++) {
if (myStuff[i] == Number(x)) {
elementExists = true;
}
}
if (elementExists != true) {
myStuff.push(Number(enteredvalue));
alert('Thank You for entering a valid number'. myStuff);
} else {
alert('Element is here');
}
}
function myfunctionB() {
window.alert(myStuff.length);
}
function myfunctionC() {
var sum = 0;
for (var i = 0; i < myStuff.length; i++) {
sum += myStuff[i];
}
alert(sum);
}
function myfunctionD() {
if (myStuff.length == 0) {
alert("already empty");
} else {
myStuff = [];
}
alert("Array Empty");
}
function myfunctionE() {
alert(myStuff.join('\n')); {
if (myStuff == [])
{
alert("Enter something into Array")
}
}
}
function bubbleSort() {
var sorted = true;
var temp;
while (sorted) {
sorted = false;
for (var i = 0; i < myStuff.length - 1; i++) {
if (myStuff[i] < myStuff[i + 1]) {
temp = myStuff[i];
myStuff[i] = myStuff[i + 1];
myStuff[i + 1] = temp;
sorted = true;
}
}
}
}
The following line evaluated to undefined:
alert('Thank You for entering a valid number'. myStuff);
Do you maybe mean?:
alert('Thank You for entering a valid number' + myStuff);

Pause screen Canvas Not updating

By clicking space you get the pause screen. I am trying to update the score but I think my if, if else loops are wrong, I am getting no error however.
Just click here and while playing hit space:
http://www.taffatech.com/Snake.html
The code that is trouble I think is:
function SetSize()
{
if (document.getElementById('Easy').checked)
{
cellSize = 10;
Mode = 1;
}
else if (document.getElementById('Medium').checked)
{
cellSize = 20;
Mode = 2;
}
else if (document.getElementById('Hard').checked)
{
cellSize = 30;
Mode = 3;
}
}
function init()
{
if (Mode == 1)
{
scoreEasy = easyScore;
if(score > scoreEasy) {
easyScore = scoreEasy;
}
}
else if (Mode == 2)
{
scoreMedium = mediumScore;
if(score > scoreMedium) {
mediumScore = score;
}
}
else if (Mode == 3)
{
scoreHard = hardScore;
if(score > scoreHard) {
highScore = score;
}
}
I'm guessing here
scoreHard = hardScore;
if(score > scoreHard) {
Should that be:
scoreHard = Math.max(score,hardScore);
And also highScore = score; in the hardScore part, shouldn't that be hardScore = score;?

Categories