How discover the element with the max font-size - javascript

I'm trying to answer the call of a function based on comparison of THIS element (keyword) font-size with font-sizes of other elements:
This is inside a prototype:
The self.settings.same is an array with the other elements.
self.settings.same.forEach(function (item) {
var me = parseInt(self.element.css('fontSize').substring(-1, 2), 10);
var other = parseInt(item.css('fontSize').substring(-1, 2), 10);
if (other > me) {
return false;
}
return true;
});
I want return false if the others elements have the font-size bigger then the current element, but this is not working because it makes the loop only once, so the comparison occurs only once.

If you can return false only when all others elements are bigger than me, then this should work.
self.settings.same.forEach(function (item) {
var me = parseInt(self.element.css('fontSize').substring(-1, 2), 10);
var other = parseInt(item.css('fontSize').substring(-1, 2), 10);
if (other < me) {
return true;
}
});
return false;

Related

Javascript - Two parameters and return object

So I have been started kinda basic with JavaScript and trying to learn to programming in JavaScript at this point. I have been studying Java before but I can't really get this on my mind. How to make it to work. Been doing some really weird stuff but no luck haha.
Anways
Im stuck at this sort of:
function item(big, low) {
}
var items = item(5, 10);
console.log(items.big);
console.log(items.low);
so what i'm trying to do is like what you see. I want to print it just for each objects in the console.log. But can't get it to work. I tried to do a return big; return low; but no luck. I would more appreciate what I should think rather than have the code written. What should I think at this point?
EDIT:
function Triangle(big, low) {
return {
big: big, low: low
}
}
var items = item(5, 10);
console.log(items.height);
console.log(items.width);
console.log(items.area());
so now I want to use a method and to a math function with area() "height * width" but when I did this:
function Triangle(big, low) {
return {
big: big, low: low
}
}
function Area(big, low) {
}
var items = item(5, 10);
console.log(items.height);
console.log(items.width);
console.log(items.area());
it immediately told me that area() is not a function. What did I go wrong?
You must define what you want return.
function item(big, low){
return { big: big, low: low}
}
function item(first, second) {
var firstIsBigger = first > second
var big = firstIsBigger ? first : second
var low = firstIsBigger ? second: first}
return {big: big, low: low}
}
item(5, 10) //returnes {big: 10, low: 5}
ES6 solution:
const item = (...numbers) => {
cosnt [min, max] = numbers.sort()
return {max, min}
}
You can have n number of arguments
Math.min and Math.max will determine min and max value from arguments array.
function item() {
var args = [].slice.call(arguments);
var max = Math.max.apply(null, args);
var min = Math.min.apply(null, args);
return {
big: max,
low: min
}
}
var items = item(5, 10);
console.log(items.big);
console.log(items.low);

Return value from function whilst using setTimeout

Is there any way to have a function run with a setTimeout and also return a value assigned to a variable?
An example of what I mean is like this:
function count(start, target) {
if (start < target) {
start += 0.1;
return start;
} else if (start > target) {
return target;
}
// i'm fully aware the return above will prevent this line occur
// this is my issue currently
setTimeout(function () {
count(start, target)
}, 1000);
}
var el = document.getElementById('test');
var value = count(0.0, 1.0); //get the opacity value
function check() {
el.style.opacity = value; //assign what the value is in this moment
if (value != 1.0) {
setTimeout(check, 0);
}
}
check();
I know this code won't work the way i want it because return exit's the function, I wrote it to explain what I am trying to do.
The reason I want to do this in this kinda way is because I have an element which i want to fade in by altering it's opacity.
So i have a function which would increment a start value to a target value using what ever easing i want, this then returns said value which would be assigned to the element's opacity property.
I don't want to pass the element to this count function because that means it limits the use of it for when i want to animate other things besides elements.
What would the solution to this problem be?
I think what you are trying to do is to call the count method again if the value passed for start and target are not the same if so
function count(start, target) {
var ret;
if (start < target) {
ret = start + 0.1;
} else if (start > target) {
ret = target;
}
if (start != target) {
setTimeout(function () {
count(ret, target)
}, 1000);
}
return ret;
}

Javascript square function

I want to write a function that checks if the given number has a certain order.
The second number has to be the square of the previous number.
The first number can only be 0 - 9.
So for example 2439 would return 'true' because 4 is the square of 2 and 9 is the square of 3.
39416 would also give 'true', and for example 1624 would return 'false'.
I don't really have an idea how to do this. It should be a recursive function but an example of how to do it without recursion would be helpfull too.
I would try something like this:
function isOrdered(input){
var position = 0;
while(position<input.length-2)
{
var currentFirstNumber = parseInt(input[position]);
if(currentFirstNumber<=2) {
if (Math.sqrt(parseInt(input[position + 1])) !== currentFirstNumber)
return false;
else
position+=2;
}
if(currentFirstNumber>=4 && currentFirstNumber<=9)
{
var squared = input.substring(position+1,position+3);
if(Math.sqrt(parseInt(squared))!==currentFirstNumber)
return false;
else
position=position+3;
}
}
return true;
}
console.log(isOrdered("2439")); // outputs true
console.log(isOrdered("39416")); // outputs true
console.log(isOrdered("1624")); // outputs false
I pass the number to the function as a string.
Take a look at this recursive function
function detectOrder(input)
{
var input = input.toString()
var first = input.substr(0,1);
var power = Math.pow(parseInt(first), 2);
var powerLength = power.toString().length;
if ( parseInt(input.substr(1, powerLength)) == power )
{
if (input.length <= 1+powerLength)
{
return true;
}
else
{
return detectOrder(input.substr(1+powerLength));
}
}
else
{
return false;
}
}
As mention in the comment section, OP said that the 'firsts' are limited to 0..9. So the easiest way to accomplish this is by going through the power function instead of the square root function.
UPDATE: Sorry, you asked for JavaScript code. Be careful with the FIRST CALL. if you manually pass to the function the last position, it will return true.
function verification(number, position){
var str = String(number); // Cast number to String
if(str.length > position){ // Verify valid position
var value = str.substr(position, 1); // take the 'first' value
var pow = Math.pow(value, 2); // Calculate the power
// Verify if the next value is equivalent to the power
if(str.indexOf(pow, position) == position + 1){
// Recursive call to verify the next position
return verification(number, position + String(pow).length + 1);
} else {
// If not equivalent, you found an inconsistency.
return false;
}
// If you ran it until you reached the last position, you're good to go.
}else if(str.length == position){
return true;
}
}
console.log(verification(39416, 0)); // True
console.log(verification(39415, 0)); // True
console.log(verification(981524, 0)); // false
console.log(verification(981525, 0)); // true
console.log(verification(98525, 0)); // false

return biggest/highest object from jquery elements

Howdey!
Let's take a look at the following jQuery function:
$.fn.getMax = function() {
return this.height(Math.max.apply(this, $(this).map(function(i, e) {
return $(e).height();
}).get()));
};
It returns and sets the heighest height for all selectors. But what is, if you want to return the object (not the height) with the heighest value?
So if you call the function like this:
$(selector).getMax().css({backgroundColor: "indigo"});
...how the element with the heighest height gets the backgroundColor?
UPDATE
I've managed it now with $.makeArray, as Amareswar said it.
$.fn.getMax = function(prop) {
var max = $.makeArray($(this)).sort(function(a, b) {
return (parseInt($(b).css(prop), 10) || 1) - (parseInt($(a).css(prop), 10) || 1);
}).shift();
return $(max);
};
Cheers!
Try this:
$.fn.getMax = function() {
/* create array of heights*/
var heights = $(this).map(function(i, e) {
return $(e).height();
}).get();
/* get max height*/
var max = Math.max.apply(this, heights);
/* get index of max in array*/
var pos = $.inArray(max, heights)
/* return element with proper index*/
return this.eq(pos);
};
DEMO: http://jsfiddle.net/tTuE7/
EDIT : assumes you only want one element returned

Random Number with javascript or jquery

I am trying to make a script to pick random number between two numbers . but it picks same number sometimes. i donot want to repeat same number until array is finished .
Here is my code
$(document).ready(function () {
abc();
test = array();
function abc() {
res = randomXToY(1, 10, 0);
$('#img' + res).fadeTo(1200, 1);
//$(this).addClass('activeImg');
//});
setTimeout(function () {
removeClassImg(res)
}, 3000);
}
function removeClassImg(res) {
$('#img' + res).fadeTo(1200, 0.1);
//$('#img' + res).removeClass('activeImg');
abc();
}
function randomXToY(minVal, maxVal, floatVal) {
var randVal = minVal + (Math.random() * (maxVal - minVal));
return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}
});
Does Anybody have idea about this ...
You'll have to maintain a list of numbers that have already been generated, and check against this list. Re-generate a new number if you find a dupe.
If you do not want the random numbers repeating themselves you have to keep track of the some way.
If you have the range you are dealing with is relatively small, you can create an array with all possible results and simply randomly pick out of it.
function Randomizer(minVal, maxVal, floatVal){
var possible_results = []; // for larger arrays you can build this using a loop of course
var randomization_array = [];
var count = minVal;
var incrementor = floatVal || 1; // set the distance between possible values (if floatVal equals 0 we round to 1)
while (count <= maxVal) {
possible_results.push(count);
count += incrementor;
}
this.run = function(){
// if randomization_array is empty set posssible results into it
randomization_array = randomization_array.length ? randomization_array : $.merge(randomization_array, possible_results);
// pick a random element within the array
var rand = Math.floor(Math.random()*randomization_array.length);
// return the relevant element
return randomization_array.splice(rand,1)[0];
}
}
and in order to use it (it creates a specialized object for each possible range):
rand = new Randomizer(1,10,0);
rand.run();
note that this approach does not work well for very large ranges

Categories

Resources