var text = "some text jjke kjerk jker helmi kjekjr helmi ekjrkje helmi";
var myName = "helmi";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === 'h') {
for (var j = i; j < text[i] + myName.length; j+=1) {
}
hits.push('text[j]');
}
};
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
I want it to find "myName" in the "text", and push it. But it only pushes whatever I put in the parenthesis after hits.push. What is wrong with this code?
But it only pushes whatever I put in the parenthesis after hits.push
Exactly, which is why you don't want to put a string in there:
hits.push('text[j]');
but a variable value:
hits.push(text[j]);
for (var i = 0; i < text.length; i++) {
if (text[i] === 'h') {
for (var j = i; j < text[i] + myName.length; j+=1) {
hits.push(text[j]);
}
}
your code has bracket problem and in some case you're doing wrong .
You can use split function. It gives you an opportunity to check your name word by word instead of checking your name character by character.
var text = "some text jjke kjerk jker helmi kjekjr helmi ekjrkje helmi";
var myName = "helmi";
var hits = [];
var texts = text.split(" ");
for (var i = 0; i < texts.length; i++) {
if (texts[i] === myName) {
hits.push(texts[i]);
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
Simple example for what you are trying to do is
if(text.indexOf(myName)!=-1)
console.log(myName);
else
console.log("Your name wasn't found");
Related
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 */ }
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);
}
I have a game hangman. But I tried to make the logic, that if a user enters the wrong letter, then the user will get a message that he/she has to try again.
But now even if the user has chosen the correct letter, the user will get the message that he/she has to try it again.
This is the code:
<script>
var words = [
"ha",
"pe",
"jaa"
];
var word = words[Math.floor(Math.random() * words.length)];
var answareArray = [];
for (var i = 0; i < word.length; i++) {
answareArray[i] = "_";
}
var remainingLetters = word.length;
while (remainingLetters > 0) {
//gaming code
alert(answareArray.join(" "));
//Get a gues from the user:
var guess = prompt("Guess a letter, or click cancel to stop");
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter");
} else {
for (var j = 0; j < word.length; j++) {
if (word[j] !== guess) {
debugger;
alert("try again");
}
if (word[j] === guess) {
if (answareArray[j] !== "_") {
alert("Letter already be guessed");
break;
} else {
answareArray[j] = guess;
remainingLetters--;
}
}
}
}
}
alert(answareArray.join(" "));
alert("Good Job the answare was: " + word);
</script>
And in this part:
if (word[j] !== guess) {
debugger;
alert("try again");
}
I try to return the message to the user.
So what do I have to correct?
You have to show the alert after you checked all the letters, you're currently showing it at each iteration.
You can do that by using a flag (a boolean variable).
Here is an example:
var goodGuess = false;
for (var j = 0; j < word.length; j++) {
if (word[j] === guess) {
goodGuess = true;
if (answareArray[j] !== "_") {
alert("Letter already be guessed");
break;
} else {
answareArray[j] = guess;
remainingLetters--;
}
}
}
if(!goodGuess){
alert("try again");
}
What this does, is to first set the goodGuess variable to false, then go through all the letters and if the user's guess is equal to any of them, set goodGuess to true.
At the end of the loop (after all the letters were checked), if goodGuess is false, the alert is shown.
I am trying to do a pop-up warning before the sales order is saved if the exact same item is entered twice when the order is created/modified on Netsuite. However, there is no window popping up and I am not sure what is wrong with the script. Here is what I got:
function validateitem (type){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (type == 'item' && numLine > 0) {
for(var i = 0; i < numLine; i++) {
var itemSO = {};
itemSO.id = nlapiGetLineValue('item','item',i);
if (itemSO.id != null && itemSO.id !=''){
for (var j = 0; j < numLine; j++){
if(itenArr.indexOf(itemSO[i].id) === -1) {
itemArr.push(itemSO[i].id);}
else{
if (!confirm('You have entered a duplicate item for this sales order. Continue?'))
{
flag = false;
}
}
}
}
}
}
return flag;
}
Can somebody help, please?
Here is a slightly edited version:
function validateitem (){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (numLine > 0) {
for(var i = 1; i <= numLine; i++) {
var itemSO = nlapiGetLineItemValue('item','item',i);
if (itemSO != null && itemSO !=''){
for (var j = 1; j <= numLine; j++){
if(itemArr.indexOf(itemSO[i]) === -1) {
itemArr.push(itemSO[i]);}
else{
flag = false;
}
}
}
}
}
if (flag == false){
alert('You have entered the same item twice.Continue?');
}
return flag;
}
This is the complete after-edit code that works:
function validateitem (){
var flag = true;
var numLine = nlapiGetLineItemCount('item');
itemArr = [];
if (numLine > 0) {
for(var i = 1; i <= numLine; i++) {
var itemSO = nlapiGetLineItemValue('item','item',i);
if (itemSO != null && itemSO !=''){
for (var j = i+1; j <= numLine; j++){
var itemSOplus = nlapiGetLineItemValue('item','item',j);
if(itemSO === itemSOplus) {
flag = false;
}
}
}
}
}
if (flag == false){
alert('You have entered the same item twice.Continue?');
}
return flag;
}
Thanks to Krypton!!
As per SuiteAnswers ID 10579, there are no paramters passed to the saveRecord client event. Therefore when your code checks the following:
if (type == 'item' && numLine > 0)
it finds that type equals undefined, so the condition is not met and the code will jump straight down to return flag which has been set to true.
Also note that in SuiteScript 1.0, line indexes start from 1 - not 0 as your code seems to assume.
EDIT - adding comment to form part of this answer:
I'd like to understand your logic behind itemSO[i] - as itemSO is not an array. Why not just compare the item from the current line of the inner loop with the current line of the outer loop and set the flag false if they match? Also, the inner loop need only start from j = i + 1 as the previous lines would have already been compared.
I'm experimenting with data-, and ran into a problem that I can't solve at the moment.
I'm trying to scrollIntoView, e.g.: [0] , for the first 'clients[i]':
function search() {
searchTerm = document.getElementById("searchInput").value;
clients = document.getElementsByClassName("client");
for (var i = 0; i < clients.length; i++) {
find = clients[i].getAttribute("data-search-firstname");
if (searchTerm === find) {
console.log("Found: " + find);
selClient(clients[i]);
clients[i].scrollIntoView(); // not sure where to put [0] without an error
}
}
}
Thanks a ton!
EDIT:
For example, if I have multiple of the same 'clients[i]', I want the first client to scrollIntoView.
You just need to identify if i is the first result
function search() {
searchTerm = document.getElementById("searchInput").value;
clients = document.getElementsByClassName("client");
for (var i = 0; i < clients.length; i++) {
find = clients[i].getAttribute("data-search-firstname");
if (searchTerm === find) {
console.log("Found: " + find);
selClient(clients[i]);
if (i === 0) {
clients[i].scrollIntoView();
}
}
}
}
Or in your case it looks like you only want it scrolling to the first result in loop where searchterm === find so you might want to do this:
function search() {
searchTerm = document.getElementById("searchInput").value;
clients = document.getElementsByClassName("client");
var scrolled = false;
for (var i = 0; i < clients.length; i++) {
find = clients[i].getAttribute("data-search-firstname");
if (searchTerm === find) {
console.log("Found: " + find);
selClient(clients[i]);
if (!scrolled) {
clients[i].scrollIntoView();
scrolled = true;
}
}
}
}