Javascript For Loop...One function in another? [duplicate] - javascript

This question already has answers here:
How to count vowels in a Javascript string with two functions?
(5 answers)
Closed 6 years ago.
Alright I cant seem to get this to work. I have one function written so far. It works great. Now I need to get my new function to call the second function.
Here is the first function:
function isVowelR(str) {
if(str = str.match(/[aeiou]/gi))
return true
else
return false
}
This one works fine. Returns true if str is a vowel. Here is the one Im stuck on though. This is what Ive tried (as well as some other stuff.
function countVowels(str) {
var count = 0
for (var i = 0; i == str.length; i++)
{
if (i == isVowelR(i))
{
++count
}
}
return count
}
This second function needs count how many vowels are in the string that is entered (which I figured out in a different function). But how do I get it to work if I am required to call the first function?

The problem is that the i variable is a number, so what you actually do is isVowelR(0), isVowelR(1), isVowelR(2) ...
You can change this to:
isVowelR(str[i])
Another option is just to check the length of the match:
str.match(/[aeiou]/gi).length
This way you don't need to loop over each character on the string:
function countVowels(str) {
return str.match(/[aeiou]/gi).length
}

Related

How to use a variable in a Jquery selector [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you
Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

Is possible to add NUMBER value return in Js? [duplicate]

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 3 years ago.
I am using Javascript. In success function, I am getting some results and storing in var say "baldet" in JS-CODE.
example:
success: function (result) {
baldet = result;
console.log(baldet[0].cleavebalance);
if (openmon == 1){
$("#clvbalance").val(baldet[0].cleavebalance);
}
}
from baldet[0].cleavebalance I am getting 2. My condition is if openmon == 1 then I want to add number 1 in it.
NOTE: add means SUM, not CONCAT()
So that i could get, 2 (from "baldet[0].cleavebalance" + 1 (number i want to add)) i.e. 2 + 1 = 3.
and show in $("#clvbalance").val("3"); // value 3 is after getting SUM RESULT
Currently, I am getting "21" instead of "3"
I hope I made my query clear. Any help will be appreciated.
You need to parse from string to number like below
console.log("2" + 1); // dose not parse
console.log(parseInt("2") + 1)

Write a function that accepts an array of test scores as an argument then the function should return a number of scores that 80 or higher? [duplicate]

This question already has answers here:
How to filter array values greater than x
(4 answers)
Closed 4 years ago.
I'm still pretty new into JS but I'm having a hard time with this problem. I'm sure the answer is somewhat simple but i can't figure out what to put into the function expression after i've made my array of scores? Do i need to do a for loop?
Thanks
array = [20,30,40,50,60,70,80,92,93,95,98,100]
function testScores(array){
}
you can use array's filter method. please follow below definition.
function testScores(array){
return array.filter(ele=>ele>=80)
}
function testScores(array){
return array.filter(number => number >= 80);
}
This will return an array of scores more 80 and above.
e.g
let scores = [20,30,40,50,60,70,80,92,93,95,98,100];
testScores(scores)
If you just want the count of scores more than 80 you can do
testScores(scores).length

finding a index of string [duplicate]

This question already has answers here:
How to check if the URL contains a given string?
(21 answers)
Closed 6 years ago.
I have a long array of strings with page IDs; when the current page ID matches one from the array, stuff needs to happen (alert in this test).
The alert pops up on any pages, regardless if the url contains one of the ids from the array or not. What is wrong with my if statement: if(pageHref.indexOf(id))? thanks for any advise
var pageHref = window.location.href;
var ids = ['14528','14417','17529'];
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
if (id.length > 0) {
if(pageHref.indexOf(id)){
//do something
alert('a');
}
}
}
A blog post here has a table that explains what values evaluate as true in javascript conditional statements. For numerical values it states only +0,-0 and NaN evaluate to false. Because indexOf returns -1 for no match, it is evaluating to true for non-matching ids. You would want to use:
if(pageHref.indexOf(id) > -1)
IndexOf returns a number, if the number is -1, there is no match.
Try for example.
pageHref.indexOf(id) != -1.

javascript - check if string is in a array without case sensitive [duplicate]

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.
Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.
This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.
I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}

Categories

Resources